2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * IEEE 802.11 generic handler
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/socket.h>
42 #include <net/if_var.h>
43 #include <net/if_dl.h>
44 #include <net/if_media.h>
45 #include <net/if_types.h>
46 #include <net/ethernet.h>
48 #include <netproto/802_11/ieee80211_var.h>
49 #include <netproto/802_11/ieee80211_regdomain.h>
50 #ifdef IEEE80211_SUPPORT_SUPERG
51 #include <netproto/802_11/ieee80211_superg.h>
53 #include <netproto/802_11/ieee80211_ratectl.h>
57 #define IEEE80211_NMBCLUSTERS_DEFMIN 32
58 #define IEEE80211_NMBCLUSTERS_DEFAULT 128
60 static int ieee80211_nmbclusters_default = IEEE80211_NMBCLUSTERS_DEFAULT;
61 TUNABLE_INT("net.link.ieee80211.nmbclusters", &ieee80211_nmbclusters_default);
63 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
64 [IEEE80211_MODE_AUTO] = "auto",
65 [IEEE80211_MODE_11A] = "11a",
66 [IEEE80211_MODE_11B] = "11b",
67 [IEEE80211_MODE_11G] = "11g",
68 [IEEE80211_MODE_FH] = "FH",
69 [IEEE80211_MODE_TURBO_A] = "turboA",
70 [IEEE80211_MODE_TURBO_G] = "turboG",
71 [IEEE80211_MODE_STURBO_A] = "sturboA",
72 [IEEE80211_MODE_HALF] = "half",
73 [IEEE80211_MODE_QUARTER] = "quarter",
74 [IEEE80211_MODE_11NA] = "11na",
75 [IEEE80211_MODE_11NG] = "11ng",
77 /* map ieee80211_opmode to the corresponding capability bit */
78 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
79 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
80 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
81 [IEEE80211_M_STA] = IEEE80211_C_STA,
82 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
83 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
84 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
85 #ifdef IEEE80211_SUPPORT_MESH
86 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
90 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
91 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
93 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
94 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
95 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
96 static int ieee80211_media_setup(struct ieee80211com *ic,
97 struct ifmedia *media, int caps, int addsta,
98 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
99 static void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
100 static int ieee80211com_media_change(struct ifnet *);
101 static int media_status(enum ieee80211_opmode,
102 const struct ieee80211_channel *);
104 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
107 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
109 #define B(r) ((r) | IEEE80211_RATE_BASIC)
110 static const struct ieee80211_rateset ieee80211_rateset_11a =
111 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
112 static const struct ieee80211_rateset ieee80211_rateset_half =
113 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
114 static const struct ieee80211_rateset ieee80211_rateset_quarter =
115 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
116 static const struct ieee80211_rateset ieee80211_rateset_11b =
117 { 4, { B(2), B(4), B(11), B(22) } };
118 /* NB: OFDM rates are handled specially based on mode */
119 static const struct ieee80211_rateset ieee80211_rateset_11g =
120 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
124 * Fill in 802.11 available channel set, mark
125 * all available channels as active, and pick
126 * a default channel if not already specified.
129 ieee80211_chan_init(struct ieee80211com *ic)
131 #define DEFAULTRATES(m, def) do { \
132 if (ic->ic_sup_rates[m].rs_nrates == 0) \
133 ic->ic_sup_rates[m] = def; \
135 struct ieee80211_channel *c;
138 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
139 ("invalid number of channels specified: %u", ic->ic_nchans));
140 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
141 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
142 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
143 for (i = 0; i < ic->ic_nchans; i++) {
144 c = &ic->ic_channels[i];
145 KASSERT(c->ic_flags != 0, ("channel with no flags"));
147 * Help drivers that work only with frequencies by filling
148 * in IEEE channel #'s if not already calculated. Note this
149 * mimics similar work done in ieee80211_setregdomain when
150 * changing regulatory state.
153 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
154 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
155 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
156 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
158 /* default max tx power to max regulatory */
159 if (c->ic_maxpower == 0)
160 c->ic_maxpower = 2*c->ic_maxregpower;
161 setbit(ic->ic_chan_avail, c->ic_ieee);
163 * Identify mode capabilities.
165 if (IEEE80211_IS_CHAN_A(c))
166 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
167 if (IEEE80211_IS_CHAN_B(c))
168 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
169 if (IEEE80211_IS_CHAN_ANYG(c))
170 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
171 if (IEEE80211_IS_CHAN_FHSS(c))
172 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
173 if (IEEE80211_IS_CHAN_108A(c))
174 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
175 if (IEEE80211_IS_CHAN_108G(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
177 if (IEEE80211_IS_CHAN_ST(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
179 if (IEEE80211_IS_CHAN_HALF(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
181 if (IEEE80211_IS_CHAN_QUARTER(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
183 if (IEEE80211_IS_CHAN_HTA(c))
184 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
185 if (IEEE80211_IS_CHAN_HTG(c))
186 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
188 /* initialize candidate channels to all available */
189 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
190 sizeof(ic->ic_chan_avail));
192 /* sort channel table to allow lookup optimizations */
193 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
195 /* invalidate any previous state */
196 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
197 ic->ic_prevchan = NULL;
198 ic->ic_csa_newchan = NULL;
199 /* arbitrarily pick the first channel */
200 ic->ic_curchan = &ic->ic_channels[0];
201 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
203 /* fillin well-known rate sets if driver has not specified */
204 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
205 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
206 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
207 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
208 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
209 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
210 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
211 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
212 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
213 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
216 * Setup required information to fill the mcsset field, if driver did
217 * not. Assume a 2T2R setup for historic reasons.
219 if (ic->ic_rxstream == 0)
221 if (ic->ic_txstream == 0)
225 * Set auto mode to reset active channel state and any desired channel.
227 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
232 null_update_mcast(struct ifnet *ifp)
234 if_printf(ifp, "need multicast update callback\n");
238 null_update_promisc(struct ifnet *ifp)
240 if_printf(ifp, "need promiscuous mode update callback\n");
244 null_transmit(struct ifnet *ifp, struct mbuf *m)
247 IFNET_STAT_INC(ifp, oerrors, 1);
248 return EACCES; /* XXX EIO/EPERM? */
251 #if defined(__DragonFly__)
253 null_output(struct ifnet *ifp, struct mbuf *m,
254 struct sockaddr *dst, struct rtentry *ro)
255 #elif __FreeBSD_version >= 1000031
257 null_output(struct ifnet *ifp, struct mbuf *m,
258 const struct sockaddr *dst, struct route *ro)
261 null_output(struct ifnet *ifp, struct mbuf *m,
262 struct sockaddr *dst, struct route *ro)
265 if_printf(ifp, "discard raw packet\n");
266 return null_transmit(ifp, m);
269 #if defined(__DragonFly__)
272 null_input(struct ifnet *ifp, struct mbuf *m,
273 const struct pktinfo *pi, int cpuid)
275 if_printf(ifp, "if_input should not be called\n");
282 null_input(struct ifnet *ifp, struct mbuf *m)
284 if_printf(ifp, "if_input should not be called\n");
291 null_update_chw(struct ieee80211com *ic)
294 if_printf(ic->ic_ifp, "%s: need callback\n", __func__);
298 * Attach/setup the common net80211 state. Called by
299 * the driver on attach to prior to creating any vap's.
302 ieee80211_ifattach(struct ieee80211com *ic,
303 const uint8_t macaddr[IEEE80211_ADDR_LEN])
305 struct ifnet *ifp = ic->ic_ifp;
306 struct sockaddr_dl *sdl;
309 KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
311 IEEE80211_LOCK_INIT(ic, ifp->if_xname);
312 IEEE80211_TX_LOCK_INIT(ic, ifp->if_xname);
313 TAILQ_INIT(&ic->ic_vaps);
315 /* Create a taskqueue for all state changes */
316 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
317 taskqueue_thread_enqueue, &ic->ic_tq);
318 #if defined(__DragonFly__)
319 taskqueue_start_threads(&ic->ic_tq, 1, TDPRI_KERN_DAEMON, -1,
320 "%s net80211 taskq", ifp->if_xname);
322 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
326 * Fill in 802.11 available channel set, mark all
327 * available channels as active, and pick a default
328 * channel if not already specified.
330 ieee80211_media_init(ic);
332 ic->ic_update_mcast = null_update_mcast;
333 ic->ic_update_promisc = null_update_promisc;
334 ic->ic_update_chw = null_update_chw;
336 ic->ic_hash_key = arc4random();
337 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
338 ic->ic_lintval = ic->ic_bintval;
339 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
341 ieee80211_crypto_attach(ic);
342 ieee80211_node_attach(ic);
343 ieee80211_power_attach(ic);
344 ieee80211_proto_attach(ic);
345 #ifdef IEEE80211_SUPPORT_SUPERG
346 ieee80211_superg_attach(ic);
348 ieee80211_ht_attach(ic);
349 ieee80211_scan_attach(ic);
350 ieee80211_regdomain_attach(ic);
351 ieee80211_dfs_attach(ic);
353 ieee80211_sysctl_attach(ic);
355 ifp->if_addrlen = IEEE80211_ADDR_LEN;
359 * If driver does not configure # of mbuf clusters/jclusters
360 * that could sit on the device queues for quite some time,
362 * - The device queues only consume mbuf clusters.
363 * - No more than ieee80211_nmbclusters_default (by default
364 * 128) mbuf clusters will sit on the device queues for
367 if (ifp->if_nmbclusters <= 0 && ifp->if_nmbjclusters <= 0) {
368 if (ieee80211_nmbclusters_default <
369 IEEE80211_NMBCLUSTERS_DEFMIN) {
370 kprintf("ieee80211 nmbclusters %d -> %d\n",
371 ieee80211_nmbclusters_default,
372 IEEE80211_NMBCLUSTERS_DEFAULT);
373 ieee80211_nmbclusters_default =
374 IEEE80211_NMBCLUSTERS_DEFAULT;
376 ifp->if_nmbclusters = ieee80211_nmbclusters_default;
382 * This function must _not_ be serialized by the WLAN serializer,
383 * since it could dead-lock the domsg to netisrs in if_attach().
385 wlan_serialize_exit();
386 #if defined(__DragonFly__)
387 if_attach(ifp, &wlan_global_serializer);
391 wlan_serialize_enter();
393 ifp->if_mtu = IEEE80211_MTU_MAX;
394 ifp->if_broadcastaddr = ieee80211broadcastaddr;
395 ifp->if_output = null_output;
396 ifp->if_input = null_input; /* just in case */
397 ifp->if_resolvemulti = NULL; /* NB: callers check */
399 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
400 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
401 sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */
402 sdl->sdl_alen = IEEE80211_ADDR_LEN;
403 IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
409 * Detach net80211 state on device detach. Tear down
410 * all vap's and reclaim all common state prior to the
411 * device state going away. Note we may call back into
412 * driver; it must be prepared for this.
415 ieee80211_ifdetach(struct ieee80211com *ic)
417 struct ifnet *ifp = ic->ic_ifp;
418 struct ieee80211vap *vap;
421 * The VAP is responsible for setting and clearing
422 * the VIMAGE context.
424 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
425 ieee80211_vap_destroy(vap);
428 * WLAN serializer must _not_ be held for if_detach(),
429 * since it could dead-lock the domsg to netisrs.
432 * This function actually should _not_ be serialized
433 * by the WLAN serializer, however, all 802.11 device
434 * drivers serialize it ...
436 wlan_serialize_exit();
439 * This detaches the main interface, but not the vaps.
440 * Each VAP may be in a separate VIMAGE.
442 * Detach the main interface _after_ all vaps are
443 * destroyed, since the main interface is referenced
444 * on vaps' detach path.
446 CURVNET_SET(ifp->if_vnet);
450 /* Re-hold WLAN serializer */
451 wlan_serialize_enter();
453 ieee80211_waitfor_parent(ic);
455 ieee80211_sysctl_detach(ic);
456 ieee80211_dfs_detach(ic);
457 ieee80211_regdomain_detach(ic);
458 ieee80211_scan_detach(ic);
459 #ifdef IEEE80211_SUPPORT_SUPERG
460 ieee80211_superg_detach(ic);
462 ieee80211_ht_detach(ic);
463 /* NB: must be called before ieee80211_node_detach */
464 ieee80211_proto_detach(ic);
465 ieee80211_crypto_detach(ic);
466 ieee80211_power_detach(ic);
467 ieee80211_node_detach(ic);
469 /* XXX VNET needed? */
470 ifmedia_removeall(&ic->ic_media);
472 taskqueue_free(ic->ic_tq);
473 IEEE80211_TX_LOCK_DESTROY(ic);
474 IEEE80211_LOCK_DESTROY(ic);
478 * Default reset method for use with the ioctl support. This
479 * method is invoked after any state change in the 802.11
480 * layer that should be propagated to the hardware but not
481 * require re-initialization of the 802.11 state machine (e.g
482 * rescanning for an ap). We always return ENETRESET which
483 * should cause the driver to re-initialize the device. Drivers
484 * can override this method to implement more optimized support.
487 default_reset(struct ieee80211vap *vap, u_long cmd)
493 * Prepare a vap for use. Drivers use this call to
494 * setup net80211 state in new vap's prior attaching
495 * them with ieee80211_vap_attach (below).
498 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
499 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
500 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
501 const uint8_t macaddr[IEEE80211_ADDR_LEN])
505 ifp = if_alloc(IFT_ETHER);
507 if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
511 if_initname(ifp, name, unit);
512 ifp->if_softc = vap; /* back pointer */
513 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
514 ifp->if_start = ieee80211_vap_start;
516 ifp->if_transmit = ieee80211_vap_transmit;
517 ifp->if_qflush = ieee80211_vap_qflush;
519 ifp->if_ioctl = ieee80211_ioctl;
520 ifp->if_init = ieee80211_init;
524 vap->iv_flags = ic->ic_flags; /* propagate common flags */
525 vap->iv_flags_ext = ic->ic_flags_ext;
526 vap->iv_flags_ven = ic->ic_flags_ven;
527 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
528 vap->iv_htcaps = ic->ic_htcaps;
529 vap->iv_htextcaps = ic->ic_htextcaps;
530 vap->iv_opmode = opmode;
531 vap->iv_caps |= ieee80211_opcap[opmode];
533 case IEEE80211_M_WDS:
535 * WDS links must specify the bssid of the far end.
536 * For legacy operation this is a static relationship.
537 * For non-legacy operation the station must associate
538 * and be authorized to pass traffic. Plumbing the
539 * vap to the proper node happens when the vap
540 * transitions to RUN state.
542 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
543 vap->iv_flags |= IEEE80211_F_DESBSSID;
544 if (flags & IEEE80211_CLONE_WDSLEGACY)
545 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
547 #ifdef IEEE80211_SUPPORT_TDMA
548 case IEEE80211_M_AHDEMO:
549 if (flags & IEEE80211_CLONE_TDMA) {
550 /* NB: checked before clone operation allowed */
551 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
552 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
554 * Propagate TDMA capability to mark vap; this
555 * cannot be removed and is used to distinguish
556 * regular ahdemo operation from ahdemo+tdma.
558 vap->iv_caps |= IEEE80211_C_TDMA;
565 /* auto-enable s/w beacon miss support */
566 if (flags & IEEE80211_CLONE_NOBEACONS)
567 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
568 /* auto-generated or user supplied MAC address */
569 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
570 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
572 * Enable various functionality by default if we're
573 * capable; the driver can override us if it knows better.
575 if (vap->iv_caps & IEEE80211_C_WME)
576 vap->iv_flags |= IEEE80211_F_WME;
577 if (vap->iv_caps & IEEE80211_C_BURST)
578 vap->iv_flags |= IEEE80211_F_BURST;
579 /* NB: bg scanning only makes sense for station mode right now */
582 * DISABLE BGSCAN BY DEFAULT, many issues can crop up including
583 * the link going dead.
585 if (vap->iv_opmode == IEEE80211_M_STA &&
586 (vap->iv_caps & IEEE80211_C_BGSCAN))
587 vap->iv_flags |= IEEE80211_F_BGSCAN;
589 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
590 /* NB: DFS support only makes sense for ap mode right now */
591 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
592 (vap->iv_caps & IEEE80211_C_DFS))
593 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
595 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
596 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
597 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
599 * Install a default reset method for the ioctl support;
600 * the driver can override this.
602 vap->iv_reset = default_reset;
604 IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
606 ieee80211_sysctl_vattach(vap);
607 ieee80211_crypto_vattach(vap);
608 ieee80211_node_vattach(vap);
609 ieee80211_power_vattach(vap);
610 ieee80211_proto_vattach(vap);
611 #ifdef IEEE80211_SUPPORT_SUPERG
612 ieee80211_superg_vattach(vap);
614 ieee80211_ht_vattach(vap);
615 ieee80211_scan_vattach(vap);
616 ieee80211_regdomain_vattach(vap);
617 ieee80211_radiotap_vattach(vap);
618 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
624 * Activate a vap. State should have been prepared with a
625 * call to ieee80211_vap_setup and by the driver. On return
626 * from this call the vap is ready for use.
629 ieee80211_vap_attach(struct ieee80211vap *vap,
630 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
632 struct ifnet *ifp = vap->iv_ifp;
633 struct ieee80211com *ic = vap->iv_ic;
634 struct ifmediareq imr;
638 * This function must _not_ be serialized by the WLAN serializer,
639 * since it could dead-lock the domsg to netisrs in ether_ifattach().
641 wlan_assert_notserialized();
643 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
644 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
645 __func__, ieee80211_opmode_name[vap->iv_opmode],
646 ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
649 * Do late attach work that cannot happen until after
650 * the driver has had a chance to override defaults.
652 ieee80211_node_latevattach(vap);
653 ieee80211_power_latevattach(vap);
655 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
656 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
657 ieee80211_media_status(ifp, &imr);
658 /* NB: strip explicit mode; we're actually in autoselect */
659 ifmedia_set(&vap->iv_media,
660 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
662 ifp->if_baudrate = IF_Mbps(maxrate);
664 #if defined(__DragonFly__)
665 ether_ifattach(ifp, vap->iv_myaddr, &wlan_global_serializer);
667 ether_ifattach(ifp, vap->iv_myaddr);
669 /* hook output method setup by ether_ifattach */
670 vap->iv_output = ifp->if_output;
671 ifp->if_output = ieee80211_output;
672 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
675 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
676 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
677 #ifdef IEEE80211_SUPPORT_SUPERG
678 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
680 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
681 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
682 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
683 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
684 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
685 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
686 IEEE80211_UNLOCK(ic);
692 * Tear down vap state and reclaim the ifnet.
693 * The driver is assumed to have prepared for
694 * this; e.g. by turning off interrupts for the
698 ieee80211_vap_detach(struct ieee80211vap *vap)
700 struct ieee80211com *ic = vap->iv_ic;
701 struct ifnet *ifp = vap->iv_ifp;
704 * This function must _not_ be serialized by the WLAN serializer,
705 * since it could dead-lock the domsg to netisrs in ether_ifdettach().
707 wlan_assert_notserialized();
709 CURVNET_SET(ifp->if_vnet);
711 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
712 __func__, ieee80211_opmode_name[vap->iv_opmode],
713 ic->ic_ifp->if_xname);
715 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
721 * Flush any deferred vap tasks.
723 ieee80211_draintask(ic, &vap->iv_nstate_task);
724 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
726 #if !defined(__DragonFly__)
727 /* XXX band-aid until ifnet handles this for us */
728 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
732 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
733 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
734 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
735 #ifdef IEEE80211_SUPPORT_SUPERG
736 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
738 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
739 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
740 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
741 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
742 /* NB: this handles the bpfdetach done below */
743 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
744 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
745 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
746 IEEE80211_UNLOCK(ic);
748 ifmedia_removeall(&vap->iv_media);
750 ieee80211_radiotap_vdetach(vap);
751 ieee80211_regdomain_vdetach(vap);
752 ieee80211_scan_vdetach(vap);
753 #ifdef IEEE80211_SUPPORT_SUPERG
754 ieee80211_superg_vdetach(vap);
756 ieee80211_ht_vdetach(vap);
757 /* NB: must be before ieee80211_node_vdetach */
758 ieee80211_proto_vdetach(vap);
759 ieee80211_crypto_vdetach(vap);
760 ieee80211_power_vdetach(vap);
761 ieee80211_node_vdetach(vap);
762 ieee80211_sysctl_vdetach(vap);
770 * Synchronize flag bit state in the parent ifnet structure
771 * according to the state of all vap ifnet's. This is used,
772 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
775 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
777 struct ifnet *ifp = ic->ic_ifp;
778 struct ieee80211vap *vap;
781 IEEE80211_LOCK_ASSERT(ic);
784 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
785 if (vap->iv_ifp->if_flags & flag) {
787 * XXX the bridge sets PROMISC but we don't want to
788 * enable it on the device, discard here so all the
789 * drivers don't need to special-case it
791 if (flag == IFF_PROMISC &&
792 !(vap->iv_opmode == IEEE80211_M_MONITOR ||
793 (vap->iv_opmode == IEEE80211_M_AHDEMO &&
794 (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
799 oflags = ifp->if_flags;
801 ifp->if_flags |= flag;
803 ifp->if_flags &= ~flag;
804 if ((ifp->if_flags ^ oflags) & flag) {
805 /* XXX should we return 1/0 and let caller do this? */
806 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
807 if (flag == IFF_PROMISC)
808 ieee80211_runtask(ic, &ic->ic_promisc_task);
809 else if (flag == IFF_ALLMULTI)
810 ieee80211_runtask(ic, &ic->ic_mcast_task);
816 * Synchronize flag bit state in the com structure
817 * according to the state of all vap's. This is used,
818 * for example, to handle state changes via ioctls.
821 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
823 struct ieee80211vap *vap;
826 IEEE80211_LOCK_ASSERT(ic);
829 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
830 if (vap->iv_flags & flag) {
835 ic->ic_flags |= flag;
837 ic->ic_flags &= ~flag;
841 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
843 struct ieee80211com *ic = vap->iv_ic;
848 vap->iv_flags &= ~flag;
850 vap->iv_flags |= flag;
851 ieee80211_syncflag_locked(ic, flag);
852 IEEE80211_UNLOCK(ic);
856 * Synchronize flags_ht bit state in the com structure
857 * according to the state of all vap's. This is used,
858 * for example, to handle state changes via ioctls.
861 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
863 struct ieee80211vap *vap;
866 IEEE80211_LOCK_ASSERT(ic);
869 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
870 if (vap->iv_flags_ht & flag) {
875 ic->ic_flags_ht |= flag;
877 ic->ic_flags_ht &= ~flag;
881 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
883 struct ieee80211com *ic = vap->iv_ic;
888 vap->iv_flags_ht &= ~flag;
890 vap->iv_flags_ht |= flag;
891 ieee80211_syncflag_ht_locked(ic, flag);
892 IEEE80211_UNLOCK(ic);
896 * Synchronize flags_ext bit state in the com structure
897 * according to the state of all vap's. This is used,
898 * for example, to handle state changes via ioctls.
901 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
903 struct ieee80211vap *vap;
906 IEEE80211_LOCK_ASSERT(ic);
909 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
910 if (vap->iv_flags_ext & flag) {
915 ic->ic_flags_ext |= flag;
917 ic->ic_flags_ext &= ~flag;
921 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
923 struct ieee80211com *ic = vap->iv_ic;
928 vap->iv_flags_ext &= ~flag;
930 vap->iv_flags_ext |= flag;
931 ieee80211_syncflag_ext_locked(ic, flag);
932 IEEE80211_UNLOCK(ic);
936 mapgsm(u_int freq, u_int flags)
939 if (flags & IEEE80211_CHAN_QUARTER)
941 else if (flags & IEEE80211_CHAN_HALF)
945 /* NB: there is no 907/20 wide but leave room */
946 return (freq - 906*10) / 5;
950 mappsb(u_int freq, u_int flags)
952 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
956 * Convert MHz frequency to IEEE channel number.
959 ieee80211_mhz2ieee(u_int freq, u_int flags)
961 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
962 if (flags & IEEE80211_CHAN_GSM)
963 return mapgsm(freq, flags);
964 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
968 return ((int) freq - 2407) / 5;
970 return 15 + ((freq - 2512) / 20);
971 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
973 /* XXX check regdomain? */
974 if (IS_FREQ_IN_PSB(freq))
975 return mappsb(freq, flags);
976 return (freq - 4000) / 5;
978 return (freq - 5000) / 5;
979 } else { /* either, guess */
983 if (907 <= freq && freq <= 922)
984 return mapgsm(freq, flags);
985 return ((int) freq - 2407) / 5;
988 if (IS_FREQ_IN_PSB(freq))
989 return mappsb(freq, flags);
990 else if (freq > 4900)
991 return (freq - 4000) / 5;
993 return 15 + ((freq - 2512) / 20);
995 return (freq - 5000) / 5;
997 #undef IS_FREQ_IN_PSB
1001 * Convert channel to IEEE channel number.
1004 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1007 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
1010 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
1014 * Convert IEEE channel number to MHz frequency.
1017 ieee80211_ieee2mhz(u_int chan, u_int flags)
1019 if (flags & IEEE80211_CHAN_GSM)
1020 return 907 + 5 * (chan / 10);
1021 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1025 return 2407 + chan*5;
1027 return 2512 + ((chan-15)*20);
1028 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1029 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1031 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1033 return 5000 + (chan*5);
1034 } else { /* either, guess */
1035 /* XXX can't distinguish PSB+GSM channels */
1038 if (chan < 14) /* 0-13 */
1039 return 2407 + chan*5;
1040 if (chan < 27) /* 15-26 */
1041 return 2512 + ((chan-15)*20);
1042 return 5000 + (chan*5);
1047 * Locate a channel given a frequency+flags. We cache
1048 * the previous lookup to optimize switching between two
1049 * channels--as happens with dynamic turbo.
1051 struct ieee80211_channel *
1052 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1054 struct ieee80211_channel *c;
1057 flags &= IEEE80211_CHAN_ALLTURBO;
1058 c = ic->ic_prevchan;
1059 if (c != NULL && c->ic_freq == freq &&
1060 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1062 /* brute force search */
1063 for (i = 0; i < ic->ic_nchans; i++) {
1064 c = &ic->ic_channels[i];
1065 if (c->ic_freq == freq &&
1066 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1073 * Locate a channel given a channel number+flags. We cache
1074 * the previous lookup to optimize switching between two
1075 * channels--as happens with dynamic turbo.
1077 struct ieee80211_channel *
1078 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1080 struct ieee80211_channel *c;
1083 flags &= IEEE80211_CHAN_ALLTURBO;
1084 c = ic->ic_prevchan;
1085 if (c != NULL && c->ic_ieee == ieee &&
1086 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1088 /* brute force search */
1089 for (i = 0; i < ic->ic_nchans; i++) {
1090 c = &ic->ic_channels[i];
1091 if (c->ic_ieee == ieee &&
1092 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1099 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1101 #define ADD(_ic, _s, _o) \
1102 ifmedia_add(media, \
1103 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1104 static const u_int mopts[IEEE80211_MODE_MAX] = {
1105 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1106 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1107 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1108 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1109 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1110 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1111 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1112 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1113 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1114 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1115 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1116 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1122 ADD(ic, mword, mopt); /* STA mode has no cap */
1123 if (caps & IEEE80211_C_IBSS)
1124 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1125 if (caps & IEEE80211_C_HOSTAP)
1126 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1127 if (caps & IEEE80211_C_AHDEMO)
1128 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1129 if (caps & IEEE80211_C_MONITOR)
1130 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1131 if (caps & IEEE80211_C_WDS)
1132 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1133 if (caps & IEEE80211_C_MBSS)
1134 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1139 * Setup the media data structures according to the channel and
1143 ieee80211_media_setup(struct ieee80211com *ic,
1144 struct ifmedia *media, int caps, int addsta,
1145 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1147 int i, j, rate, maxrate, mword, r;
1148 enum ieee80211_phymode mode;
1149 const struct ieee80211_rateset *rs;
1150 struct ieee80211_rateset allrates;
1153 * Fill in media characteristics.
1155 ifmedia_init(media, 0, media_change, media_stat);
1158 * Add media for legacy operating modes.
1160 memset(&allrates, 0, sizeof(allrates));
1161 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1162 if (isclr(ic->ic_modecaps, mode))
1164 addmedia(media, caps, addsta, mode, IFM_AUTO);
1165 if (mode == IEEE80211_MODE_AUTO)
1167 rs = &ic->ic_sup_rates[mode];
1168 for (i = 0; i < rs->rs_nrates; i++) {
1169 rate = rs->rs_rates[i];
1170 mword = ieee80211_rate2media(ic, rate, mode);
1173 addmedia(media, caps, addsta, mode, mword);
1175 * Add legacy rate to the collection of all rates.
1177 r = rate & IEEE80211_RATE_VAL;
1178 for (j = 0; j < allrates.rs_nrates; j++)
1179 if (allrates.rs_rates[j] == r)
1181 if (j == allrates.rs_nrates) {
1182 /* unique, add to the set */
1183 allrates.rs_rates[j] = r;
1184 allrates.rs_nrates++;
1186 rate = (rate & IEEE80211_RATE_VAL) / 2;
1191 for (i = 0; i < allrates.rs_nrates; i++) {
1192 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1193 IEEE80211_MODE_AUTO);
1196 /* NB: remove media options from mword */
1197 addmedia(media, caps, addsta,
1198 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1201 * Add HT/11n media. Note that we do not have enough
1202 * bits in the media subtype to express the MCS so we
1203 * use a "placeholder" media subtype and any fixed MCS
1204 * must be specified with a different mechanism.
1206 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1207 if (isclr(ic->ic_modecaps, mode))
1209 addmedia(media, caps, addsta, mode, IFM_AUTO);
1210 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1212 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1213 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1214 addmedia(media, caps, addsta,
1215 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1216 i = ic->ic_txstream * 8 - 1;
1217 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1218 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1219 rate = ieee80211_htrates[i].ht40_rate_400ns;
1220 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1221 rate = ieee80211_htrates[i].ht40_rate_800ns;
1222 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1223 rate = ieee80211_htrates[i].ht20_rate_400ns;
1225 rate = ieee80211_htrates[i].ht20_rate_800ns;
1233 ieee80211_media_init(struct ieee80211com *ic)
1235 struct ifnet *ifp = ic->ic_ifp;
1238 /* NB: this works because the structure is initialized to zero */
1239 if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1241 * We are re-initializing the channel list; clear
1242 * the existing media state as the media routines
1243 * don't suppress duplicates.
1245 ifmedia_removeall(&ic->ic_media);
1247 ieee80211_chan_init(ic);
1250 * Recalculate media settings in case new channel list changes
1251 * the set of available modes.
1253 maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1254 ieee80211com_media_change, ieee80211com_media_status);
1255 /* NB: strip explicit mode; we're actually in autoselect */
1256 ifmedia_set(&ic->ic_media,
1257 media_status(ic->ic_opmode, ic->ic_curchan) &~
1258 (IFM_MMASK | IFM_IEEE80211_TURBO));
1260 ifp->if_baudrate = IF_Mbps(maxrate);
1262 /* XXX need to propagate new media settings to vap's */
1265 /* XXX inline or eliminate? */
1266 const struct ieee80211_rateset *
1267 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1269 /* XXX does this work for 11ng basic rates? */
1270 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1274 ieee80211_announce(struct ieee80211com *ic)
1276 struct ifnet *ifp = ic->ic_ifp;
1278 enum ieee80211_phymode mode;
1279 const struct ieee80211_rateset *rs;
1281 /* NB: skip AUTO since it has no rates */
1282 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1283 if (isclr(ic->ic_modecaps, mode))
1285 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1286 rs = &ic->ic_sup_rates[mode];
1287 for (i = 0; i < rs->rs_nrates; i++) {
1288 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1291 rate = ieee80211_media2rate(mword);
1292 kprintf("%s%d%sMbps", (i != 0 ? " " : ""),
1293 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1297 ieee80211_ht_announce(ic);
1301 ieee80211_announce_channels(struct ieee80211com *ic)
1303 const struct ieee80211_channel *c;
1307 kprintf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1308 for (i = 0; i < ic->ic_nchans; i++) {
1309 c = &ic->ic_channels[i];
1310 if (IEEE80211_IS_CHAN_ST(c))
1312 else if (IEEE80211_IS_CHAN_108A(c))
1314 else if (IEEE80211_IS_CHAN_108G(c))
1316 else if (IEEE80211_IS_CHAN_HT(c))
1318 else if (IEEE80211_IS_CHAN_A(c))
1320 else if (IEEE80211_IS_CHAN_ANYG(c))
1322 else if (IEEE80211_IS_CHAN_B(c))
1326 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1328 else if (IEEE80211_IS_CHAN_HALF(c))
1330 else if (IEEE80211_IS_CHAN_QUARTER(c))
1334 kprintf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1335 , c->ic_ieee, c->ic_freq, type
1337 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1338 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1340 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1341 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1347 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1349 switch (IFM_MODE(ime->ifm_media)) {
1350 case IFM_IEEE80211_11A:
1351 *mode = IEEE80211_MODE_11A;
1353 case IFM_IEEE80211_11B:
1354 *mode = IEEE80211_MODE_11B;
1356 case IFM_IEEE80211_11G:
1357 *mode = IEEE80211_MODE_11G;
1359 case IFM_IEEE80211_FH:
1360 *mode = IEEE80211_MODE_FH;
1362 case IFM_IEEE80211_11NA:
1363 *mode = IEEE80211_MODE_11NA;
1365 case IFM_IEEE80211_11NG:
1366 *mode = IEEE80211_MODE_11NG;
1369 *mode = IEEE80211_MODE_AUTO;
1375 * Turbo mode is an ``option''.
1376 * XXX does not apply to AUTO
1378 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1379 if (*mode == IEEE80211_MODE_11A) {
1380 if (flags & IEEE80211_F_TURBOP)
1381 *mode = IEEE80211_MODE_TURBO_A;
1383 *mode = IEEE80211_MODE_STURBO_A;
1384 } else if (*mode == IEEE80211_MODE_11G)
1385 *mode = IEEE80211_MODE_TURBO_G;
1394 * Handle a media change request on the underlying interface.
1397 ieee80211com_media_change(struct ifnet *ifp)
1403 * Handle a media change request on the vap interface.
1406 ieee80211_media_change(struct ifnet *ifp)
1408 struct ieee80211vap *vap = ifp->if_softc;
1409 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1412 if (!media2mode(ime, vap->iv_flags, &newmode))
1414 if (vap->iv_des_mode != newmode) {
1415 vap->iv_des_mode = newmode;
1416 /* XXX kick state machine if up+running */
1422 * Common code to calculate the media status word
1423 * from the operating mode and channel state.
1426 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1430 status = IFM_IEEE80211;
1432 case IEEE80211_M_STA:
1434 case IEEE80211_M_IBSS:
1435 status |= IFM_IEEE80211_ADHOC;
1437 case IEEE80211_M_HOSTAP:
1438 status |= IFM_IEEE80211_HOSTAP;
1440 case IEEE80211_M_MONITOR:
1441 status |= IFM_IEEE80211_MONITOR;
1443 case IEEE80211_M_AHDEMO:
1444 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1446 case IEEE80211_M_WDS:
1447 status |= IFM_IEEE80211_WDS;
1449 case IEEE80211_M_MBSS:
1450 status |= IFM_IEEE80211_MBSS;
1453 if (IEEE80211_IS_CHAN_HTA(chan)) {
1454 status |= IFM_IEEE80211_11NA;
1455 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1456 status |= IFM_IEEE80211_11NG;
1457 } else if (IEEE80211_IS_CHAN_A(chan)) {
1458 status |= IFM_IEEE80211_11A;
1459 } else if (IEEE80211_IS_CHAN_B(chan)) {
1460 status |= IFM_IEEE80211_11B;
1461 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1462 status |= IFM_IEEE80211_11G;
1463 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1464 status |= IFM_IEEE80211_FH;
1466 /* XXX else complain? */
1468 if (IEEE80211_IS_CHAN_TURBO(chan))
1469 status |= IFM_IEEE80211_TURBO;
1471 if (IEEE80211_IS_CHAN_HT20(chan))
1472 status |= IFM_IEEE80211_HT20;
1473 if (IEEE80211_IS_CHAN_HT40(chan))
1474 status |= IFM_IEEE80211_HT40;
1480 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1482 struct ieee80211com *ic = ifp->if_l2com;
1483 struct ieee80211vap *vap;
1485 imr->ifm_status = IFM_AVALID;
1486 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1487 if (vap->iv_ifp->if_flags & IFF_UP) {
1488 imr->ifm_status |= IFM_ACTIVE;
1491 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1492 if (imr->ifm_status & IFM_ACTIVE)
1493 imr->ifm_current = imr->ifm_active;
1497 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1499 struct ieee80211vap *vap = ifp->if_softc;
1500 struct ieee80211com *ic = vap->iv_ic;
1501 enum ieee80211_phymode mode;
1503 imr->ifm_status = IFM_AVALID;
1505 * NB: use the current channel's mode to lock down a xmit
1506 * rate only when running; otherwise we may have a mismatch
1507 * in which case the rate will not be convertible.
1509 if (vap->iv_state == IEEE80211_S_RUN ||
1510 vap->iv_state == IEEE80211_S_SLEEP) {
1511 imr->ifm_status |= IFM_ACTIVE;
1512 mode = ieee80211_chan2mode(ic->ic_curchan);
1514 mode = IEEE80211_MODE_AUTO;
1515 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1517 * Calculate a current rate if possible.
1519 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1521 * A fixed rate is set, report that.
1523 imr->ifm_active |= ieee80211_rate2media(ic,
1524 vap->iv_txparms[mode].ucastrate, mode);
1525 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1527 * In station mode report the current transmit rate.
1529 imr->ifm_active |= ieee80211_rate2media(ic,
1530 vap->iv_bss->ni_txrate, mode);
1532 imr->ifm_active |= IFM_AUTO;
1533 if (imr->ifm_status & IFM_ACTIVE)
1534 imr->ifm_current = imr->ifm_active;
1538 * Set the current phy mode and recalculate the active channel
1539 * set based on the available channels for this mode. Also
1540 * select a new default/current channel if the current one is
1541 * inappropriate for this mode.
1544 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1547 * Adjust basic rates in 11b/11g supported rate set.
1548 * Note that if operating on a hal/quarter rate channel
1549 * this is a noop as those rates sets are different
1552 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1553 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1555 ic->ic_curmode = mode;
1556 ieee80211_reset_erp(ic); /* reset ERP state */
1562 * Return the phy mode for with the specified channel.
1564 enum ieee80211_phymode
1565 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1568 if (IEEE80211_IS_CHAN_HTA(chan))
1569 return IEEE80211_MODE_11NA;
1570 else if (IEEE80211_IS_CHAN_HTG(chan))
1571 return IEEE80211_MODE_11NG;
1572 else if (IEEE80211_IS_CHAN_108G(chan))
1573 return IEEE80211_MODE_TURBO_G;
1574 else if (IEEE80211_IS_CHAN_ST(chan))
1575 return IEEE80211_MODE_STURBO_A;
1576 else if (IEEE80211_IS_CHAN_TURBO(chan))
1577 return IEEE80211_MODE_TURBO_A;
1578 else if (IEEE80211_IS_CHAN_HALF(chan))
1579 return IEEE80211_MODE_HALF;
1580 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1581 return IEEE80211_MODE_QUARTER;
1582 else if (IEEE80211_IS_CHAN_A(chan))
1583 return IEEE80211_MODE_11A;
1584 else if (IEEE80211_IS_CHAN_ANYG(chan))
1585 return IEEE80211_MODE_11G;
1586 else if (IEEE80211_IS_CHAN_B(chan))
1587 return IEEE80211_MODE_11B;
1588 else if (IEEE80211_IS_CHAN_FHSS(chan))
1589 return IEEE80211_MODE_FH;
1591 /* NB: should not get here */
1592 kprintf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1593 __func__, chan->ic_freq, chan->ic_flags);
1594 return IEEE80211_MODE_11B;
1598 u_int match; /* rate + mode */
1599 u_int media; /* if_media rate */
1603 findmedia(const struct ratemedia rates[], int n, u_int match)
1607 for (i = 0; i < n; i++)
1608 if (rates[i].match == match)
1609 return rates[i].media;
1614 * Convert IEEE80211 rate value to ifmedia subtype.
1615 * Rate is either a legacy rate in units of 0.5Mbps
1619 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1621 static const struct ratemedia rates[] = {
1622 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1623 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1624 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1625 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1626 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1627 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1628 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1629 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1630 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1631 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1632 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1633 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1634 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1635 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1636 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1637 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1638 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1639 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1640 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1641 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1642 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1643 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1644 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1645 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1646 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1647 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1648 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1649 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1650 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1651 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1652 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1654 static const struct ratemedia htrates[] = {
1655 { 0, IFM_IEEE80211_MCS },
1656 { 1, IFM_IEEE80211_MCS },
1657 { 2, IFM_IEEE80211_MCS },
1658 { 3, IFM_IEEE80211_MCS },
1659 { 4, IFM_IEEE80211_MCS },
1660 { 5, IFM_IEEE80211_MCS },
1661 { 6, IFM_IEEE80211_MCS },
1662 { 7, IFM_IEEE80211_MCS },
1663 { 8, IFM_IEEE80211_MCS },
1664 { 9, IFM_IEEE80211_MCS },
1665 { 10, IFM_IEEE80211_MCS },
1666 { 11, IFM_IEEE80211_MCS },
1667 { 12, IFM_IEEE80211_MCS },
1668 { 13, IFM_IEEE80211_MCS },
1669 { 14, IFM_IEEE80211_MCS },
1670 { 15, IFM_IEEE80211_MCS },
1671 { 16, IFM_IEEE80211_MCS },
1672 { 17, IFM_IEEE80211_MCS },
1673 { 18, IFM_IEEE80211_MCS },
1674 { 19, IFM_IEEE80211_MCS },
1675 { 20, IFM_IEEE80211_MCS },
1676 { 21, IFM_IEEE80211_MCS },
1677 { 22, IFM_IEEE80211_MCS },
1678 { 23, IFM_IEEE80211_MCS },
1679 { 24, IFM_IEEE80211_MCS },
1680 { 25, IFM_IEEE80211_MCS },
1681 { 26, IFM_IEEE80211_MCS },
1682 { 27, IFM_IEEE80211_MCS },
1683 { 28, IFM_IEEE80211_MCS },
1684 { 29, IFM_IEEE80211_MCS },
1685 { 30, IFM_IEEE80211_MCS },
1686 { 31, IFM_IEEE80211_MCS },
1687 { 32, IFM_IEEE80211_MCS },
1688 { 33, IFM_IEEE80211_MCS },
1689 { 34, IFM_IEEE80211_MCS },
1690 { 35, IFM_IEEE80211_MCS },
1691 { 36, IFM_IEEE80211_MCS },
1692 { 37, IFM_IEEE80211_MCS },
1693 { 38, IFM_IEEE80211_MCS },
1694 { 39, IFM_IEEE80211_MCS },
1695 { 40, IFM_IEEE80211_MCS },
1696 { 41, IFM_IEEE80211_MCS },
1697 { 42, IFM_IEEE80211_MCS },
1698 { 43, IFM_IEEE80211_MCS },
1699 { 44, IFM_IEEE80211_MCS },
1700 { 45, IFM_IEEE80211_MCS },
1701 { 46, IFM_IEEE80211_MCS },
1702 { 47, IFM_IEEE80211_MCS },
1703 { 48, IFM_IEEE80211_MCS },
1704 { 49, IFM_IEEE80211_MCS },
1705 { 50, IFM_IEEE80211_MCS },
1706 { 51, IFM_IEEE80211_MCS },
1707 { 52, IFM_IEEE80211_MCS },
1708 { 53, IFM_IEEE80211_MCS },
1709 { 54, IFM_IEEE80211_MCS },
1710 { 55, IFM_IEEE80211_MCS },
1711 { 56, IFM_IEEE80211_MCS },
1712 { 57, IFM_IEEE80211_MCS },
1713 { 58, IFM_IEEE80211_MCS },
1714 { 59, IFM_IEEE80211_MCS },
1715 { 60, IFM_IEEE80211_MCS },
1716 { 61, IFM_IEEE80211_MCS },
1717 { 62, IFM_IEEE80211_MCS },
1718 { 63, IFM_IEEE80211_MCS },
1719 { 64, IFM_IEEE80211_MCS },
1720 { 65, IFM_IEEE80211_MCS },
1721 { 66, IFM_IEEE80211_MCS },
1722 { 67, IFM_IEEE80211_MCS },
1723 { 68, IFM_IEEE80211_MCS },
1724 { 69, IFM_IEEE80211_MCS },
1725 { 70, IFM_IEEE80211_MCS },
1726 { 71, IFM_IEEE80211_MCS },
1727 { 72, IFM_IEEE80211_MCS },
1728 { 73, IFM_IEEE80211_MCS },
1729 { 74, IFM_IEEE80211_MCS },
1730 { 75, IFM_IEEE80211_MCS },
1731 { 76, IFM_IEEE80211_MCS },
1736 * Check 11n rates first for match as an MCS.
1738 if (mode == IEEE80211_MODE_11NA) {
1739 if (rate & IEEE80211_RATE_MCS) {
1740 rate &= ~IEEE80211_RATE_MCS;
1741 m = findmedia(htrates, nitems(htrates), rate);
1743 return m | IFM_IEEE80211_11NA;
1745 } else if (mode == IEEE80211_MODE_11NG) {
1746 /* NB: 12 is ambiguous, it will be treated as an MCS */
1747 if (rate & IEEE80211_RATE_MCS) {
1748 rate &= ~IEEE80211_RATE_MCS;
1749 m = findmedia(htrates, nitems(htrates), rate);
1751 return m | IFM_IEEE80211_11NG;
1754 rate &= IEEE80211_RATE_VAL;
1756 case IEEE80211_MODE_11A:
1757 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1758 case IEEE80211_MODE_QUARTER:
1759 case IEEE80211_MODE_11NA:
1760 case IEEE80211_MODE_TURBO_A:
1761 case IEEE80211_MODE_STURBO_A:
1762 return findmedia(rates, nitems(rates),
1763 rate | IFM_IEEE80211_11A);
1764 case IEEE80211_MODE_11B:
1765 return findmedia(rates, nitems(rates),
1766 rate | IFM_IEEE80211_11B);
1767 case IEEE80211_MODE_FH:
1768 return findmedia(rates, nitems(rates),
1769 rate | IFM_IEEE80211_FH);
1770 case IEEE80211_MODE_AUTO:
1771 /* NB: ic may be NULL for some drivers */
1772 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1773 return findmedia(rates, nitems(rates),
1774 rate | IFM_IEEE80211_FH);
1775 /* NB: hack, 11g matches both 11b+11a rates */
1777 case IEEE80211_MODE_11G:
1778 case IEEE80211_MODE_11NG:
1779 case IEEE80211_MODE_TURBO_G:
1780 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1786 ieee80211_media2rate(int mword)
1788 static const int ieeerates[] = {
1792 2, /* IFM_IEEE80211_FH1 */
1793 4, /* IFM_IEEE80211_FH2 */
1794 2, /* IFM_IEEE80211_DS1 */
1795 4, /* IFM_IEEE80211_DS2 */
1796 11, /* IFM_IEEE80211_DS5 */
1797 22, /* IFM_IEEE80211_DS11 */
1798 44, /* IFM_IEEE80211_DS22 */
1799 12, /* IFM_IEEE80211_OFDM6 */
1800 18, /* IFM_IEEE80211_OFDM9 */
1801 24, /* IFM_IEEE80211_OFDM12 */
1802 36, /* IFM_IEEE80211_OFDM18 */
1803 48, /* IFM_IEEE80211_OFDM24 */
1804 72, /* IFM_IEEE80211_OFDM36 */
1805 96, /* IFM_IEEE80211_OFDM48 */
1806 108, /* IFM_IEEE80211_OFDM54 */
1807 144, /* IFM_IEEE80211_OFDM72 */
1808 0, /* IFM_IEEE80211_DS354k */
1809 0, /* IFM_IEEE80211_DS512k */
1810 6, /* IFM_IEEE80211_OFDM3 */
1811 9, /* IFM_IEEE80211_OFDM4 */
1812 54, /* IFM_IEEE80211_OFDM27 */
1813 -1, /* IFM_IEEE80211_MCS */
1815 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1816 ieeerates[IFM_SUBTYPE(mword)] : 0;
1820 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1821 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1823 #define mix(a, b, c) \
1825 a -= b; a -= c; a ^= (c >> 13); \
1826 b -= c; b -= a; b ^= (a << 8); \
1827 c -= a; c -= b; c ^= (b >> 13); \
1828 a -= b; a -= c; a ^= (c >> 12); \
1829 b -= c; b -= a; b ^= (a << 16); \
1830 c -= a; c -= b; c ^= (b >> 5); \
1831 a -= b; a -= c; a ^= (c >> 3); \
1832 b -= c; b -= a; b ^= (a << 10); \
1833 c -= a; c -= b; c ^= (b >> 15); \
1834 } while (/*CONSTCOND*/0)
1837 ieee80211_mac_hash(const struct ieee80211com *ic,
1838 const uint8_t addr[IEEE80211_ADDR_LEN])
1840 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;