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 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
58 [IEEE80211_MODE_AUTO] = "auto",
59 [IEEE80211_MODE_11A] = "11a",
60 [IEEE80211_MODE_11B] = "11b",
61 [IEEE80211_MODE_11G] = "11g",
62 [IEEE80211_MODE_FH] = "FH",
63 [IEEE80211_MODE_TURBO_A] = "turboA",
64 [IEEE80211_MODE_TURBO_G] = "turboG",
65 [IEEE80211_MODE_STURBO_A] = "sturboA",
66 [IEEE80211_MODE_HALF] = "half",
67 [IEEE80211_MODE_QUARTER] = "quarter",
68 [IEEE80211_MODE_11NA] = "11na",
69 [IEEE80211_MODE_11NG] = "11ng",
71 /* map ieee80211_opmode to the corresponding capability bit */
72 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
73 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
74 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
75 [IEEE80211_M_STA] = IEEE80211_C_STA,
76 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
77 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
78 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
79 #ifdef IEEE80211_SUPPORT_MESH
80 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
84 static const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
85 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
87 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
88 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
89 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
90 static int ieee80211_media_setup(struct ieee80211com *ic,
91 struct ifmedia *media, int caps, int addsta,
92 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
93 static void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
94 static int ieee80211com_media_change(struct ifnet *);
95 static int media_status(enum ieee80211_opmode,
96 const struct ieee80211_channel *);
98 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
101 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
103 #define B(r) ((r) | IEEE80211_RATE_BASIC)
104 static const struct ieee80211_rateset ieee80211_rateset_11a =
105 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
106 static const struct ieee80211_rateset ieee80211_rateset_half =
107 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
108 static const struct ieee80211_rateset ieee80211_rateset_quarter =
109 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
110 static const struct ieee80211_rateset ieee80211_rateset_11b =
111 { 4, { B(2), B(4), B(11), B(22) } };
112 /* NB: OFDM rates are handled specially based on mode */
113 static const struct ieee80211_rateset ieee80211_rateset_11g =
114 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
118 * Fill in 802.11 available channel set, mark
119 * all available channels as active, and pick
120 * a default channel if not already specified.
123 ieee80211_chan_init(struct ieee80211com *ic)
125 #define DEFAULTRATES(m, def) do { \
126 if (ic->ic_sup_rates[m].rs_nrates == 0) \
127 ic->ic_sup_rates[m] = def; \
129 struct ieee80211_channel *c;
132 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
133 ("invalid number of channels specified: %u", ic->ic_nchans));
134 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
135 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
136 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
137 for (i = 0; i < ic->ic_nchans; i++) {
138 c = &ic->ic_channels[i];
139 KASSERT(c->ic_flags != 0, ("channel with no flags"));
141 * Help drivers that work only with frequencies by filling
142 * in IEEE channel #'s if not already calculated. Note this
143 * mimics similar work done in ieee80211_setregdomain when
144 * changing regulatory state.
147 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
148 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
149 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
150 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
152 /* default max tx power to max regulatory */
153 if (c->ic_maxpower == 0)
154 c->ic_maxpower = 2*c->ic_maxregpower;
155 setbit(ic->ic_chan_avail, c->ic_ieee);
157 * Identify mode capabilities.
159 if (IEEE80211_IS_CHAN_A(c))
160 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
161 if (IEEE80211_IS_CHAN_B(c))
162 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
163 if (IEEE80211_IS_CHAN_ANYG(c))
164 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
165 if (IEEE80211_IS_CHAN_FHSS(c))
166 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
167 if (IEEE80211_IS_CHAN_108A(c))
168 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
169 if (IEEE80211_IS_CHAN_108G(c))
170 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
171 if (IEEE80211_IS_CHAN_ST(c))
172 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
173 if (IEEE80211_IS_CHAN_HALF(c))
174 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
175 if (IEEE80211_IS_CHAN_QUARTER(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
177 if (IEEE80211_IS_CHAN_HTA(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
179 if (IEEE80211_IS_CHAN_HTG(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
182 /* initialize candidate channels to all available */
183 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
184 sizeof(ic->ic_chan_avail));
186 /* sort channel table to allow lookup optimizations */
187 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
189 /* invalidate any previous state */
190 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
191 ic->ic_prevchan = NULL;
192 ic->ic_csa_newchan = NULL;
193 /* arbitrarily pick the first channel */
194 ic->ic_curchan = &ic->ic_channels[0];
195 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
197 /* fillin well-known rate sets if driver has not specified */
198 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
199 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
200 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
201 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
202 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
203 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
204 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
205 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
206 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
207 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
210 * Setup required information to fill the mcsset field, if driver did
211 * not. Assume a 2T2R setup for historic reasons.
213 if (ic->ic_rxstream == 0)
215 if (ic->ic_txstream == 0)
219 * Set auto mode to reset active channel state and any desired channel.
221 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
226 null_update_mcast(struct ifnet *ifp)
228 if_printf(ifp, "need multicast update callback\n");
232 null_update_promisc(struct ifnet *ifp)
234 if_printf(ifp, "need promiscuous mode update callback\n");
238 null_transmit(struct ifnet *ifp, struct mbuf *m)
241 IFNET_STAT_INC(ifp, oerrors, 1);
242 return EACCES; /* XXX EIO/EPERM? */
245 #if defined(__DragonFly__)
247 null_output(struct ifnet *ifp, struct mbuf *m,
248 struct sockaddr *dst, struct rtentry *ro)
249 #elif __FreeBSD_version >= 1000031
251 null_output(struct ifnet *ifp, struct mbuf *m,
252 const struct sockaddr *dst, struct route *ro)
255 null_output(struct ifnet *ifp, struct mbuf *m,
256 struct sockaddr *dst, struct route *ro)
259 if_printf(ifp, "discard raw packet\n");
260 return null_transmit(ifp, m);
263 #if defined(__DragonFly__)
266 null_input(struct ifnet *ifp, struct mbuf *m,
267 const struct pktinfo *pi, int cpuid)
269 if_printf(ifp, "if_input should not be called\n");
276 null_input(struct ifnet *ifp, struct mbuf *m)
278 if_printf(ifp, "if_input should not be called\n");
285 null_update_chw(struct ieee80211com *ic)
288 if_printf(ic->ic_ifp, "%s: need callback\n", __func__);
292 * Attach/setup the common net80211 state. Called by
293 * the driver on attach to prior to creating any vap's.
296 ieee80211_ifattach(struct ieee80211com *ic,
297 const uint8_t macaddr[IEEE80211_ADDR_LEN])
299 struct ifnet *ifp = ic->ic_ifp;
300 struct sockaddr_dl *sdl;
303 KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
305 IEEE80211_LOCK_INIT(ic, ifp->if_xname);
306 IEEE80211_TX_LOCK_INIT(ic, ifp->if_xname);
307 TAILQ_INIT(&ic->ic_vaps);
309 /* Create a taskqueue for all state changes */
310 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
311 taskqueue_thread_enqueue, &ic->ic_tq);
312 #if defined(__DragonFly__)
313 taskqueue_start_threads(&ic->ic_tq, 1, TDPRI_KERN_DAEMON, -1,
314 "%s net80211 taskq", ifp->if_xname);
316 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
320 * Fill in 802.11 available channel set, mark all
321 * available channels as active, and pick a default
322 * channel if not already specified.
324 ieee80211_media_init(ic);
326 ic->ic_update_mcast = null_update_mcast;
327 ic->ic_update_promisc = null_update_promisc;
328 ic->ic_update_chw = null_update_chw;
330 ic->ic_hash_key = arc4random();
331 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
332 ic->ic_lintval = ic->ic_bintval;
333 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
335 ieee80211_crypto_attach(ic);
336 ieee80211_node_attach(ic);
337 ieee80211_power_attach(ic);
338 ieee80211_proto_attach(ic);
339 #ifdef IEEE80211_SUPPORT_SUPERG
340 ieee80211_superg_attach(ic);
342 ieee80211_ht_attach(ic);
343 ieee80211_scan_attach(ic);
344 ieee80211_regdomain_attach(ic);
345 ieee80211_dfs_attach(ic);
347 ieee80211_sysctl_attach(ic);
349 ifp->if_addrlen = IEEE80211_ADDR_LEN;
354 #if defined(__DragonFly__)
355 if_attach(ifp, &wlan_global_serializer);
360 ifp->if_mtu = IEEE80211_MTU_MAX;
361 ifp->if_broadcastaddr = ieee80211broadcastaddr;
362 ifp->if_output = null_output;
363 ifp->if_input = null_input; /* just in case */
364 ifp->if_resolvemulti = NULL; /* NB: callers check */
366 ifa = TAILQ_FIRST(&ifp->if_addrheads[mycpuid])->ifa;
367 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
368 sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */
369 sdl->sdl_alen = IEEE80211_ADDR_LEN;
370 IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
376 * Detach net80211 state on device detach. Tear down
377 * all vap's and reclaim all common state prior to the
378 * device state going away. Note we may call back into
379 * driver; it must be prepared for this.
382 ieee80211_ifdetach(struct ieee80211com *ic)
384 struct ifnet *ifp = ic->ic_ifp;
385 struct ieee80211vap *vap;
388 * This detaches the main interface, but not the vaps.
389 * Each VAP may be in a separate VIMAGE.
391 CURVNET_SET(ifp->if_vnet);
396 * The VAP is responsible for setting and clearing
397 * the VIMAGE context.
399 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
400 ieee80211_vap_destroy(vap);
401 ieee80211_waitfor_parent(ic);
403 ieee80211_sysctl_detach(ic);
404 ieee80211_dfs_detach(ic);
405 ieee80211_regdomain_detach(ic);
406 ieee80211_scan_detach(ic);
407 #ifdef IEEE80211_SUPPORT_SUPERG
408 ieee80211_superg_detach(ic);
410 ieee80211_ht_detach(ic);
411 /* NB: must be called before ieee80211_node_detach */
412 ieee80211_proto_detach(ic);
413 ieee80211_crypto_detach(ic);
414 ieee80211_power_detach(ic);
415 ieee80211_node_detach(ic);
417 /* XXX VNET needed? */
418 ifmedia_removeall(&ic->ic_media);
420 taskqueue_free(ic->ic_tq);
421 IEEE80211_TX_LOCK_DESTROY(ic);
422 IEEE80211_LOCK_DESTROY(ic);
426 * Default reset method for use with the ioctl support. This
427 * method is invoked after any state change in the 802.11
428 * layer that should be propagated to the hardware but not
429 * require re-initialization of the 802.11 state machine (e.g
430 * rescanning for an ap). We always return ENETRESET which
431 * should cause the driver to re-initialize the device. Drivers
432 * can override this method to implement more optimized support.
435 default_reset(struct ieee80211vap *vap, u_long cmd)
441 * Prepare a vap for use. Drivers use this call to
442 * setup net80211 state in new vap's prior attaching
443 * them with ieee80211_vap_attach (below).
446 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
447 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
448 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
449 const uint8_t macaddr[IEEE80211_ADDR_LEN])
453 ifp = if_alloc(IFT_ETHER);
455 if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
459 if_initname(ifp, name, unit);
460 ifp->if_softc = vap; /* back pointer */
461 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
462 ifp->if_start = ieee80211_vap_start;
464 ifp->if_transmit = ieee80211_vap_transmit;
465 ifp->if_qflush = ieee80211_vap_qflush;
467 ifp->if_ioctl = ieee80211_ioctl;
468 ifp->if_init = ieee80211_init;
472 vap->iv_flags = ic->ic_flags; /* propagate common flags */
473 vap->iv_flags_ext = ic->ic_flags_ext;
474 vap->iv_flags_ven = ic->ic_flags_ven;
475 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
476 vap->iv_htcaps = ic->ic_htcaps;
477 vap->iv_htextcaps = ic->ic_htextcaps;
478 vap->iv_opmode = opmode;
479 vap->iv_caps |= ieee80211_opcap[opmode];
481 case IEEE80211_M_WDS:
483 * WDS links must specify the bssid of the far end.
484 * For legacy operation this is a static relationship.
485 * For non-legacy operation the station must associate
486 * and be authorized to pass traffic. Plumbing the
487 * vap to the proper node happens when the vap
488 * transitions to RUN state.
490 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
491 vap->iv_flags |= IEEE80211_F_DESBSSID;
492 if (flags & IEEE80211_CLONE_WDSLEGACY)
493 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
495 #ifdef IEEE80211_SUPPORT_TDMA
496 case IEEE80211_M_AHDEMO:
497 if (flags & IEEE80211_CLONE_TDMA) {
498 /* NB: checked before clone operation allowed */
499 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
500 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
502 * Propagate TDMA capability to mark vap; this
503 * cannot be removed and is used to distinguish
504 * regular ahdemo operation from ahdemo+tdma.
506 vap->iv_caps |= IEEE80211_C_TDMA;
513 /* auto-enable s/w beacon miss support */
514 if (flags & IEEE80211_CLONE_NOBEACONS)
515 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
516 /* auto-generated or user supplied MAC address */
517 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
518 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
520 * Enable various functionality by default if we're
521 * capable; the driver can override us if it knows better.
523 if (vap->iv_caps & IEEE80211_C_WME)
524 vap->iv_flags |= IEEE80211_F_WME;
525 if (vap->iv_caps & IEEE80211_C_BURST)
526 vap->iv_flags |= IEEE80211_F_BURST;
527 /* NB: bg scanning only makes sense for station mode right now */
530 * DISABLE BGSCAN BY DEFAULT, many issues can crop up including
531 * the link going dead.
533 if (vap->iv_opmode == IEEE80211_M_STA &&
534 (vap->iv_caps & IEEE80211_C_BGSCAN))
535 vap->iv_flags |= IEEE80211_F_BGSCAN;
537 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
538 /* NB: DFS support only makes sense for ap mode right now */
539 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
540 (vap->iv_caps & IEEE80211_C_DFS))
541 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
543 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
544 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
545 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
547 * Install a default reset method for the ioctl support;
548 * the driver can override this.
550 vap->iv_reset = default_reset;
552 IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
554 ieee80211_sysctl_vattach(vap);
555 ieee80211_crypto_vattach(vap);
556 ieee80211_node_vattach(vap);
557 ieee80211_power_vattach(vap);
558 ieee80211_proto_vattach(vap);
559 #ifdef IEEE80211_SUPPORT_SUPERG
560 ieee80211_superg_vattach(vap);
562 ieee80211_ht_vattach(vap);
563 ieee80211_scan_vattach(vap);
564 ieee80211_regdomain_vattach(vap);
565 ieee80211_radiotap_vattach(vap);
566 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
572 * Activate a vap. State should have been prepared with a
573 * call to ieee80211_vap_setup and by the driver. On return
574 * from this call the vap is ready for use.
577 ieee80211_vap_attach(struct ieee80211vap *vap,
578 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
580 struct ifnet *ifp = vap->iv_ifp;
581 struct ieee80211com *ic = vap->iv_ic;
582 struct ifmediareq imr;
585 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
586 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
587 __func__, ieee80211_opmode_name[vap->iv_opmode],
588 ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
591 * Do late attach work that cannot happen until after
592 * the driver has had a chance to override defaults.
594 ieee80211_node_latevattach(vap);
595 ieee80211_power_latevattach(vap);
597 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
598 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
599 ieee80211_media_status(ifp, &imr);
600 /* NB: strip explicit mode; we're actually in autoselect */
601 ifmedia_set(&vap->iv_media,
602 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
604 ifp->if_baudrate = IF_Mbps(maxrate);
606 #if defined(__DragonFly__)
607 ether_ifattach(ifp, vap->iv_myaddr, &wlan_global_serializer);
609 ether_ifattach(ifp, vap->iv_myaddr);
611 /* hook output method setup by ether_ifattach */
612 vap->iv_output = ifp->if_output;
613 ifp->if_output = ieee80211_output;
614 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
617 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
618 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
619 #ifdef IEEE80211_SUPPORT_SUPERG
620 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
622 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
623 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
624 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
625 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
626 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
627 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
628 IEEE80211_UNLOCK(ic);
634 * Tear down vap state and reclaim the ifnet.
635 * The driver is assumed to have prepared for
636 * this; e.g. by turning off interrupts for the
640 ieee80211_vap_detach(struct ieee80211vap *vap)
642 struct ieee80211com *ic = vap->iv_ic;
643 struct ifnet *ifp = vap->iv_ifp;
645 CURVNET_SET(ifp->if_vnet);
647 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
648 __func__, ieee80211_opmode_name[vap->iv_opmode],
649 ic->ic_ifp->if_xname);
651 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
657 * Flush any deferred vap tasks.
659 ieee80211_draintask(ic, &vap->iv_nstate_task);
660 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
662 #if !defined(__DragonFly__)
663 /* XXX band-aid until ifnet handles this for us */
664 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
668 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
669 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
670 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
671 #ifdef IEEE80211_SUPPORT_SUPERG
672 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
674 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
675 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
676 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
677 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
678 /* NB: this handles the bpfdetach done below */
679 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
680 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
681 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
682 IEEE80211_UNLOCK(ic);
684 ifmedia_removeall(&vap->iv_media);
686 ieee80211_radiotap_vdetach(vap);
687 ieee80211_regdomain_vdetach(vap);
688 ieee80211_scan_vdetach(vap);
689 #ifdef IEEE80211_SUPPORT_SUPERG
690 ieee80211_superg_vdetach(vap);
692 ieee80211_ht_vdetach(vap);
693 /* NB: must be before ieee80211_node_vdetach */
694 ieee80211_proto_vdetach(vap);
695 ieee80211_crypto_vdetach(vap);
696 ieee80211_power_vdetach(vap);
697 ieee80211_node_vdetach(vap);
698 ieee80211_sysctl_vdetach(vap);
706 * Synchronize flag bit state in the parent ifnet structure
707 * according to the state of all vap ifnet's. This is used,
708 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
711 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
713 struct ifnet *ifp = ic->ic_ifp;
714 struct ieee80211vap *vap;
717 IEEE80211_LOCK_ASSERT(ic);
720 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
721 if (vap->iv_ifp->if_flags & flag) {
723 * XXX the bridge sets PROMISC but we don't want to
724 * enable it on the device, discard here so all the
725 * drivers don't need to special-case it
727 if (flag == IFF_PROMISC &&
728 !(vap->iv_opmode == IEEE80211_M_MONITOR ||
729 (vap->iv_opmode == IEEE80211_M_AHDEMO &&
730 (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
735 oflags = ifp->if_flags;
737 ifp->if_flags |= flag;
739 ifp->if_flags &= ~flag;
740 if ((ifp->if_flags ^ oflags) & flag) {
741 /* XXX should we return 1/0 and let caller do this? */
742 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
743 if (flag == IFF_PROMISC)
744 ieee80211_runtask(ic, &ic->ic_promisc_task);
745 else if (flag == IFF_ALLMULTI)
746 ieee80211_runtask(ic, &ic->ic_mcast_task);
752 * Synchronize flag bit state in the com structure
753 * according to the state of all vap's. This is used,
754 * for example, to handle state changes via ioctls.
757 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
759 struct ieee80211vap *vap;
762 IEEE80211_LOCK_ASSERT(ic);
765 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
766 if (vap->iv_flags & flag) {
771 ic->ic_flags |= flag;
773 ic->ic_flags &= ~flag;
777 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
779 struct ieee80211com *ic = vap->iv_ic;
784 vap->iv_flags &= ~flag;
786 vap->iv_flags |= flag;
787 ieee80211_syncflag_locked(ic, flag);
788 IEEE80211_UNLOCK(ic);
792 * Synchronize flags_ht bit state in the com structure
793 * according to the state of all vap's. This is used,
794 * for example, to handle state changes via ioctls.
797 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
799 struct ieee80211vap *vap;
802 IEEE80211_LOCK_ASSERT(ic);
805 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
806 if (vap->iv_flags_ht & flag) {
811 ic->ic_flags_ht |= flag;
813 ic->ic_flags_ht &= ~flag;
817 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
819 struct ieee80211com *ic = vap->iv_ic;
824 vap->iv_flags_ht &= ~flag;
826 vap->iv_flags_ht |= flag;
827 ieee80211_syncflag_ht_locked(ic, flag);
828 IEEE80211_UNLOCK(ic);
832 * Synchronize flags_ext bit state in the com structure
833 * according to the state of all vap's. This is used,
834 * for example, to handle state changes via ioctls.
837 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
839 struct ieee80211vap *vap;
842 IEEE80211_LOCK_ASSERT(ic);
845 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
846 if (vap->iv_flags_ext & flag) {
851 ic->ic_flags_ext |= flag;
853 ic->ic_flags_ext &= ~flag;
857 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
859 struct ieee80211com *ic = vap->iv_ic;
864 vap->iv_flags_ext &= ~flag;
866 vap->iv_flags_ext |= flag;
867 ieee80211_syncflag_ext_locked(ic, flag);
868 IEEE80211_UNLOCK(ic);
872 mapgsm(u_int freq, u_int flags)
875 if (flags & IEEE80211_CHAN_QUARTER)
877 else if (flags & IEEE80211_CHAN_HALF)
881 /* NB: there is no 907/20 wide but leave room */
882 return (freq - 906*10) / 5;
886 mappsb(u_int freq, u_int flags)
888 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
892 * Convert MHz frequency to IEEE channel number.
895 ieee80211_mhz2ieee(u_int freq, u_int flags)
897 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
898 if (flags & IEEE80211_CHAN_GSM)
899 return mapgsm(freq, flags);
900 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
904 return ((int) freq - 2407) / 5;
906 return 15 + ((freq - 2512) / 20);
907 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
909 /* XXX check regdomain? */
910 if (IS_FREQ_IN_PSB(freq))
911 return mappsb(freq, flags);
912 return (freq - 4000) / 5;
914 return (freq - 5000) / 5;
915 } else { /* either, guess */
919 if (907 <= freq && freq <= 922)
920 return mapgsm(freq, flags);
921 return ((int) freq - 2407) / 5;
924 if (IS_FREQ_IN_PSB(freq))
925 return mappsb(freq, flags);
926 else if (freq > 4900)
927 return (freq - 4000) / 5;
929 return 15 + ((freq - 2512) / 20);
931 return (freq - 5000) / 5;
933 #undef IS_FREQ_IN_PSB
937 * Convert channel to IEEE channel number.
940 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
943 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
946 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
950 * Convert IEEE channel number to MHz frequency.
953 ieee80211_ieee2mhz(u_int chan, u_int flags)
955 if (flags & IEEE80211_CHAN_GSM)
956 return 907 + 5 * (chan / 10);
957 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
961 return 2407 + chan*5;
963 return 2512 + ((chan-15)*20);
964 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
965 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
967 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
969 return 5000 + (chan*5);
970 } else { /* either, guess */
971 /* XXX can't distinguish PSB+GSM channels */
974 if (chan < 14) /* 0-13 */
975 return 2407 + chan*5;
976 if (chan < 27) /* 15-26 */
977 return 2512 + ((chan-15)*20);
978 return 5000 + (chan*5);
983 * Locate a channel given a frequency+flags. We cache
984 * the previous lookup to optimize switching between two
985 * channels--as happens with dynamic turbo.
987 struct ieee80211_channel *
988 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
990 struct ieee80211_channel *c;
993 flags &= IEEE80211_CHAN_ALLTURBO;
995 if (c != NULL && c->ic_freq == freq &&
996 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
998 /* brute force search */
999 for (i = 0; i < ic->ic_nchans; i++) {
1000 c = &ic->ic_channels[i];
1001 if (c->ic_freq == freq &&
1002 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1009 * Locate a channel given a channel number+flags. We cache
1010 * the previous lookup to optimize switching between two
1011 * channels--as happens with dynamic turbo.
1013 struct ieee80211_channel *
1014 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1016 struct ieee80211_channel *c;
1019 flags &= IEEE80211_CHAN_ALLTURBO;
1020 c = ic->ic_prevchan;
1021 if (c != NULL && c->ic_ieee == ieee &&
1022 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1024 /* brute force search */
1025 for (i = 0; i < ic->ic_nchans; i++) {
1026 c = &ic->ic_channels[i];
1027 if (c->ic_ieee == ieee &&
1028 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1035 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1037 #define ADD(_ic, _s, _o) \
1038 ifmedia_add(media, \
1039 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1040 static const u_int mopts[IEEE80211_MODE_MAX] = {
1041 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1042 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1043 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1044 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1045 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1046 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1047 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1048 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1049 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1050 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1051 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1052 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1058 ADD(ic, mword, mopt); /* STA mode has no cap */
1059 if (caps & IEEE80211_C_IBSS)
1060 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1061 if (caps & IEEE80211_C_HOSTAP)
1062 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1063 if (caps & IEEE80211_C_AHDEMO)
1064 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1065 if (caps & IEEE80211_C_MONITOR)
1066 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1067 if (caps & IEEE80211_C_WDS)
1068 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1069 if (caps & IEEE80211_C_MBSS)
1070 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1075 * Setup the media data structures according to the channel and
1079 ieee80211_media_setup(struct ieee80211com *ic,
1080 struct ifmedia *media, int caps, int addsta,
1081 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1083 int i, j, rate, maxrate, mword, r;
1084 enum ieee80211_phymode mode;
1085 const struct ieee80211_rateset *rs;
1086 struct ieee80211_rateset allrates;
1089 * Fill in media characteristics.
1091 ifmedia_init(media, 0, media_change, media_stat);
1094 * Add media for legacy operating modes.
1096 memset(&allrates, 0, sizeof(allrates));
1097 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1098 if (isclr(ic->ic_modecaps, mode))
1100 addmedia(media, caps, addsta, mode, IFM_AUTO);
1101 if (mode == IEEE80211_MODE_AUTO)
1103 rs = &ic->ic_sup_rates[mode];
1104 for (i = 0; i < rs->rs_nrates; i++) {
1105 rate = rs->rs_rates[i];
1106 mword = ieee80211_rate2media(ic, rate, mode);
1109 addmedia(media, caps, addsta, mode, mword);
1111 * Add legacy rate to the collection of all rates.
1113 r = rate & IEEE80211_RATE_VAL;
1114 for (j = 0; j < allrates.rs_nrates; j++)
1115 if (allrates.rs_rates[j] == r)
1117 if (j == allrates.rs_nrates) {
1118 /* unique, add to the set */
1119 allrates.rs_rates[j] = r;
1120 allrates.rs_nrates++;
1122 rate = (rate & IEEE80211_RATE_VAL) / 2;
1127 for (i = 0; i < allrates.rs_nrates; i++) {
1128 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1129 IEEE80211_MODE_AUTO);
1132 /* NB: remove media options from mword */
1133 addmedia(media, caps, addsta,
1134 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1137 * Add HT/11n media. Note that we do not have enough
1138 * bits in the media subtype to express the MCS so we
1139 * use a "placeholder" media subtype and any fixed MCS
1140 * must be specified with a different mechanism.
1142 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1143 if (isclr(ic->ic_modecaps, mode))
1145 addmedia(media, caps, addsta, mode, IFM_AUTO);
1146 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1148 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1149 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1150 addmedia(media, caps, addsta,
1151 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1152 i = ic->ic_txstream * 8 - 1;
1153 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1154 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1155 rate = ieee80211_htrates[i].ht40_rate_400ns;
1156 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1157 rate = ieee80211_htrates[i].ht40_rate_800ns;
1158 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1159 rate = ieee80211_htrates[i].ht20_rate_400ns;
1161 rate = ieee80211_htrates[i].ht20_rate_800ns;
1169 ieee80211_media_init(struct ieee80211com *ic)
1171 struct ifnet *ifp = ic->ic_ifp;
1174 /* NB: this works because the structure is initialized to zero */
1175 if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1177 * We are re-initializing the channel list; clear
1178 * the existing media state as the media routines
1179 * don't suppress duplicates.
1181 ifmedia_removeall(&ic->ic_media);
1183 ieee80211_chan_init(ic);
1186 * Recalculate media settings in case new channel list changes
1187 * the set of available modes.
1189 maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1190 ieee80211com_media_change, ieee80211com_media_status);
1191 /* NB: strip explicit mode; we're actually in autoselect */
1192 ifmedia_set(&ic->ic_media,
1193 media_status(ic->ic_opmode, ic->ic_curchan) &~
1194 (IFM_MMASK | IFM_IEEE80211_TURBO));
1196 ifp->if_baudrate = IF_Mbps(maxrate);
1198 /* XXX need to propagate new media settings to vap's */
1201 /* XXX inline or eliminate? */
1202 const struct ieee80211_rateset *
1203 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1205 /* XXX does this work for 11ng basic rates? */
1206 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1210 ieee80211_announce(struct ieee80211com *ic)
1212 struct ifnet *ifp = ic->ic_ifp;
1214 enum ieee80211_phymode mode;
1215 const struct ieee80211_rateset *rs;
1217 /* NB: skip AUTO since it has no rates */
1218 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1219 if (isclr(ic->ic_modecaps, mode))
1221 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1222 rs = &ic->ic_sup_rates[mode];
1223 for (i = 0; i < rs->rs_nrates; i++) {
1224 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1227 rate = ieee80211_media2rate(mword);
1228 kprintf("%s%d%sMbps", (i != 0 ? " " : ""),
1229 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1233 ieee80211_ht_announce(ic);
1237 ieee80211_announce_channels(struct ieee80211com *ic)
1239 const struct ieee80211_channel *c;
1243 kprintf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1244 for (i = 0; i < ic->ic_nchans; i++) {
1245 c = &ic->ic_channels[i];
1246 if (IEEE80211_IS_CHAN_ST(c))
1248 else if (IEEE80211_IS_CHAN_108A(c))
1250 else if (IEEE80211_IS_CHAN_108G(c))
1252 else if (IEEE80211_IS_CHAN_HT(c))
1254 else if (IEEE80211_IS_CHAN_A(c))
1256 else if (IEEE80211_IS_CHAN_ANYG(c))
1258 else if (IEEE80211_IS_CHAN_B(c))
1262 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1264 else if (IEEE80211_IS_CHAN_HALF(c))
1266 else if (IEEE80211_IS_CHAN_QUARTER(c))
1270 kprintf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1271 , c->ic_ieee, c->ic_freq, type
1273 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1274 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1276 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1277 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1283 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1285 switch (IFM_MODE(ime->ifm_media)) {
1286 case IFM_IEEE80211_11A:
1287 *mode = IEEE80211_MODE_11A;
1289 case IFM_IEEE80211_11B:
1290 *mode = IEEE80211_MODE_11B;
1292 case IFM_IEEE80211_11G:
1293 *mode = IEEE80211_MODE_11G;
1295 case IFM_IEEE80211_FH:
1296 *mode = IEEE80211_MODE_FH;
1298 case IFM_IEEE80211_11NA:
1299 *mode = IEEE80211_MODE_11NA;
1301 case IFM_IEEE80211_11NG:
1302 *mode = IEEE80211_MODE_11NG;
1305 *mode = IEEE80211_MODE_AUTO;
1311 * Turbo mode is an ``option''.
1312 * XXX does not apply to AUTO
1314 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1315 if (*mode == IEEE80211_MODE_11A) {
1316 if (flags & IEEE80211_F_TURBOP)
1317 *mode = IEEE80211_MODE_TURBO_A;
1319 *mode = IEEE80211_MODE_STURBO_A;
1320 } else if (*mode == IEEE80211_MODE_11G)
1321 *mode = IEEE80211_MODE_TURBO_G;
1330 * Handle a media change request on the underlying interface.
1333 ieee80211com_media_change(struct ifnet *ifp)
1339 * Handle a media change request on the vap interface.
1342 ieee80211_media_change(struct ifnet *ifp)
1344 struct ieee80211vap *vap = ifp->if_softc;
1345 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1348 if (!media2mode(ime, vap->iv_flags, &newmode))
1350 if (vap->iv_des_mode != newmode) {
1351 vap->iv_des_mode = newmode;
1352 /* XXX kick state machine if up+running */
1358 * Common code to calculate the media status word
1359 * from the operating mode and channel state.
1362 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1366 status = IFM_IEEE80211;
1368 case IEEE80211_M_STA:
1370 case IEEE80211_M_IBSS:
1371 status |= IFM_IEEE80211_ADHOC;
1373 case IEEE80211_M_HOSTAP:
1374 status |= IFM_IEEE80211_HOSTAP;
1376 case IEEE80211_M_MONITOR:
1377 status |= IFM_IEEE80211_MONITOR;
1379 case IEEE80211_M_AHDEMO:
1380 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1382 case IEEE80211_M_WDS:
1383 status |= IFM_IEEE80211_WDS;
1385 case IEEE80211_M_MBSS:
1386 status |= IFM_IEEE80211_MBSS;
1389 if (IEEE80211_IS_CHAN_HTA(chan)) {
1390 status |= IFM_IEEE80211_11NA;
1391 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1392 status |= IFM_IEEE80211_11NG;
1393 } else if (IEEE80211_IS_CHAN_A(chan)) {
1394 status |= IFM_IEEE80211_11A;
1395 } else if (IEEE80211_IS_CHAN_B(chan)) {
1396 status |= IFM_IEEE80211_11B;
1397 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1398 status |= IFM_IEEE80211_11G;
1399 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1400 status |= IFM_IEEE80211_FH;
1402 /* XXX else complain? */
1404 if (IEEE80211_IS_CHAN_TURBO(chan))
1405 status |= IFM_IEEE80211_TURBO;
1407 if (IEEE80211_IS_CHAN_HT20(chan))
1408 status |= IFM_IEEE80211_HT20;
1409 if (IEEE80211_IS_CHAN_HT40(chan))
1410 status |= IFM_IEEE80211_HT40;
1416 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1418 struct ieee80211com *ic = ifp->if_l2com;
1419 struct ieee80211vap *vap;
1421 imr->ifm_status = IFM_AVALID;
1422 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1423 if (vap->iv_ifp->if_flags & IFF_UP) {
1424 imr->ifm_status |= IFM_ACTIVE;
1427 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1428 if (imr->ifm_status & IFM_ACTIVE)
1429 imr->ifm_current = imr->ifm_active;
1433 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1435 struct ieee80211vap *vap = ifp->if_softc;
1436 struct ieee80211com *ic = vap->iv_ic;
1437 enum ieee80211_phymode mode;
1439 imr->ifm_status = IFM_AVALID;
1441 * NB: use the current channel's mode to lock down a xmit
1442 * rate only when running; otherwise we may have a mismatch
1443 * in which case the rate will not be convertible.
1445 if (vap->iv_state == IEEE80211_S_RUN ||
1446 vap->iv_state == IEEE80211_S_SLEEP) {
1447 imr->ifm_status |= IFM_ACTIVE;
1448 mode = ieee80211_chan2mode(ic->ic_curchan);
1450 mode = IEEE80211_MODE_AUTO;
1451 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1453 * Calculate a current rate if possible.
1455 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1457 * A fixed rate is set, report that.
1459 imr->ifm_active |= ieee80211_rate2media(ic,
1460 vap->iv_txparms[mode].ucastrate, mode);
1461 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1463 * In station mode report the current transmit rate.
1465 imr->ifm_active |= ieee80211_rate2media(ic,
1466 vap->iv_bss->ni_txrate, mode);
1468 imr->ifm_active |= IFM_AUTO;
1469 if (imr->ifm_status & IFM_ACTIVE)
1470 imr->ifm_current = imr->ifm_active;
1474 * Set the current phy mode and recalculate the active channel
1475 * set based on the available channels for this mode. Also
1476 * select a new default/current channel if the current one is
1477 * inappropriate for this mode.
1480 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1483 * Adjust basic rates in 11b/11g supported rate set.
1484 * Note that if operating on a hal/quarter rate channel
1485 * this is a noop as those rates sets are different
1488 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1489 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1491 ic->ic_curmode = mode;
1492 ieee80211_reset_erp(ic); /* reset ERP state */
1498 * Return the phy mode for with the specified channel.
1500 enum ieee80211_phymode
1501 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1504 if (IEEE80211_IS_CHAN_HTA(chan))
1505 return IEEE80211_MODE_11NA;
1506 else if (IEEE80211_IS_CHAN_HTG(chan))
1507 return IEEE80211_MODE_11NG;
1508 else if (IEEE80211_IS_CHAN_108G(chan))
1509 return IEEE80211_MODE_TURBO_G;
1510 else if (IEEE80211_IS_CHAN_ST(chan))
1511 return IEEE80211_MODE_STURBO_A;
1512 else if (IEEE80211_IS_CHAN_TURBO(chan))
1513 return IEEE80211_MODE_TURBO_A;
1514 else if (IEEE80211_IS_CHAN_HALF(chan))
1515 return IEEE80211_MODE_HALF;
1516 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1517 return IEEE80211_MODE_QUARTER;
1518 else if (IEEE80211_IS_CHAN_A(chan))
1519 return IEEE80211_MODE_11A;
1520 else if (IEEE80211_IS_CHAN_ANYG(chan))
1521 return IEEE80211_MODE_11G;
1522 else if (IEEE80211_IS_CHAN_B(chan))
1523 return IEEE80211_MODE_11B;
1524 else if (IEEE80211_IS_CHAN_FHSS(chan))
1525 return IEEE80211_MODE_FH;
1527 /* NB: should not get here */
1528 kprintf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1529 __func__, chan->ic_freq, chan->ic_flags);
1530 return IEEE80211_MODE_11B;
1534 u_int match; /* rate + mode */
1535 u_int media; /* if_media rate */
1539 findmedia(const struct ratemedia rates[], int n, u_int match)
1543 for (i = 0; i < n; i++)
1544 if (rates[i].match == match)
1545 return rates[i].media;
1550 * Convert IEEE80211 rate value to ifmedia subtype.
1551 * Rate is either a legacy rate in units of 0.5Mbps
1555 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1557 static const struct ratemedia rates[] = {
1558 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1559 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1560 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1561 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1562 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1563 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1564 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1565 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1566 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1567 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1568 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1569 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1570 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1571 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1572 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1573 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1574 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1575 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1576 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1577 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1578 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1579 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1580 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1581 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1582 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1583 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1584 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1585 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1586 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1587 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1588 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1590 static const struct ratemedia htrates[] = {
1591 { 0, IFM_IEEE80211_MCS },
1592 { 1, IFM_IEEE80211_MCS },
1593 { 2, IFM_IEEE80211_MCS },
1594 { 3, IFM_IEEE80211_MCS },
1595 { 4, IFM_IEEE80211_MCS },
1596 { 5, IFM_IEEE80211_MCS },
1597 { 6, IFM_IEEE80211_MCS },
1598 { 7, IFM_IEEE80211_MCS },
1599 { 8, IFM_IEEE80211_MCS },
1600 { 9, IFM_IEEE80211_MCS },
1601 { 10, IFM_IEEE80211_MCS },
1602 { 11, IFM_IEEE80211_MCS },
1603 { 12, IFM_IEEE80211_MCS },
1604 { 13, IFM_IEEE80211_MCS },
1605 { 14, IFM_IEEE80211_MCS },
1606 { 15, IFM_IEEE80211_MCS },
1607 { 16, IFM_IEEE80211_MCS },
1608 { 17, IFM_IEEE80211_MCS },
1609 { 18, IFM_IEEE80211_MCS },
1610 { 19, IFM_IEEE80211_MCS },
1611 { 20, IFM_IEEE80211_MCS },
1612 { 21, IFM_IEEE80211_MCS },
1613 { 22, IFM_IEEE80211_MCS },
1614 { 23, IFM_IEEE80211_MCS },
1615 { 24, IFM_IEEE80211_MCS },
1616 { 25, IFM_IEEE80211_MCS },
1617 { 26, IFM_IEEE80211_MCS },
1618 { 27, IFM_IEEE80211_MCS },
1619 { 28, IFM_IEEE80211_MCS },
1620 { 29, IFM_IEEE80211_MCS },
1621 { 30, IFM_IEEE80211_MCS },
1622 { 31, IFM_IEEE80211_MCS },
1623 { 32, IFM_IEEE80211_MCS },
1624 { 33, IFM_IEEE80211_MCS },
1625 { 34, IFM_IEEE80211_MCS },
1626 { 35, IFM_IEEE80211_MCS },
1627 { 36, IFM_IEEE80211_MCS },
1628 { 37, IFM_IEEE80211_MCS },
1629 { 38, IFM_IEEE80211_MCS },
1630 { 39, IFM_IEEE80211_MCS },
1631 { 40, IFM_IEEE80211_MCS },
1632 { 41, IFM_IEEE80211_MCS },
1633 { 42, IFM_IEEE80211_MCS },
1634 { 43, IFM_IEEE80211_MCS },
1635 { 44, IFM_IEEE80211_MCS },
1636 { 45, IFM_IEEE80211_MCS },
1637 { 46, IFM_IEEE80211_MCS },
1638 { 47, IFM_IEEE80211_MCS },
1639 { 48, IFM_IEEE80211_MCS },
1640 { 49, IFM_IEEE80211_MCS },
1641 { 50, IFM_IEEE80211_MCS },
1642 { 51, IFM_IEEE80211_MCS },
1643 { 52, IFM_IEEE80211_MCS },
1644 { 53, IFM_IEEE80211_MCS },
1645 { 54, IFM_IEEE80211_MCS },
1646 { 55, IFM_IEEE80211_MCS },
1647 { 56, IFM_IEEE80211_MCS },
1648 { 57, IFM_IEEE80211_MCS },
1649 { 58, IFM_IEEE80211_MCS },
1650 { 59, IFM_IEEE80211_MCS },
1651 { 60, IFM_IEEE80211_MCS },
1652 { 61, IFM_IEEE80211_MCS },
1653 { 62, IFM_IEEE80211_MCS },
1654 { 63, IFM_IEEE80211_MCS },
1655 { 64, IFM_IEEE80211_MCS },
1656 { 65, IFM_IEEE80211_MCS },
1657 { 66, IFM_IEEE80211_MCS },
1658 { 67, IFM_IEEE80211_MCS },
1659 { 68, IFM_IEEE80211_MCS },
1660 { 69, IFM_IEEE80211_MCS },
1661 { 70, IFM_IEEE80211_MCS },
1662 { 71, IFM_IEEE80211_MCS },
1663 { 72, IFM_IEEE80211_MCS },
1664 { 73, IFM_IEEE80211_MCS },
1665 { 74, IFM_IEEE80211_MCS },
1666 { 75, IFM_IEEE80211_MCS },
1667 { 76, IFM_IEEE80211_MCS },
1672 * Check 11n rates first for match as an MCS.
1674 if (mode == IEEE80211_MODE_11NA) {
1675 if (rate & IEEE80211_RATE_MCS) {
1676 rate &= ~IEEE80211_RATE_MCS;
1677 m = findmedia(htrates, nitems(htrates), rate);
1679 return m | IFM_IEEE80211_11NA;
1681 } else if (mode == IEEE80211_MODE_11NG) {
1682 /* NB: 12 is ambiguous, it will be treated as an MCS */
1683 if (rate & IEEE80211_RATE_MCS) {
1684 rate &= ~IEEE80211_RATE_MCS;
1685 m = findmedia(htrates, nitems(htrates), rate);
1687 return m | IFM_IEEE80211_11NG;
1690 rate &= IEEE80211_RATE_VAL;
1692 case IEEE80211_MODE_11A:
1693 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1694 case IEEE80211_MODE_QUARTER:
1695 case IEEE80211_MODE_11NA:
1696 case IEEE80211_MODE_TURBO_A:
1697 case IEEE80211_MODE_STURBO_A:
1698 return findmedia(rates, nitems(rates),
1699 rate | IFM_IEEE80211_11A);
1700 case IEEE80211_MODE_11B:
1701 return findmedia(rates, nitems(rates),
1702 rate | IFM_IEEE80211_11B);
1703 case IEEE80211_MODE_FH:
1704 return findmedia(rates, nitems(rates),
1705 rate | IFM_IEEE80211_FH);
1706 case IEEE80211_MODE_AUTO:
1707 /* NB: ic may be NULL for some drivers */
1708 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1709 return findmedia(rates, nitems(rates),
1710 rate | IFM_IEEE80211_FH);
1711 /* NB: hack, 11g matches both 11b+11a rates */
1713 case IEEE80211_MODE_11G:
1714 case IEEE80211_MODE_11NG:
1715 case IEEE80211_MODE_TURBO_G:
1716 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1722 ieee80211_media2rate(int mword)
1724 static const int ieeerates[] = {
1728 2, /* IFM_IEEE80211_FH1 */
1729 4, /* IFM_IEEE80211_FH2 */
1730 2, /* IFM_IEEE80211_DS1 */
1731 4, /* IFM_IEEE80211_DS2 */
1732 11, /* IFM_IEEE80211_DS5 */
1733 22, /* IFM_IEEE80211_DS11 */
1734 44, /* IFM_IEEE80211_DS22 */
1735 12, /* IFM_IEEE80211_OFDM6 */
1736 18, /* IFM_IEEE80211_OFDM9 */
1737 24, /* IFM_IEEE80211_OFDM12 */
1738 36, /* IFM_IEEE80211_OFDM18 */
1739 48, /* IFM_IEEE80211_OFDM24 */
1740 72, /* IFM_IEEE80211_OFDM36 */
1741 96, /* IFM_IEEE80211_OFDM48 */
1742 108, /* IFM_IEEE80211_OFDM54 */
1743 144, /* IFM_IEEE80211_OFDM72 */
1744 0, /* IFM_IEEE80211_DS354k */
1745 0, /* IFM_IEEE80211_DS512k */
1746 6, /* IFM_IEEE80211_OFDM3 */
1747 9, /* IFM_IEEE80211_OFDM4 */
1748 54, /* IFM_IEEE80211_OFDM27 */
1749 -1, /* IFM_IEEE80211_MCS */
1751 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1752 ieeerates[IFM_SUBTYPE(mword)] : 0;
1756 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1757 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1759 #define mix(a, b, c) \
1761 a -= b; a -= c; a ^= (c >> 13); \
1762 b -= c; b -= a; b ^= (a << 8); \
1763 c -= a; c -= b; c ^= (b >> 13); \
1764 a -= b; a -= c; a ^= (c >> 12); \
1765 b -= c; b -= a; b ^= (a << 16); \
1766 c -= a; c -= b; c ^= (b >> 5); \
1767 a -= b; a -= c; a ^= (c >> 3); \
1768 b -= c; b -= a; b ^= (a << 10); \
1769 c -= a; c -= b; c ^= (b >> 15); \
1770 } while (/*CONSTCOND*/0)
1773 ieee80211_mac_hash(const struct ieee80211com *ic,
1774 const uint8_t addr[IEEE80211_ADDR_LEN])
1776 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;