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>
41 #include <machine/stdarg.h>
44 #include <net/if_var.h>
45 #include <net/if_dl.h>
46 #include <net/if_media.h>
47 #include <net/if_types.h>
48 #include <net/ethernet.h>
50 #include <netproto/802_11/ieee80211_var.h>
51 #include <netproto/802_11/ieee80211_regdomain.h>
52 #ifdef IEEE80211_SUPPORT_SUPERG
53 #include <netproto/802_11/ieee80211_superg.h>
55 #include <netproto/802_11/ieee80211_ratectl.h>
59 #define IEEE80211_NMBCLUSTERS_DEFMIN 32
60 #define IEEE80211_NMBCLUSTERS_DEFAULT 128
62 static int ieee80211_nmbclusters_default = IEEE80211_NMBCLUSTERS_DEFAULT;
63 TUNABLE_INT("net.link.ieee80211.nmbclusters", &ieee80211_nmbclusters_default);
65 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
66 [IEEE80211_MODE_AUTO] = "auto",
67 [IEEE80211_MODE_11A] = "11a",
68 [IEEE80211_MODE_11B] = "11b",
69 [IEEE80211_MODE_11G] = "11g",
70 [IEEE80211_MODE_FH] = "FH",
71 [IEEE80211_MODE_TURBO_A] = "turboA",
72 [IEEE80211_MODE_TURBO_G] = "turboG",
73 [IEEE80211_MODE_STURBO_A] = "sturboA",
74 [IEEE80211_MODE_HALF] = "half",
75 [IEEE80211_MODE_QUARTER] = "quarter",
76 [IEEE80211_MODE_11NA] = "11na",
77 [IEEE80211_MODE_11NG] = "11ng",
79 /* map ieee80211_opmode to the corresponding capability bit */
80 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
81 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
82 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
83 [IEEE80211_M_STA] = IEEE80211_C_STA,
84 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
85 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
86 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
87 #ifdef IEEE80211_SUPPORT_MESH
88 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
92 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
93 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
95 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
96 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
97 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
98 static int ieee80211_media_setup(struct ieee80211com *ic,
99 struct ifmedia *media, int caps, int addsta,
100 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
101 static void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
102 static int ieee80211com_media_change(struct ifnet *);
103 static int media_status(enum ieee80211_opmode,
104 const struct ieee80211_channel *);
106 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
109 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
111 #define B(r) ((r) | IEEE80211_RATE_BASIC)
112 static const struct ieee80211_rateset ieee80211_rateset_11a =
113 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
114 static const struct ieee80211_rateset ieee80211_rateset_half =
115 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
116 static const struct ieee80211_rateset ieee80211_rateset_quarter =
117 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
118 static const struct ieee80211_rateset ieee80211_rateset_11b =
119 { 4, { B(2), B(4), B(11), B(22) } };
120 /* NB: OFDM rates are handled specially based on mode */
121 static const struct ieee80211_rateset ieee80211_rateset_11g =
122 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
126 * Fill in 802.11 available channel set, mark
127 * all available channels as active, and pick
128 * a default channel if not already specified.
131 ieee80211_chan_init(struct ieee80211com *ic)
133 #define DEFAULTRATES(m, def) do { \
134 if (ic->ic_sup_rates[m].rs_nrates == 0) \
135 ic->ic_sup_rates[m] = def; \
137 struct ieee80211_channel *c;
140 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
141 ("invalid number of channels specified: %u", ic->ic_nchans));
142 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
143 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
144 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
145 for (i = 0; i < ic->ic_nchans; i++) {
146 c = &ic->ic_channels[i];
147 KASSERT(c->ic_flags != 0, ("channel with no flags"));
149 * Help drivers that work only with frequencies by filling
150 * in IEEE channel #'s if not already calculated. Note this
151 * mimics similar work done in ieee80211_setregdomain when
152 * changing regulatory state.
155 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
156 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
157 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
158 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
160 /* default max tx power to max regulatory */
161 if (c->ic_maxpower == 0)
162 c->ic_maxpower = 2*c->ic_maxregpower;
163 setbit(ic->ic_chan_avail, c->ic_ieee);
165 * Identify mode capabilities.
167 if (IEEE80211_IS_CHAN_A(c))
168 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
169 if (IEEE80211_IS_CHAN_B(c))
170 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
171 if (IEEE80211_IS_CHAN_ANYG(c))
172 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
173 if (IEEE80211_IS_CHAN_FHSS(c))
174 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
175 if (IEEE80211_IS_CHAN_108A(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
177 if (IEEE80211_IS_CHAN_108G(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
179 if (IEEE80211_IS_CHAN_ST(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
181 if (IEEE80211_IS_CHAN_HALF(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
183 if (IEEE80211_IS_CHAN_QUARTER(c))
184 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
185 if (IEEE80211_IS_CHAN_HTA(c))
186 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
187 if (IEEE80211_IS_CHAN_HTG(c))
188 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
190 /* initialize candidate channels to all available */
191 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
192 sizeof(ic->ic_chan_avail));
194 /* sort channel table to allow lookup optimizations */
195 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
197 /* invalidate any previous state */
198 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
199 ic->ic_prevchan = NULL;
200 ic->ic_csa_newchan = NULL;
201 /* arbitrarily pick the first channel */
202 ic->ic_curchan = &ic->ic_channels[0];
203 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
205 /* fillin well-known rate sets if driver has not specified */
206 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
207 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
208 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
209 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
210 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
211 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
212 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
213 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
214 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
215 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
218 * Setup required information to fill the mcsset field, if driver did
219 * not. Assume a 2T2R setup for historic reasons.
221 if (ic->ic_rxstream == 0)
223 if (ic->ic_txstream == 0)
227 * Set auto mode to reset active channel state and any desired channel.
229 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
234 null_update_mcast(struct ieee80211com *ic)
236 ic_printf(ic, "need multicast update callback\n");
240 null_update_promisc(struct ieee80211com *ic)
242 ic_printf(ic, "need promiscuous mode update callback\n");
246 null_transmit(struct ifnet *ifp, struct mbuf *m)
249 IFNET_STAT_INC(ifp, oerrors, 1);
250 return EACCES; /* XXX EIO/EPERM? */
253 #if defined(__DragonFly__)
255 null_output(struct ifnet *ifp, struct mbuf *m,
256 struct sockaddr *dst, struct rtentry *ro)
259 null_output(struct ifnet *ifp, struct mbuf *m,
260 const struct sockaddr *dst, struct route *ro)
263 if_printf(ifp, "discard raw packet\n");
264 return null_transmit(ifp, m);
267 #if defined(__DragonFly__)
270 null_input(struct ifnet *ifp, struct mbuf *m,
271 const struct pktinfo *pi, int cpuid)
273 if_printf(ifp, "if_input should not be called\n");
280 null_input(struct ifnet *ifp, struct mbuf *m)
282 if_printf(ifp, "if_input should not be called\n");
289 null_update_chw(struct ieee80211com *ic)
292 ic_printf(ic, "%s: need callback\n", __func__);
296 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
301 retval = kprintf("%s: ", ic->ic_name);
302 osdep_va_start(ap, fmt);
303 retval += kvprintf(fmt, ap);
309 * Attach/setup the common net80211 state. Called by
310 * the driver on attach to prior to creating any vap's.
313 ieee80211_ifattach(struct ieee80211com *ic,
314 const uint8_t macaddr[IEEE80211_ADDR_LEN])
316 struct ifnet *ifp = ic->ic_ifp;
317 struct sockaddr_dl *sdl;
320 KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
322 IEEE80211_LOCK_INIT(ic, ic->ic_name);
323 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
324 TAILQ_INIT(&ic->ic_vaps);
326 /* Create a taskqueue for all state changes */
327 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
328 taskqueue_thread_enqueue, &ic->ic_tq);
329 #if defined(__DragonFly__)
330 taskqueue_start_threads(&ic->ic_tq, 1, TDPRI_KERN_DAEMON, -1,
331 "%s net80211 taskq", ic->ic_name);
333 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
337 * Fill in 802.11 available channel set, mark all
338 * available channels as active, and pick a default
339 * channel if not already specified.
341 ieee80211_media_init(ic);
343 ic->ic_update_mcast = null_update_mcast;
344 ic->ic_update_promisc = null_update_promisc;
345 ic->ic_update_chw = null_update_chw;
347 ic->ic_hash_key = arc4random();
348 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
349 ic->ic_lintval = ic->ic_bintval;
350 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
352 ieee80211_crypto_attach(ic);
353 ieee80211_node_attach(ic);
354 ieee80211_power_attach(ic);
355 ieee80211_proto_attach(ic);
356 #ifdef IEEE80211_SUPPORT_SUPERG
357 ieee80211_superg_attach(ic);
359 ieee80211_ht_attach(ic);
360 ieee80211_scan_attach(ic);
361 ieee80211_regdomain_attach(ic);
362 ieee80211_dfs_attach(ic);
364 ieee80211_sysctl_attach(ic);
366 ifp->if_addrlen = IEEE80211_ADDR_LEN;
370 * If driver does not configure # of mbuf clusters/jclusters
371 * that could sit on the device queues for quite some time,
373 * - The device queues only consume mbuf clusters.
374 * - No more than ieee80211_nmbclusters_default (by default
375 * 128) mbuf clusters will sit on the device queues for
378 if (ifp->if_nmbclusters <= 0 && ifp->if_nmbjclusters <= 0) {
379 if (ieee80211_nmbclusters_default <
380 IEEE80211_NMBCLUSTERS_DEFMIN) {
381 kprintf("ieee80211 nmbclusters %d -> %d\n",
382 ieee80211_nmbclusters_default,
383 IEEE80211_NMBCLUSTERS_DEFAULT);
384 ieee80211_nmbclusters_default =
385 IEEE80211_NMBCLUSTERS_DEFAULT;
387 ifp->if_nmbclusters = ieee80211_nmbclusters_default;
393 * This function must _not_ be serialized by the WLAN serializer,
394 * since it could dead-lock the domsg to netisrs in if_attach().
396 wlan_serialize_exit();
397 #if defined(__DragonFly__)
398 if_attach(ifp, &wlan_global_serializer);
402 wlan_serialize_enter();
404 ifp->if_mtu = IEEE80211_MTU_MAX;
405 ifp->if_broadcastaddr = ieee80211broadcastaddr;
406 ifp->if_output = null_output;
407 ifp->if_input = null_input; /* just in case */
408 ifp->if_resolvemulti = NULL; /* NB: callers check */
410 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
411 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
412 sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */
413 sdl->sdl_alen = IEEE80211_ADDR_LEN;
414 IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
420 * Detach net80211 state on device detach. Tear down
421 * all vap's and reclaim all common state prior to the
422 * device state going away. Note we may call back into
423 * driver; it must be prepared for this.
426 ieee80211_ifdetach(struct ieee80211com *ic)
428 struct ifnet *ifp = ic->ic_ifp;
429 struct ieee80211vap *vap;
432 * The VAP is responsible for setting and clearing
433 * the VIMAGE context.
435 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
436 ieee80211_vap_destroy(vap);
439 * WLAN serializer must _not_ be held for if_detach(),
440 * since it could dead-lock the domsg to netisrs.
443 * This function actually should _not_ be serialized
444 * by the WLAN serializer, however, all 802.11 device
445 * drivers serialize it ...
447 wlan_serialize_exit();
450 * This detaches the main interface, but not the vaps.
451 * Each VAP may be in a separate VIMAGE.
453 * Detach the main interface _after_ all vaps are
454 * destroyed, since the main interface is referenced
455 * on vaps' detach path.
457 CURVNET_SET(ifp->if_vnet);
461 /* Re-hold WLAN serializer */
462 wlan_serialize_enter();
464 ieee80211_waitfor_parent(ic);
466 ieee80211_sysctl_detach(ic);
467 ieee80211_dfs_detach(ic);
468 ieee80211_regdomain_detach(ic);
469 ieee80211_scan_detach(ic);
470 #ifdef IEEE80211_SUPPORT_SUPERG
471 ieee80211_superg_detach(ic);
473 ieee80211_ht_detach(ic);
474 /* NB: must be called before ieee80211_node_detach */
475 ieee80211_proto_detach(ic);
476 ieee80211_crypto_detach(ic);
477 ieee80211_power_detach(ic);
478 ieee80211_node_detach(ic);
480 /* XXX VNET needed? */
481 ifmedia_removeall(&ic->ic_media);
483 taskqueue_free(ic->ic_tq);
484 IEEE80211_TX_LOCK_DESTROY(ic);
485 IEEE80211_LOCK_DESTROY(ic);
489 * Default reset method for use with the ioctl support. This
490 * method is invoked after any state change in the 802.11
491 * layer that should be propagated to the hardware but not
492 * require re-initialization of the 802.11 state machine (e.g
493 * rescanning for an ap). We always return ENETRESET which
494 * should cause the driver to re-initialize the device. Drivers
495 * can override this method to implement more optimized support.
498 default_reset(struct ieee80211vap *vap, u_long cmd)
504 * Prepare a vap for use. Drivers use this call to
505 * setup net80211 state in new vap's prior attaching
506 * them with ieee80211_vap_attach (below).
509 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
510 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
511 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
512 const uint8_t macaddr[IEEE80211_ADDR_LEN])
516 ifp = if_alloc(IFT_ETHER);
518 ic_printf(ic, "%s: unable to allocate ifnet\n",
522 if_initname(ifp, name, unit);
523 ifp->if_softc = vap; /* back pointer */
524 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
525 ifp->if_start = ieee80211_vap_start;
527 ifp->if_transmit = ieee80211_vap_transmit;
528 ifp->if_qflush = ieee80211_vap_qflush;
530 ifp->if_ioctl = ieee80211_ioctl;
531 ifp->if_init = ieee80211_init;
535 vap->iv_flags = ic->ic_flags; /* propagate common flags */
536 vap->iv_flags_ext = ic->ic_flags_ext;
537 vap->iv_flags_ven = ic->ic_flags_ven;
538 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
539 vap->iv_htcaps = ic->ic_htcaps;
540 vap->iv_htextcaps = ic->ic_htextcaps;
541 vap->iv_opmode = opmode;
542 vap->iv_caps |= ieee80211_opcap[opmode];
544 case IEEE80211_M_WDS:
546 * WDS links must specify the bssid of the far end.
547 * For legacy operation this is a static relationship.
548 * For non-legacy operation the station must associate
549 * and be authorized to pass traffic. Plumbing the
550 * vap to the proper node happens when the vap
551 * transitions to RUN state.
553 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
554 vap->iv_flags |= IEEE80211_F_DESBSSID;
555 if (flags & IEEE80211_CLONE_WDSLEGACY)
556 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
558 #ifdef IEEE80211_SUPPORT_TDMA
559 case IEEE80211_M_AHDEMO:
560 if (flags & IEEE80211_CLONE_TDMA) {
561 /* NB: checked before clone operation allowed */
562 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
563 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
565 * Propagate TDMA capability to mark vap; this
566 * cannot be removed and is used to distinguish
567 * regular ahdemo operation from ahdemo+tdma.
569 vap->iv_caps |= IEEE80211_C_TDMA;
576 /* auto-enable s/w beacon miss support */
577 if (flags & IEEE80211_CLONE_NOBEACONS)
578 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
579 /* auto-generated or user supplied MAC address */
580 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
581 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
583 * Enable various functionality by default if we're
584 * capable; the driver can override us if it knows better.
586 if (vap->iv_caps & IEEE80211_C_WME)
587 vap->iv_flags |= IEEE80211_F_WME;
588 if (vap->iv_caps & IEEE80211_C_BURST)
589 vap->iv_flags |= IEEE80211_F_BURST;
590 /* NB: bg scanning only makes sense for station mode right now */
593 * DISABLE BGSCAN BY DEFAULT, many issues can crop up including
594 * the link going dead.
596 if (vap->iv_opmode == IEEE80211_M_STA &&
597 (vap->iv_caps & IEEE80211_C_BGSCAN))
598 vap->iv_flags |= IEEE80211_F_BGSCAN;
600 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
601 /* NB: DFS support only makes sense for ap mode right now */
602 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
603 (vap->iv_caps & IEEE80211_C_DFS))
604 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
606 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
607 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
608 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
610 * Install a default reset method for the ioctl support;
611 * the driver can override this.
613 vap->iv_reset = default_reset;
615 IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
617 ieee80211_sysctl_vattach(vap);
618 ieee80211_crypto_vattach(vap);
619 ieee80211_node_vattach(vap);
620 ieee80211_power_vattach(vap);
621 ieee80211_proto_vattach(vap);
622 #ifdef IEEE80211_SUPPORT_SUPERG
623 ieee80211_superg_vattach(vap);
625 ieee80211_ht_vattach(vap);
626 ieee80211_scan_vattach(vap);
627 ieee80211_regdomain_vattach(vap);
628 ieee80211_radiotap_vattach(vap);
629 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
635 * Activate a vap. State should have been prepared with a
636 * call to ieee80211_vap_setup and by the driver. On return
637 * from this call the vap is ready for use.
640 ieee80211_vap_attach(struct ieee80211vap *vap,
641 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
643 struct ifnet *ifp = vap->iv_ifp;
644 struct ieee80211com *ic = vap->iv_ic;
645 struct ifmediareq imr;
649 * This function must _not_ be serialized by the WLAN serializer,
650 * since it could dead-lock the domsg to netisrs in ether_ifattach().
652 wlan_assert_notserialized();
654 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
655 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
656 __func__, ieee80211_opmode_name[vap->iv_opmode],
657 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
660 * Do late attach work that cannot happen until after
661 * the driver has had a chance to override defaults.
663 ieee80211_node_latevattach(vap);
664 ieee80211_power_latevattach(vap);
666 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
667 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
668 ieee80211_media_status(ifp, &imr);
669 /* NB: strip explicit mode; we're actually in autoselect */
670 ifmedia_set(&vap->iv_media,
671 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
673 ifp->if_baudrate = IF_Mbps(maxrate);
675 #if defined(__DragonFly__)
676 ether_ifattach(ifp, vap->iv_myaddr, &wlan_global_serializer);
678 ether_ifattach(ifp, vap->iv_myaddr);
680 /* hook output method setup by ether_ifattach */
681 vap->iv_output = ifp->if_output;
682 ifp->if_output = ieee80211_output;
683 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
686 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
687 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
688 #ifdef IEEE80211_SUPPORT_SUPERG
689 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
691 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
692 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
693 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
694 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
695 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
696 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
697 IEEE80211_UNLOCK(ic);
703 * Tear down vap state and reclaim the ifnet.
704 * The driver is assumed to have prepared for
705 * this; e.g. by turning off interrupts for the
709 ieee80211_vap_detach(struct ieee80211vap *vap)
711 struct ieee80211com *ic = vap->iv_ic;
712 struct ifnet *ifp = vap->iv_ifp;
715 * This function must _not_ be serialized by the WLAN serializer,
716 * since it could dead-lock the domsg to netisrs in ether_ifdettach().
718 wlan_assert_notserialized();
720 CURVNET_SET(ifp->if_vnet);
722 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
723 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
725 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
731 * Flush any deferred vap tasks.
733 ieee80211_draintask(ic, &vap->iv_nstate_task);
734 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
736 #if !defined(__DragonFly__)
737 /* XXX band-aid until ifnet handles this for us */
738 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
742 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
743 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
744 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
745 #ifdef IEEE80211_SUPPORT_SUPERG
746 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
748 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
749 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
750 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
751 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
752 /* NB: this handles the bpfdetach done below */
753 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
754 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
755 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
756 IEEE80211_UNLOCK(ic);
758 ifmedia_removeall(&vap->iv_media);
760 ieee80211_radiotap_vdetach(vap);
761 ieee80211_regdomain_vdetach(vap);
762 ieee80211_scan_vdetach(vap);
763 #ifdef IEEE80211_SUPPORT_SUPERG
764 ieee80211_superg_vdetach(vap);
766 ieee80211_ht_vdetach(vap);
767 /* NB: must be before ieee80211_node_vdetach */
768 ieee80211_proto_vdetach(vap);
769 ieee80211_crypto_vdetach(vap);
770 ieee80211_power_vdetach(vap);
771 ieee80211_node_vdetach(vap);
772 ieee80211_sysctl_vdetach(vap);
780 * Synchronize flag bit state in the parent ifnet structure
781 * according to the state of all vap ifnet's. This is used,
782 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
785 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
787 struct ifnet *ifp = ic->ic_ifp;
788 struct ieee80211vap *vap;
791 IEEE80211_LOCK_ASSERT(ic);
794 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
795 if (vap->iv_ifp->if_flags & flag) {
797 * XXX the bridge sets PROMISC but we don't want to
798 * enable it on the device, discard here so all the
799 * drivers don't need to special-case it
801 if (flag == IFF_PROMISC &&
802 !(vap->iv_opmode == IEEE80211_M_MONITOR ||
803 (vap->iv_opmode == IEEE80211_M_AHDEMO &&
804 (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
809 oflags = ifp->if_flags;
811 ifp->if_flags |= flag;
813 ifp->if_flags &= ~flag;
814 if ((ifp->if_flags ^ oflags) & flag) {
815 /* XXX should we return 1/0 and let caller do this? */
816 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
817 if (flag == IFF_PROMISC)
818 ieee80211_runtask(ic, &ic->ic_promisc_task);
819 else if (flag == IFF_ALLMULTI)
820 ieee80211_runtask(ic, &ic->ic_mcast_task);
826 * Synchronize flag bit state in the com structure
827 * according to the state of all vap's. This is used,
828 * for example, to handle state changes via ioctls.
831 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
833 struct ieee80211vap *vap;
836 IEEE80211_LOCK_ASSERT(ic);
839 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
840 if (vap->iv_flags & flag) {
845 ic->ic_flags |= flag;
847 ic->ic_flags &= ~flag;
851 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
853 struct ieee80211com *ic = vap->iv_ic;
858 vap->iv_flags &= ~flag;
860 vap->iv_flags |= flag;
861 ieee80211_syncflag_locked(ic, flag);
862 IEEE80211_UNLOCK(ic);
866 * Synchronize flags_ht bit state in the com structure
867 * according to the state of all vap's. This is used,
868 * for example, to handle state changes via ioctls.
871 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
873 struct ieee80211vap *vap;
876 IEEE80211_LOCK_ASSERT(ic);
879 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
880 if (vap->iv_flags_ht & flag) {
885 ic->ic_flags_ht |= flag;
887 ic->ic_flags_ht &= ~flag;
891 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
893 struct ieee80211com *ic = vap->iv_ic;
898 vap->iv_flags_ht &= ~flag;
900 vap->iv_flags_ht |= flag;
901 ieee80211_syncflag_ht_locked(ic, flag);
902 IEEE80211_UNLOCK(ic);
906 * Synchronize flags_ext bit state in the com structure
907 * according to the state of all vap's. This is used,
908 * for example, to handle state changes via ioctls.
911 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
913 struct ieee80211vap *vap;
916 IEEE80211_LOCK_ASSERT(ic);
919 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
920 if (vap->iv_flags_ext & flag) {
925 ic->ic_flags_ext |= flag;
927 ic->ic_flags_ext &= ~flag;
931 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
933 struct ieee80211com *ic = vap->iv_ic;
938 vap->iv_flags_ext &= ~flag;
940 vap->iv_flags_ext |= flag;
941 ieee80211_syncflag_ext_locked(ic, flag);
942 IEEE80211_UNLOCK(ic);
946 mapgsm(u_int freq, u_int flags)
949 if (flags & IEEE80211_CHAN_QUARTER)
951 else if (flags & IEEE80211_CHAN_HALF)
955 /* NB: there is no 907/20 wide but leave room */
956 return (freq - 906*10) / 5;
960 mappsb(u_int freq, u_int flags)
962 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
966 * Convert MHz frequency to IEEE channel number.
969 ieee80211_mhz2ieee(u_int freq, u_int flags)
971 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
972 if (flags & IEEE80211_CHAN_GSM)
973 return mapgsm(freq, flags);
974 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
978 return ((int) freq - 2407) / 5;
980 return 15 + ((freq - 2512) / 20);
981 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
983 /* XXX check regdomain? */
984 if (IS_FREQ_IN_PSB(freq))
985 return mappsb(freq, flags);
986 return (freq - 4000) / 5;
988 return (freq - 5000) / 5;
989 } else { /* either, guess */
993 if (907 <= freq && freq <= 922)
994 return mapgsm(freq, flags);
995 return ((int) freq - 2407) / 5;
998 if (IS_FREQ_IN_PSB(freq))
999 return mappsb(freq, flags);
1000 else if (freq > 4900)
1001 return (freq - 4000) / 5;
1003 return 15 + ((freq - 2512) / 20);
1005 return (freq - 5000) / 5;
1007 #undef IS_FREQ_IN_PSB
1011 * Convert channel to IEEE channel number.
1014 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
1017 ic_printf(ic, "invalid channel (NULL)\n");
1020 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
1024 * Convert IEEE channel number to MHz frequency.
1027 ieee80211_ieee2mhz(u_int chan, u_int flags)
1029 if (flags & IEEE80211_CHAN_GSM)
1030 return 907 + 5 * (chan / 10);
1031 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
1035 return 2407 + chan*5;
1037 return 2512 + ((chan-15)*20);
1038 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
1039 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
1041 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
1043 return 5000 + (chan*5);
1044 } else { /* either, guess */
1045 /* XXX can't distinguish PSB+GSM channels */
1048 if (chan < 14) /* 0-13 */
1049 return 2407 + chan*5;
1050 if (chan < 27) /* 15-26 */
1051 return 2512 + ((chan-15)*20);
1052 return 5000 + (chan*5);
1057 * Locate a channel given a frequency+flags. We cache
1058 * the previous lookup to optimize switching between two
1059 * channels--as happens with dynamic turbo.
1061 struct ieee80211_channel *
1062 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1064 struct ieee80211_channel *c;
1067 flags &= IEEE80211_CHAN_ALLTURBO;
1068 c = ic->ic_prevchan;
1069 if (c != NULL && c->ic_freq == freq &&
1070 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1072 /* brute force search */
1073 for (i = 0; i < ic->ic_nchans; i++) {
1074 c = &ic->ic_channels[i];
1075 if (c->ic_freq == freq &&
1076 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1083 * Locate a channel given a channel number+flags. We cache
1084 * the previous lookup to optimize switching between two
1085 * channels--as happens with dynamic turbo.
1087 struct ieee80211_channel *
1088 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1090 struct ieee80211_channel *c;
1093 flags &= IEEE80211_CHAN_ALLTURBO;
1094 c = ic->ic_prevchan;
1095 if (c != NULL && c->ic_ieee == ieee &&
1096 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1098 /* brute force search */
1099 for (i = 0; i < ic->ic_nchans; i++) {
1100 c = &ic->ic_channels[i];
1101 if (c->ic_ieee == ieee &&
1102 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1109 * Lookup a channel suitable for the given rx status.
1111 * This is used to find a channel for a frame (eg beacon, probe
1112 * response) based purely on the received PHY information.
1114 * For now it tries to do it based on R_FREQ / R_IEEE.
1115 * This is enough for 11bg and 11a (and thus 11ng/11na)
1116 * but it will not be enough for GSM, PSB channels and the
1117 * like. It also doesn't know about legacy-turbog and
1118 * legacy-turbo modes, which some offload NICs actually
1119 * support in weird ways.
1121 * Takes the ic and rxstatus; returns the channel or NULL
1124 * XXX TODO: Add support for that when the need arises.
1126 struct ieee80211_channel *
1127 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1128 const struct ieee80211_rx_stats *rxs)
1130 struct ieee80211com *ic = vap->iv_ic;
1132 struct ieee80211_channel *c;
1138 * Strictly speaking we only use freq for now,
1139 * however later on we may wish to just store
1140 * the ieee for verification.
1142 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1144 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1148 * If the rx status contains a valid ieee/freq, then
1149 * ensure we populate the correct channel information
1150 * in rxchan before passing it up to the scan infrastructure.
1151 * Offload NICs will pass up beacons from all channels
1152 * during background scans.
1155 /* Determine a band */
1156 /* XXX should be done by the driver? */
1157 if (rxs->c_freq < 3000) {
1158 flags = IEEE80211_CHAN_G;
1160 flags = IEEE80211_CHAN_A;
1163 /* Channel lookup */
1164 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1166 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1167 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1178 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1180 #define ADD(_ic, _s, _o) \
1181 ifmedia_add(media, \
1182 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1183 static const u_int mopts[IEEE80211_MODE_MAX] = {
1184 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1185 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1186 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1187 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1188 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1189 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1190 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1191 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1192 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1193 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1194 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1195 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1201 ADD(ic, mword, mopt); /* STA mode has no cap */
1202 if (caps & IEEE80211_C_IBSS)
1203 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1204 if (caps & IEEE80211_C_HOSTAP)
1205 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1206 if (caps & IEEE80211_C_AHDEMO)
1207 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1208 if (caps & IEEE80211_C_MONITOR)
1209 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1210 if (caps & IEEE80211_C_WDS)
1211 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1212 if (caps & IEEE80211_C_MBSS)
1213 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1218 * Setup the media data structures according to the channel and
1222 ieee80211_media_setup(struct ieee80211com *ic,
1223 struct ifmedia *media, int caps, int addsta,
1224 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1226 int i, j, rate, maxrate, mword, r;
1227 enum ieee80211_phymode mode;
1228 const struct ieee80211_rateset *rs;
1229 struct ieee80211_rateset allrates;
1232 * Fill in media characteristics.
1234 ifmedia_init(media, 0, media_change, media_stat);
1237 * Add media for legacy operating modes.
1239 memset(&allrates, 0, sizeof(allrates));
1240 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1241 if (isclr(ic->ic_modecaps, mode))
1243 addmedia(media, caps, addsta, mode, IFM_AUTO);
1244 if (mode == IEEE80211_MODE_AUTO)
1246 rs = &ic->ic_sup_rates[mode];
1247 for (i = 0; i < rs->rs_nrates; i++) {
1248 rate = rs->rs_rates[i];
1249 mword = ieee80211_rate2media(ic, rate, mode);
1252 addmedia(media, caps, addsta, mode, mword);
1254 * Add legacy rate to the collection of all rates.
1256 r = rate & IEEE80211_RATE_VAL;
1257 for (j = 0; j < allrates.rs_nrates; j++)
1258 if (allrates.rs_rates[j] == r)
1260 if (j == allrates.rs_nrates) {
1261 /* unique, add to the set */
1262 allrates.rs_rates[j] = r;
1263 allrates.rs_nrates++;
1265 rate = (rate & IEEE80211_RATE_VAL) / 2;
1270 for (i = 0; i < allrates.rs_nrates; i++) {
1271 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1272 IEEE80211_MODE_AUTO);
1275 /* NB: remove media options from mword */
1276 addmedia(media, caps, addsta,
1277 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1280 * Add HT/11n media. Note that we do not have enough
1281 * bits in the media subtype to express the MCS so we
1282 * use a "placeholder" media subtype and any fixed MCS
1283 * must be specified with a different mechanism.
1285 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1286 if (isclr(ic->ic_modecaps, mode))
1288 addmedia(media, caps, addsta, mode, IFM_AUTO);
1289 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1291 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1292 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1293 addmedia(media, caps, addsta,
1294 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1295 i = ic->ic_txstream * 8 - 1;
1296 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1297 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1298 rate = ieee80211_htrates[i].ht40_rate_400ns;
1299 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1300 rate = ieee80211_htrates[i].ht40_rate_800ns;
1301 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1302 rate = ieee80211_htrates[i].ht20_rate_400ns;
1304 rate = ieee80211_htrates[i].ht20_rate_800ns;
1312 ieee80211_media_init(struct ieee80211com *ic)
1314 struct ifnet *ifp = ic->ic_ifp;
1317 /* NB: this works because the structure is initialized to zero */
1318 if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1320 * We are re-initializing the channel list; clear
1321 * the existing media state as the media routines
1322 * don't suppress duplicates.
1324 ifmedia_removeall(&ic->ic_media);
1326 ieee80211_chan_init(ic);
1329 * Recalculate media settings in case new channel list changes
1330 * the set of available modes.
1332 maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1333 ieee80211com_media_change, ieee80211com_media_status);
1334 /* NB: strip explicit mode; we're actually in autoselect */
1335 ifmedia_set(&ic->ic_media,
1336 media_status(ic->ic_opmode, ic->ic_curchan) &~
1337 (IFM_MMASK | IFM_IEEE80211_TURBO));
1339 ifp->if_baudrate = IF_Mbps(maxrate);
1341 /* XXX need to propagate new media settings to vap's */
1344 /* XXX inline or eliminate? */
1345 const struct ieee80211_rateset *
1346 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1348 /* XXX does this work for 11ng basic rates? */
1349 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1353 ieee80211_announce(struct ieee80211com *ic)
1356 enum ieee80211_phymode mode;
1357 const struct ieee80211_rateset *rs;
1359 /* NB: skip AUTO since it has no rates */
1360 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1361 if (isclr(ic->ic_modecaps, mode))
1363 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1364 rs = &ic->ic_sup_rates[mode];
1365 for (i = 0; i < rs->rs_nrates; i++) {
1366 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1369 rate = ieee80211_media2rate(mword);
1370 kprintf("%s%d%sMbps", (i != 0 ? " " : ""),
1371 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1375 ieee80211_ht_announce(ic);
1379 ieee80211_announce_channels(struct ieee80211com *ic)
1381 const struct ieee80211_channel *c;
1385 kprintf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1386 for (i = 0; i < ic->ic_nchans; i++) {
1387 c = &ic->ic_channels[i];
1388 if (IEEE80211_IS_CHAN_ST(c))
1390 else if (IEEE80211_IS_CHAN_108A(c))
1392 else if (IEEE80211_IS_CHAN_108G(c))
1394 else if (IEEE80211_IS_CHAN_HT(c))
1396 else if (IEEE80211_IS_CHAN_A(c))
1398 else if (IEEE80211_IS_CHAN_ANYG(c))
1400 else if (IEEE80211_IS_CHAN_B(c))
1404 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1406 else if (IEEE80211_IS_CHAN_HALF(c))
1408 else if (IEEE80211_IS_CHAN_QUARTER(c))
1412 kprintf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1413 , c->ic_ieee, c->ic_freq, type
1415 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1416 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1418 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1419 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1425 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1427 switch (IFM_MODE(ime->ifm_media)) {
1428 case IFM_IEEE80211_11A:
1429 *mode = IEEE80211_MODE_11A;
1431 case IFM_IEEE80211_11B:
1432 *mode = IEEE80211_MODE_11B;
1434 case IFM_IEEE80211_11G:
1435 *mode = IEEE80211_MODE_11G;
1437 case IFM_IEEE80211_FH:
1438 *mode = IEEE80211_MODE_FH;
1440 case IFM_IEEE80211_11NA:
1441 *mode = IEEE80211_MODE_11NA;
1443 case IFM_IEEE80211_11NG:
1444 *mode = IEEE80211_MODE_11NG;
1447 *mode = IEEE80211_MODE_AUTO;
1453 * Turbo mode is an ``option''.
1454 * XXX does not apply to AUTO
1456 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1457 if (*mode == IEEE80211_MODE_11A) {
1458 if (flags & IEEE80211_F_TURBOP)
1459 *mode = IEEE80211_MODE_TURBO_A;
1461 *mode = IEEE80211_MODE_STURBO_A;
1462 } else if (*mode == IEEE80211_MODE_11G)
1463 *mode = IEEE80211_MODE_TURBO_G;
1472 * Handle a media change request on the underlying interface.
1475 ieee80211com_media_change(struct ifnet *ifp)
1481 * Handle a media change request on the vap interface.
1484 ieee80211_media_change(struct ifnet *ifp)
1486 struct ieee80211vap *vap = ifp->if_softc;
1487 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1490 if (!media2mode(ime, vap->iv_flags, &newmode))
1492 if (vap->iv_des_mode != newmode) {
1493 vap->iv_des_mode = newmode;
1494 /* XXX kick state machine if up+running */
1500 * Common code to calculate the media status word
1501 * from the operating mode and channel state.
1504 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1508 status = IFM_IEEE80211;
1510 case IEEE80211_M_STA:
1512 case IEEE80211_M_IBSS:
1513 status |= IFM_IEEE80211_ADHOC;
1515 case IEEE80211_M_HOSTAP:
1516 status |= IFM_IEEE80211_HOSTAP;
1518 case IEEE80211_M_MONITOR:
1519 status |= IFM_IEEE80211_MONITOR;
1521 case IEEE80211_M_AHDEMO:
1522 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1524 case IEEE80211_M_WDS:
1525 status |= IFM_IEEE80211_WDS;
1527 case IEEE80211_M_MBSS:
1528 status |= IFM_IEEE80211_MBSS;
1531 if (IEEE80211_IS_CHAN_HTA(chan)) {
1532 status |= IFM_IEEE80211_11NA;
1533 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1534 status |= IFM_IEEE80211_11NG;
1535 } else if (IEEE80211_IS_CHAN_A(chan)) {
1536 status |= IFM_IEEE80211_11A;
1537 } else if (IEEE80211_IS_CHAN_B(chan)) {
1538 status |= IFM_IEEE80211_11B;
1539 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1540 status |= IFM_IEEE80211_11G;
1541 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1542 status |= IFM_IEEE80211_FH;
1544 /* XXX else complain? */
1546 if (IEEE80211_IS_CHAN_TURBO(chan))
1547 status |= IFM_IEEE80211_TURBO;
1549 if (IEEE80211_IS_CHAN_HT20(chan))
1550 status |= IFM_IEEE80211_HT20;
1551 if (IEEE80211_IS_CHAN_HT40(chan))
1552 status |= IFM_IEEE80211_HT40;
1558 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1560 struct ieee80211com *ic = ifp->if_l2com;
1561 struct ieee80211vap *vap;
1563 imr->ifm_status = IFM_AVALID;
1564 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1565 if (vap->iv_ifp->if_flags & IFF_UP) {
1566 imr->ifm_status |= IFM_ACTIVE;
1569 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1570 if (imr->ifm_status & IFM_ACTIVE)
1571 imr->ifm_current = imr->ifm_active;
1575 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1577 struct ieee80211vap *vap = ifp->if_softc;
1578 struct ieee80211com *ic = vap->iv_ic;
1579 enum ieee80211_phymode mode;
1581 imr->ifm_status = IFM_AVALID;
1583 * NB: use the current channel's mode to lock down a xmit
1584 * rate only when running; otherwise we may have a mismatch
1585 * in which case the rate will not be convertible.
1587 if (vap->iv_state == IEEE80211_S_RUN ||
1588 vap->iv_state == IEEE80211_S_SLEEP) {
1589 imr->ifm_status |= IFM_ACTIVE;
1590 mode = ieee80211_chan2mode(ic->ic_curchan);
1592 mode = IEEE80211_MODE_AUTO;
1593 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1595 * Calculate a current rate if possible.
1597 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1599 * A fixed rate is set, report that.
1601 imr->ifm_active |= ieee80211_rate2media(ic,
1602 vap->iv_txparms[mode].ucastrate, mode);
1603 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1605 * In station mode report the current transmit rate.
1607 imr->ifm_active |= ieee80211_rate2media(ic,
1608 vap->iv_bss->ni_txrate, mode);
1610 imr->ifm_active |= IFM_AUTO;
1611 if (imr->ifm_status & IFM_ACTIVE)
1612 imr->ifm_current = imr->ifm_active;
1616 * Set the current phy mode and recalculate the active channel
1617 * set based on the available channels for this mode. Also
1618 * select a new default/current channel if the current one is
1619 * inappropriate for this mode.
1622 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1625 * Adjust basic rates in 11b/11g supported rate set.
1626 * Note that if operating on a hal/quarter rate channel
1627 * this is a noop as those rates sets are different
1630 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1631 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1633 ic->ic_curmode = mode;
1634 ieee80211_reset_erp(ic); /* reset ERP state */
1640 * Return the phy mode for with the specified channel.
1642 enum ieee80211_phymode
1643 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1646 if (IEEE80211_IS_CHAN_HTA(chan))
1647 return IEEE80211_MODE_11NA;
1648 else if (IEEE80211_IS_CHAN_HTG(chan))
1649 return IEEE80211_MODE_11NG;
1650 else if (IEEE80211_IS_CHAN_108G(chan))
1651 return IEEE80211_MODE_TURBO_G;
1652 else if (IEEE80211_IS_CHAN_ST(chan))
1653 return IEEE80211_MODE_STURBO_A;
1654 else if (IEEE80211_IS_CHAN_TURBO(chan))
1655 return IEEE80211_MODE_TURBO_A;
1656 else if (IEEE80211_IS_CHAN_HALF(chan))
1657 return IEEE80211_MODE_HALF;
1658 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1659 return IEEE80211_MODE_QUARTER;
1660 else if (IEEE80211_IS_CHAN_A(chan))
1661 return IEEE80211_MODE_11A;
1662 else if (IEEE80211_IS_CHAN_ANYG(chan))
1663 return IEEE80211_MODE_11G;
1664 else if (IEEE80211_IS_CHAN_B(chan))
1665 return IEEE80211_MODE_11B;
1666 else if (IEEE80211_IS_CHAN_FHSS(chan))
1667 return IEEE80211_MODE_FH;
1669 /* NB: should not get here */
1670 kprintf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1671 __func__, chan->ic_freq, chan->ic_flags);
1672 return IEEE80211_MODE_11B;
1676 u_int match; /* rate + mode */
1677 u_int media; /* if_media rate */
1681 findmedia(const struct ratemedia rates[], int n, u_int match)
1685 for (i = 0; i < n; i++)
1686 if (rates[i].match == match)
1687 return rates[i].media;
1692 * Convert IEEE80211 rate value to ifmedia subtype.
1693 * Rate is either a legacy rate in units of 0.5Mbps
1697 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1699 static const struct ratemedia rates[] = {
1700 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1701 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1702 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1703 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1704 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1705 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1706 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1707 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1708 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1709 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1710 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1711 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1712 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1713 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1714 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1715 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1716 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1717 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1718 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1719 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1720 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1721 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1722 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1723 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1724 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1725 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1726 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1727 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1728 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1729 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1730 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1732 static const struct ratemedia htrates[] = {
1733 { 0, IFM_IEEE80211_MCS },
1734 { 1, IFM_IEEE80211_MCS },
1735 { 2, IFM_IEEE80211_MCS },
1736 { 3, IFM_IEEE80211_MCS },
1737 { 4, IFM_IEEE80211_MCS },
1738 { 5, IFM_IEEE80211_MCS },
1739 { 6, IFM_IEEE80211_MCS },
1740 { 7, IFM_IEEE80211_MCS },
1741 { 8, IFM_IEEE80211_MCS },
1742 { 9, IFM_IEEE80211_MCS },
1743 { 10, IFM_IEEE80211_MCS },
1744 { 11, IFM_IEEE80211_MCS },
1745 { 12, IFM_IEEE80211_MCS },
1746 { 13, IFM_IEEE80211_MCS },
1747 { 14, IFM_IEEE80211_MCS },
1748 { 15, IFM_IEEE80211_MCS },
1749 { 16, IFM_IEEE80211_MCS },
1750 { 17, IFM_IEEE80211_MCS },
1751 { 18, IFM_IEEE80211_MCS },
1752 { 19, IFM_IEEE80211_MCS },
1753 { 20, IFM_IEEE80211_MCS },
1754 { 21, IFM_IEEE80211_MCS },
1755 { 22, IFM_IEEE80211_MCS },
1756 { 23, IFM_IEEE80211_MCS },
1757 { 24, IFM_IEEE80211_MCS },
1758 { 25, IFM_IEEE80211_MCS },
1759 { 26, IFM_IEEE80211_MCS },
1760 { 27, IFM_IEEE80211_MCS },
1761 { 28, IFM_IEEE80211_MCS },
1762 { 29, IFM_IEEE80211_MCS },
1763 { 30, IFM_IEEE80211_MCS },
1764 { 31, IFM_IEEE80211_MCS },
1765 { 32, IFM_IEEE80211_MCS },
1766 { 33, IFM_IEEE80211_MCS },
1767 { 34, IFM_IEEE80211_MCS },
1768 { 35, IFM_IEEE80211_MCS },
1769 { 36, IFM_IEEE80211_MCS },
1770 { 37, IFM_IEEE80211_MCS },
1771 { 38, IFM_IEEE80211_MCS },
1772 { 39, IFM_IEEE80211_MCS },
1773 { 40, IFM_IEEE80211_MCS },
1774 { 41, IFM_IEEE80211_MCS },
1775 { 42, IFM_IEEE80211_MCS },
1776 { 43, IFM_IEEE80211_MCS },
1777 { 44, IFM_IEEE80211_MCS },
1778 { 45, IFM_IEEE80211_MCS },
1779 { 46, IFM_IEEE80211_MCS },
1780 { 47, IFM_IEEE80211_MCS },
1781 { 48, IFM_IEEE80211_MCS },
1782 { 49, IFM_IEEE80211_MCS },
1783 { 50, IFM_IEEE80211_MCS },
1784 { 51, IFM_IEEE80211_MCS },
1785 { 52, IFM_IEEE80211_MCS },
1786 { 53, IFM_IEEE80211_MCS },
1787 { 54, IFM_IEEE80211_MCS },
1788 { 55, IFM_IEEE80211_MCS },
1789 { 56, IFM_IEEE80211_MCS },
1790 { 57, IFM_IEEE80211_MCS },
1791 { 58, IFM_IEEE80211_MCS },
1792 { 59, IFM_IEEE80211_MCS },
1793 { 60, IFM_IEEE80211_MCS },
1794 { 61, IFM_IEEE80211_MCS },
1795 { 62, IFM_IEEE80211_MCS },
1796 { 63, IFM_IEEE80211_MCS },
1797 { 64, IFM_IEEE80211_MCS },
1798 { 65, IFM_IEEE80211_MCS },
1799 { 66, IFM_IEEE80211_MCS },
1800 { 67, IFM_IEEE80211_MCS },
1801 { 68, IFM_IEEE80211_MCS },
1802 { 69, IFM_IEEE80211_MCS },
1803 { 70, IFM_IEEE80211_MCS },
1804 { 71, IFM_IEEE80211_MCS },
1805 { 72, IFM_IEEE80211_MCS },
1806 { 73, IFM_IEEE80211_MCS },
1807 { 74, IFM_IEEE80211_MCS },
1808 { 75, IFM_IEEE80211_MCS },
1809 { 76, IFM_IEEE80211_MCS },
1814 * Check 11n rates first for match as an MCS.
1816 if (mode == IEEE80211_MODE_11NA) {
1817 if (rate & IEEE80211_RATE_MCS) {
1818 rate &= ~IEEE80211_RATE_MCS;
1819 m = findmedia(htrates, nitems(htrates), rate);
1821 return m | IFM_IEEE80211_11NA;
1823 } else if (mode == IEEE80211_MODE_11NG) {
1824 /* NB: 12 is ambiguous, it will be treated as an MCS */
1825 if (rate & IEEE80211_RATE_MCS) {
1826 rate &= ~IEEE80211_RATE_MCS;
1827 m = findmedia(htrates, nitems(htrates), rate);
1829 return m | IFM_IEEE80211_11NG;
1832 rate &= IEEE80211_RATE_VAL;
1834 case IEEE80211_MODE_11A:
1835 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1836 case IEEE80211_MODE_QUARTER:
1837 case IEEE80211_MODE_11NA:
1838 case IEEE80211_MODE_TURBO_A:
1839 case IEEE80211_MODE_STURBO_A:
1840 return findmedia(rates, nitems(rates),
1841 rate | IFM_IEEE80211_11A);
1842 case IEEE80211_MODE_11B:
1843 return findmedia(rates, nitems(rates),
1844 rate | IFM_IEEE80211_11B);
1845 case IEEE80211_MODE_FH:
1846 return findmedia(rates, nitems(rates),
1847 rate | IFM_IEEE80211_FH);
1848 case IEEE80211_MODE_AUTO:
1849 /* NB: ic may be NULL for some drivers */
1850 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1851 return findmedia(rates, nitems(rates),
1852 rate | IFM_IEEE80211_FH);
1853 /* NB: hack, 11g matches both 11b+11a rates */
1855 case IEEE80211_MODE_11G:
1856 case IEEE80211_MODE_11NG:
1857 case IEEE80211_MODE_TURBO_G:
1858 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1864 ieee80211_media2rate(int mword)
1866 static const int ieeerates[] = {
1870 2, /* IFM_IEEE80211_FH1 */
1871 4, /* IFM_IEEE80211_FH2 */
1872 2, /* IFM_IEEE80211_DS1 */
1873 4, /* IFM_IEEE80211_DS2 */
1874 11, /* IFM_IEEE80211_DS5 */
1875 22, /* IFM_IEEE80211_DS11 */
1876 44, /* IFM_IEEE80211_DS22 */
1877 12, /* IFM_IEEE80211_OFDM6 */
1878 18, /* IFM_IEEE80211_OFDM9 */
1879 24, /* IFM_IEEE80211_OFDM12 */
1880 36, /* IFM_IEEE80211_OFDM18 */
1881 48, /* IFM_IEEE80211_OFDM24 */
1882 72, /* IFM_IEEE80211_OFDM36 */
1883 96, /* IFM_IEEE80211_OFDM48 */
1884 108, /* IFM_IEEE80211_OFDM54 */
1885 144, /* IFM_IEEE80211_OFDM72 */
1886 0, /* IFM_IEEE80211_DS354k */
1887 0, /* IFM_IEEE80211_DS512k */
1888 6, /* IFM_IEEE80211_OFDM3 */
1889 9, /* IFM_IEEE80211_OFDM4 */
1890 54, /* IFM_IEEE80211_OFDM27 */
1891 -1, /* IFM_IEEE80211_MCS */
1893 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1894 ieeerates[IFM_SUBTYPE(mword)] : 0;
1898 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1899 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1901 #define mix(a, b, c) \
1903 a -= b; a -= c; a ^= (c >> 13); \
1904 b -= c; b -= a; b ^= (a << 8); \
1905 c -= a; c -= b; c ^= (b >> 13); \
1906 a -= b; a -= c; a ^= (c >> 12); \
1907 b -= c; b -= a; b ^= (a << 16); \
1908 c -= a; c -= b; c ^= (b >> 5); \
1909 a -= b; a -= c; a ^= (c >> 3); \
1910 b -= c; b -= a; b ^= (a << 10); \
1911 c -= a; c -= b; c ^= (b >> 15); \
1912 } while (/*CONSTCOND*/0)
1915 ieee80211_mac_hash(const struct ieee80211com *ic,
1916 const uint8_t addr[IEEE80211_ADDR_LEN])
1918 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1934 ieee80211_channel_type_char(const struct ieee80211_channel *c)
1936 if (IEEE80211_IS_CHAN_ST(c))
1938 if (IEEE80211_IS_CHAN_108A(c))
1940 if (IEEE80211_IS_CHAN_108G(c))
1942 if (IEEE80211_IS_CHAN_HT(c))
1944 if (IEEE80211_IS_CHAN_A(c))
1946 if (IEEE80211_IS_CHAN_ANYG(c))
1948 if (IEEE80211_IS_CHAN_B(c))