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>
38 #include <sys/socket.h>
40 #include <machine/stdarg.h>
43 #include <net/if_var.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/if_types.h>
47 #include <net/ethernet.h>
49 #include <net80211/ieee80211_var.h>
50 #include <net80211/ieee80211_regdomain.h>
51 #ifdef IEEE80211_SUPPORT_SUPERG
52 #include <net80211/ieee80211_superg.h>
54 #include <net80211/ieee80211_ratectl.h>
58 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
59 [IEEE80211_MODE_AUTO] = "auto",
60 [IEEE80211_MODE_11A] = "11a",
61 [IEEE80211_MODE_11B] = "11b",
62 [IEEE80211_MODE_11G] = "11g",
63 [IEEE80211_MODE_FH] = "FH",
64 [IEEE80211_MODE_TURBO_A] = "turboA",
65 [IEEE80211_MODE_TURBO_G] = "turboG",
66 [IEEE80211_MODE_STURBO_A] = "sturboA",
67 [IEEE80211_MODE_HALF] = "half",
68 [IEEE80211_MODE_QUARTER] = "quarter",
69 [IEEE80211_MODE_11NA] = "11na",
70 [IEEE80211_MODE_11NG] = "11ng",
72 /* map ieee80211_opmode to the corresponding capability bit */
73 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
74 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
75 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
76 [IEEE80211_M_STA] = IEEE80211_C_STA,
77 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
78 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
79 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
80 #ifdef IEEE80211_SUPPORT_MESH
81 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
85 const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
86 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
88 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
89 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
90 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
91 static int ieee80211_media_setup(struct ieee80211com *ic,
92 struct ifmedia *media, int caps, int addsta,
93 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
94 static void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
95 static int ieee80211com_media_change(struct ifnet *);
96 static int media_status(enum ieee80211_opmode,
97 const struct ieee80211_channel *);
98 static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
100 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
103 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
105 #define B(r) ((r) | IEEE80211_RATE_BASIC)
106 static const struct ieee80211_rateset ieee80211_rateset_11a =
107 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
108 static const struct ieee80211_rateset ieee80211_rateset_half =
109 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
110 static const struct ieee80211_rateset ieee80211_rateset_quarter =
111 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112 static const struct ieee80211_rateset ieee80211_rateset_11b =
113 { 4, { B(2), B(4), B(11), B(22) } };
114 /* NB: OFDM rates are handled specially based on mode */
115 static const struct ieee80211_rateset ieee80211_rateset_11g =
116 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
120 * Fill in 802.11 available channel set, mark
121 * all available channels as active, and pick
122 * a default channel if not already specified.
125 ieee80211_chan_init(struct ieee80211com *ic)
127 #define DEFAULTRATES(m, def) do { \
128 if (ic->ic_sup_rates[m].rs_nrates == 0) \
129 ic->ic_sup_rates[m] = def; \
131 struct ieee80211_channel *c;
134 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
135 ("invalid number of channels specified: %u", ic->ic_nchans));
136 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
137 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
138 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
139 for (i = 0; i < ic->ic_nchans; i++) {
140 c = &ic->ic_channels[i];
141 KASSERT(c->ic_flags != 0, ("channel with no flags"));
143 * Help drivers that work only with frequencies by filling
144 * in IEEE channel #'s if not already calculated. Note this
145 * mimics similar work done in ieee80211_setregdomain when
146 * changing regulatory state.
149 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
150 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
151 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
152 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
154 /* default max tx power to max regulatory */
155 if (c->ic_maxpower == 0)
156 c->ic_maxpower = 2*c->ic_maxregpower;
157 setbit(ic->ic_chan_avail, c->ic_ieee);
159 * Identify mode capabilities.
161 if (IEEE80211_IS_CHAN_A(c))
162 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
163 if (IEEE80211_IS_CHAN_B(c))
164 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
165 if (IEEE80211_IS_CHAN_ANYG(c))
166 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
167 if (IEEE80211_IS_CHAN_FHSS(c))
168 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
169 if (IEEE80211_IS_CHAN_108A(c))
170 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
171 if (IEEE80211_IS_CHAN_108G(c))
172 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
173 if (IEEE80211_IS_CHAN_ST(c))
174 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
175 if (IEEE80211_IS_CHAN_HALF(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
177 if (IEEE80211_IS_CHAN_QUARTER(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
179 if (IEEE80211_IS_CHAN_HTA(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
181 if (IEEE80211_IS_CHAN_HTG(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
184 /* initialize candidate channels to all available */
185 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
186 sizeof(ic->ic_chan_avail));
188 /* sort channel table to allow lookup optimizations */
189 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
191 /* invalidate any previous state */
192 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
193 ic->ic_prevchan = NULL;
194 ic->ic_csa_newchan = NULL;
195 /* arbitrarily pick the first channel */
196 ic->ic_curchan = &ic->ic_channels[0];
197 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
199 /* fillin well-known rate sets if driver has not specified */
200 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
201 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
202 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
203 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
204 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
205 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
206 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
207 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
208 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
209 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
212 * Setup required information to fill the mcsset field, if driver did
213 * not. Assume a 2T2R setup for historic reasons.
215 if (ic->ic_rxstream == 0)
217 if (ic->ic_txstream == 0)
221 * Set auto mode to reset active channel state and any desired channel.
223 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
228 null_update_mcast(struct ieee80211com *ic)
231 ic_printf(ic, "need multicast update callback\n");
235 null_update_promisc(struct ieee80211com *ic)
238 ic_printf(ic, "need promiscuous mode update callback\n");
242 null_transmit(struct ifnet *ifp, struct mbuf *m)
245 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
246 return EACCES; /* XXX EIO/EPERM? */
250 null_output(struct ifnet *ifp, struct mbuf *m,
251 const struct sockaddr *dst, struct route *ro)
253 if_printf(ifp, "discard raw packet\n");
254 return null_transmit(ifp, m);
258 null_input(struct ifnet *ifp, struct mbuf *m)
260 if_printf(ifp, "if_input should not be called\n");
265 null_update_chw(struct ieee80211com *ic)
268 ic_printf(ic, "%s: need callback\n", __func__);
272 ic_printf(struct ieee80211com *ic, const char * fmt, ...)
277 retval = printf("%s: ", ic->ic_name);
279 retval += vprintf(fmt, ap);
285 * Attach/setup the common net80211 state. Called by
286 * the driver on attach to prior to creating any vap's.
289 ieee80211_ifattach(struct ieee80211com *ic,
290 const uint8_t macaddr[IEEE80211_ADDR_LEN])
292 struct ifnet *ifp = ic->ic_ifp;
293 struct sockaddr_dl *sdl;
296 KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
298 IEEE80211_LOCK_INIT(ic, ic->ic_name);
299 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
300 TAILQ_INIT(&ic->ic_vaps);
302 /* Create a taskqueue for all state changes */
303 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
304 taskqueue_thread_enqueue, &ic->ic_tq);
305 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
307 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
308 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
310 * Fill in 802.11 available channel set, mark all
311 * available channels as active, and pick a default
312 * channel if not already specified.
314 ieee80211_media_init(ic);
316 ic->ic_update_mcast = null_update_mcast;
317 ic->ic_update_promisc = null_update_promisc;
318 ic->ic_update_chw = null_update_chw;
320 ic->ic_hash_key = arc4random();
321 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
322 ic->ic_lintval = ic->ic_bintval;
323 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
325 ieee80211_crypto_attach(ic);
326 ieee80211_node_attach(ic);
327 ieee80211_power_attach(ic);
328 ieee80211_proto_attach(ic);
329 #ifdef IEEE80211_SUPPORT_SUPERG
330 ieee80211_superg_attach(ic);
332 ieee80211_ht_attach(ic);
333 ieee80211_scan_attach(ic);
334 ieee80211_regdomain_attach(ic);
335 ieee80211_dfs_attach(ic);
337 ieee80211_sysctl_attach(ic);
339 ifp->if_addrlen = IEEE80211_ADDR_LEN;
346 ifp->if_mtu = IEEE80211_MTU_MAX;
347 ifp->if_broadcastaddr = ieee80211broadcastaddr;
348 ifp->if_output = null_output;
349 ifp->if_input = null_input; /* just in case */
350 ifp->if_resolvemulti = NULL; /* NB: callers check */
352 ifa = ifaddr_byindex(ifp->if_index);
353 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
354 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
355 sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */
356 sdl->sdl_alen = IEEE80211_ADDR_LEN;
357 IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
364 * Detach net80211 state on device detach. Tear down
365 * all vap's and reclaim all common state prior to the
366 * device state going away. Note we may call back into
367 * driver; it must be prepared for this.
370 ieee80211_ifdetach(struct ieee80211com *ic)
372 struct ifnet *ifp = ic->ic_ifp;
373 struct ieee80211vap *vap;
376 * This detaches the main interface, but not the vaps.
377 * Each VAP may be in a separate VIMAGE.
379 CURVNET_SET(ifp->if_vnet);
384 * The VAP is responsible for setting and clearing
385 * the VIMAGE context.
387 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
388 ieee80211_vap_destroy(vap);
389 ieee80211_waitfor_parent(ic);
391 ieee80211_sysctl_detach(ic);
392 ieee80211_dfs_detach(ic);
393 ieee80211_regdomain_detach(ic);
394 ieee80211_scan_detach(ic);
395 #ifdef IEEE80211_SUPPORT_SUPERG
396 ieee80211_superg_detach(ic);
398 ieee80211_ht_detach(ic);
399 /* NB: must be called before ieee80211_node_detach */
400 ieee80211_proto_detach(ic);
401 ieee80211_crypto_detach(ic);
402 ieee80211_power_detach(ic);
403 ieee80211_node_detach(ic);
405 /* XXX VNET needed? */
406 ifmedia_removeall(&ic->ic_media);
407 counter_u64_free(ic->ic_ierrors);
408 counter_u64_free(ic->ic_oerrors);
410 taskqueue_free(ic->ic_tq);
411 IEEE80211_TX_LOCK_DESTROY(ic);
412 IEEE80211_LOCK_DESTROY(ic);
416 * Default reset method for use with the ioctl support. This
417 * method is invoked after any state change in the 802.11
418 * layer that should be propagated to the hardware but not
419 * require re-initialization of the 802.11 state machine (e.g
420 * rescanning for an ap). We always return ENETRESET which
421 * should cause the driver to re-initialize the device. Drivers
422 * can override this method to implement more optimized support.
425 default_reset(struct ieee80211vap *vap, u_long cmd)
431 * Add underlying device errors to vap errors.
434 ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
436 struct ieee80211vap *vap = ifp->if_softc;
437 struct ieee80211com *ic = vap->iv_ic;
440 rv = if_get_counter_default(ifp, cnt);
442 case IFCOUNTER_OERRORS:
443 rv += counter_u64_fetch(ic->ic_oerrors);
445 case IFCOUNTER_IERRORS:
446 rv += counter_u64_fetch(ic->ic_ierrors);
456 * Prepare a vap for use. Drivers use this call to
457 * setup net80211 state in new vap's prior attaching
458 * them with ieee80211_vap_attach (below).
461 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
462 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
463 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN],
464 const uint8_t macaddr[IEEE80211_ADDR_LEN])
468 ifp = if_alloc(IFT_ETHER);
470 ic_printf(ic, "%s: unable to allocate ifnet\n",
474 if_initname(ifp, name, unit);
475 ifp->if_softc = vap; /* back pointer */
476 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
477 ifp->if_transmit = ieee80211_vap_transmit;
478 ifp->if_qflush = ieee80211_vap_qflush;
479 ifp->if_ioctl = ieee80211_ioctl;
480 ifp->if_init = ieee80211_init;
481 ifp->if_get_counter = ieee80211_get_counter;
485 vap->iv_flags = ic->ic_flags; /* propagate common flags */
486 vap->iv_flags_ext = ic->ic_flags_ext;
487 vap->iv_flags_ven = ic->ic_flags_ven;
488 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
489 vap->iv_htcaps = ic->ic_htcaps;
490 vap->iv_htextcaps = ic->ic_htextcaps;
491 vap->iv_opmode = opmode;
492 vap->iv_caps |= ieee80211_opcap[opmode];
494 case IEEE80211_M_WDS:
496 * WDS links must specify the bssid of the far end.
497 * For legacy operation this is a static relationship.
498 * For non-legacy operation the station must associate
499 * and be authorized to pass traffic. Plumbing the
500 * vap to the proper node happens when the vap
501 * transitions to RUN state.
503 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
504 vap->iv_flags |= IEEE80211_F_DESBSSID;
505 if (flags & IEEE80211_CLONE_WDSLEGACY)
506 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
508 #ifdef IEEE80211_SUPPORT_TDMA
509 case IEEE80211_M_AHDEMO:
510 if (flags & IEEE80211_CLONE_TDMA) {
511 /* NB: checked before clone operation allowed */
512 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
513 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
515 * Propagate TDMA capability to mark vap; this
516 * cannot be removed and is used to distinguish
517 * regular ahdemo operation from ahdemo+tdma.
519 vap->iv_caps |= IEEE80211_C_TDMA;
526 /* auto-enable s/w beacon miss support */
527 if (flags & IEEE80211_CLONE_NOBEACONS)
528 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
529 /* auto-generated or user supplied MAC address */
530 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
531 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
533 * Enable various functionality by default if we're
534 * capable; the driver can override us if it knows better.
536 if (vap->iv_caps & IEEE80211_C_WME)
537 vap->iv_flags |= IEEE80211_F_WME;
538 if (vap->iv_caps & IEEE80211_C_BURST)
539 vap->iv_flags |= IEEE80211_F_BURST;
540 /* NB: bg scanning only makes sense for station mode right now */
541 if (vap->iv_opmode == IEEE80211_M_STA &&
542 (vap->iv_caps & IEEE80211_C_BGSCAN))
543 vap->iv_flags |= IEEE80211_F_BGSCAN;
544 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
545 /* NB: DFS support only makes sense for ap mode right now */
546 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
547 (vap->iv_caps & IEEE80211_C_DFS))
548 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
550 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
551 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
552 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
554 * Install a default reset method for the ioctl support;
555 * the driver can override this.
557 vap->iv_reset = default_reset;
559 IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
561 ieee80211_sysctl_vattach(vap);
562 ieee80211_crypto_vattach(vap);
563 ieee80211_node_vattach(vap);
564 ieee80211_power_vattach(vap);
565 ieee80211_proto_vattach(vap);
566 #ifdef IEEE80211_SUPPORT_SUPERG
567 ieee80211_superg_vattach(vap);
569 ieee80211_ht_vattach(vap);
570 ieee80211_scan_vattach(vap);
571 ieee80211_regdomain_vattach(vap);
572 ieee80211_radiotap_vattach(vap);
573 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
579 * Activate a vap. State should have been prepared with a
580 * call to ieee80211_vap_setup and by the driver. On return
581 * from this call the vap is ready for use.
584 ieee80211_vap_attach(struct ieee80211vap *vap,
585 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
587 struct ifnet *ifp = vap->iv_ifp;
588 struct ieee80211com *ic = vap->iv_ic;
589 struct ifmediareq imr;
592 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
593 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
594 __func__, ieee80211_opmode_name[vap->iv_opmode],
595 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
598 * Do late attach work that cannot happen until after
599 * the driver has had a chance to override defaults.
601 ieee80211_node_latevattach(vap);
602 ieee80211_power_latevattach(vap);
604 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
605 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
606 ieee80211_media_status(ifp, &imr);
607 /* NB: strip explicit mode; we're actually in autoselect */
608 ifmedia_set(&vap->iv_media,
609 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
611 ifp->if_baudrate = IF_Mbps(maxrate);
613 ether_ifattach(ifp, vap->iv_myaddr);
614 /* hook output method setup by ether_ifattach */
615 vap->iv_output = ifp->if_output;
616 ifp->if_output = ieee80211_output;
617 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
620 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
621 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
622 #ifdef IEEE80211_SUPPORT_SUPERG
623 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
625 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
626 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
627 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
628 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
629 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
630 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
631 IEEE80211_UNLOCK(ic);
637 * Tear down vap state and reclaim the ifnet.
638 * The driver is assumed to have prepared for
639 * this; e.g. by turning off interrupts for the
643 ieee80211_vap_detach(struct ieee80211vap *vap)
645 struct ieee80211com *ic = vap->iv_ic;
646 struct ifnet *ifp = vap->iv_ifp;
648 CURVNET_SET(ifp->if_vnet);
650 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
651 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
653 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
659 * Flush any deferred vap tasks.
661 ieee80211_draintask(ic, &vap->iv_nstate_task);
662 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
664 /* XXX band-aid until ifnet handles this for us */
665 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 ic_printf(ic, "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 * Lookup a channel suitable for the given rx status.
1037 * This is used to find a channel for a frame (eg beacon, probe
1038 * response) based purely on the received PHY information.
1040 * For now it tries to do it based on R_FREQ / R_IEEE.
1041 * This is enough for 11bg and 11a (and thus 11ng/11na)
1042 * but it will not be enough for GSM, PSB channels and the
1043 * like. It also doesn't know about legacy-turbog and
1044 * legacy-turbo modes, which some offload NICs actually
1045 * support in weird ways.
1047 * Takes the ic and rxstatus; returns the channel or NULL
1050 * XXX TODO: Add support for that when the need arises.
1052 struct ieee80211_channel *
1053 ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1054 const struct ieee80211_rx_stats *rxs)
1056 struct ieee80211com *ic = vap->iv_ic;
1058 struct ieee80211_channel *c;
1064 * Strictly speaking we only use freq for now,
1065 * however later on we may wish to just store
1066 * the ieee for verification.
1068 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1070 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1074 * If the rx status contains a valid ieee/freq, then
1075 * ensure we populate the correct channel information
1076 * in rxchan before passing it up to the scan infrastructure.
1077 * Offload NICs will pass up beacons from all channels
1078 * during background scans.
1081 /* Determine a band */
1082 /* XXX should be done by the driver? */
1083 if (rxs->c_freq < 3000) {
1084 flags = IEEE80211_CHAN_B;
1086 flags = IEEE80211_CHAN_A;
1089 /* Channel lookup */
1090 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1092 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1093 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1104 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1106 #define ADD(_ic, _s, _o) \
1107 ifmedia_add(media, \
1108 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1109 static const u_int mopts[IEEE80211_MODE_MAX] = {
1110 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1111 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1112 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1113 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1114 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1115 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1116 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1117 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1118 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1119 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1120 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1121 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1127 ADD(ic, mword, mopt); /* STA mode has no cap */
1128 if (caps & IEEE80211_C_IBSS)
1129 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1130 if (caps & IEEE80211_C_HOSTAP)
1131 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1132 if (caps & IEEE80211_C_AHDEMO)
1133 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1134 if (caps & IEEE80211_C_MONITOR)
1135 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1136 if (caps & IEEE80211_C_WDS)
1137 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1138 if (caps & IEEE80211_C_MBSS)
1139 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1144 * Setup the media data structures according to the channel and
1148 ieee80211_media_setup(struct ieee80211com *ic,
1149 struct ifmedia *media, int caps, int addsta,
1150 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1152 int i, j, rate, maxrate, mword, r;
1153 enum ieee80211_phymode mode;
1154 const struct ieee80211_rateset *rs;
1155 struct ieee80211_rateset allrates;
1158 * Fill in media characteristics.
1160 ifmedia_init(media, 0, media_change, media_stat);
1163 * Add media for legacy operating modes.
1165 memset(&allrates, 0, sizeof(allrates));
1166 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1167 if (isclr(ic->ic_modecaps, mode))
1169 addmedia(media, caps, addsta, mode, IFM_AUTO);
1170 if (mode == IEEE80211_MODE_AUTO)
1172 rs = &ic->ic_sup_rates[mode];
1173 for (i = 0; i < rs->rs_nrates; i++) {
1174 rate = rs->rs_rates[i];
1175 mword = ieee80211_rate2media(ic, rate, mode);
1178 addmedia(media, caps, addsta, mode, mword);
1180 * Add legacy rate to the collection of all rates.
1182 r = rate & IEEE80211_RATE_VAL;
1183 for (j = 0; j < allrates.rs_nrates; j++)
1184 if (allrates.rs_rates[j] == r)
1186 if (j == allrates.rs_nrates) {
1187 /* unique, add to the set */
1188 allrates.rs_rates[j] = r;
1189 allrates.rs_nrates++;
1191 rate = (rate & IEEE80211_RATE_VAL) / 2;
1196 for (i = 0; i < allrates.rs_nrates; i++) {
1197 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1198 IEEE80211_MODE_AUTO);
1201 /* NB: remove media options from mword */
1202 addmedia(media, caps, addsta,
1203 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1206 * Add HT/11n media. Note that we do not have enough
1207 * bits in the media subtype to express the MCS so we
1208 * use a "placeholder" media subtype and any fixed MCS
1209 * must be specified with a different mechanism.
1211 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1212 if (isclr(ic->ic_modecaps, mode))
1214 addmedia(media, caps, addsta, mode, IFM_AUTO);
1215 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1217 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1218 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1219 addmedia(media, caps, addsta,
1220 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1221 i = ic->ic_txstream * 8 - 1;
1222 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1223 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1224 rate = ieee80211_htrates[i].ht40_rate_400ns;
1225 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1226 rate = ieee80211_htrates[i].ht40_rate_800ns;
1227 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1228 rate = ieee80211_htrates[i].ht20_rate_400ns;
1230 rate = ieee80211_htrates[i].ht20_rate_800ns;
1238 ieee80211_media_init(struct ieee80211com *ic)
1240 struct ifnet *ifp = ic->ic_ifp;
1243 /* NB: this works because the structure is initialized to zero */
1244 if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1246 * We are re-initializing the channel list; clear
1247 * the existing media state as the media routines
1248 * don't suppress duplicates.
1250 ifmedia_removeall(&ic->ic_media);
1252 ieee80211_chan_init(ic);
1255 * Recalculate media settings in case new channel list changes
1256 * the set of available modes.
1258 maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1259 ieee80211com_media_change, ieee80211com_media_status);
1260 /* NB: strip explicit mode; we're actually in autoselect */
1261 ifmedia_set(&ic->ic_media,
1262 media_status(ic->ic_opmode, ic->ic_curchan) &~
1263 (IFM_MMASK | IFM_IEEE80211_TURBO));
1265 ifp->if_baudrate = IF_Mbps(maxrate);
1267 /* XXX need to propagate new media settings to vap's */
1270 /* XXX inline or eliminate? */
1271 const struct ieee80211_rateset *
1272 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1274 /* XXX does this work for 11ng basic rates? */
1275 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1279 ieee80211_announce(struct ieee80211com *ic)
1282 enum ieee80211_phymode mode;
1283 const struct ieee80211_rateset *rs;
1285 /* NB: skip AUTO since it has no rates */
1286 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1287 if (isclr(ic->ic_modecaps, mode))
1289 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1290 rs = &ic->ic_sup_rates[mode];
1291 for (i = 0; i < rs->rs_nrates; i++) {
1292 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1295 rate = ieee80211_media2rate(mword);
1296 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1297 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1301 ieee80211_ht_announce(ic);
1305 ieee80211_announce_channels(struct ieee80211com *ic)
1307 const struct ieee80211_channel *c;
1311 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1312 for (i = 0; i < ic->ic_nchans; i++) {
1313 c = &ic->ic_channels[i];
1314 if (IEEE80211_IS_CHAN_ST(c))
1316 else if (IEEE80211_IS_CHAN_108A(c))
1318 else if (IEEE80211_IS_CHAN_108G(c))
1320 else if (IEEE80211_IS_CHAN_HT(c))
1322 else if (IEEE80211_IS_CHAN_A(c))
1324 else if (IEEE80211_IS_CHAN_ANYG(c))
1326 else if (IEEE80211_IS_CHAN_B(c))
1330 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1332 else if (IEEE80211_IS_CHAN_HALF(c))
1334 else if (IEEE80211_IS_CHAN_QUARTER(c))
1338 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1339 , c->ic_ieee, c->ic_freq, type
1341 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1342 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1344 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1345 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1351 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1353 switch (IFM_MODE(ime->ifm_media)) {
1354 case IFM_IEEE80211_11A:
1355 *mode = IEEE80211_MODE_11A;
1357 case IFM_IEEE80211_11B:
1358 *mode = IEEE80211_MODE_11B;
1360 case IFM_IEEE80211_11G:
1361 *mode = IEEE80211_MODE_11G;
1363 case IFM_IEEE80211_FH:
1364 *mode = IEEE80211_MODE_FH;
1366 case IFM_IEEE80211_11NA:
1367 *mode = IEEE80211_MODE_11NA;
1369 case IFM_IEEE80211_11NG:
1370 *mode = IEEE80211_MODE_11NG;
1373 *mode = IEEE80211_MODE_AUTO;
1379 * Turbo mode is an ``option''.
1380 * XXX does not apply to AUTO
1382 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1383 if (*mode == IEEE80211_MODE_11A) {
1384 if (flags & IEEE80211_F_TURBOP)
1385 *mode = IEEE80211_MODE_TURBO_A;
1387 *mode = IEEE80211_MODE_STURBO_A;
1388 } else if (*mode == IEEE80211_MODE_11G)
1389 *mode = IEEE80211_MODE_TURBO_G;
1398 * Handle a media change request on the underlying interface.
1401 ieee80211com_media_change(struct ifnet *ifp)
1407 * Handle a media change request on the vap interface.
1410 ieee80211_media_change(struct ifnet *ifp)
1412 struct ieee80211vap *vap = ifp->if_softc;
1413 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1416 if (!media2mode(ime, vap->iv_flags, &newmode))
1418 if (vap->iv_des_mode != newmode) {
1419 vap->iv_des_mode = newmode;
1420 /* XXX kick state machine if up+running */
1426 * Common code to calculate the media status word
1427 * from the operating mode and channel state.
1430 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1434 status = IFM_IEEE80211;
1436 case IEEE80211_M_STA:
1438 case IEEE80211_M_IBSS:
1439 status |= IFM_IEEE80211_ADHOC;
1441 case IEEE80211_M_HOSTAP:
1442 status |= IFM_IEEE80211_HOSTAP;
1444 case IEEE80211_M_MONITOR:
1445 status |= IFM_IEEE80211_MONITOR;
1447 case IEEE80211_M_AHDEMO:
1448 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1450 case IEEE80211_M_WDS:
1451 status |= IFM_IEEE80211_WDS;
1453 case IEEE80211_M_MBSS:
1454 status |= IFM_IEEE80211_MBSS;
1457 if (IEEE80211_IS_CHAN_HTA(chan)) {
1458 status |= IFM_IEEE80211_11NA;
1459 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1460 status |= IFM_IEEE80211_11NG;
1461 } else if (IEEE80211_IS_CHAN_A(chan)) {
1462 status |= IFM_IEEE80211_11A;
1463 } else if (IEEE80211_IS_CHAN_B(chan)) {
1464 status |= IFM_IEEE80211_11B;
1465 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1466 status |= IFM_IEEE80211_11G;
1467 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1468 status |= IFM_IEEE80211_FH;
1470 /* XXX else complain? */
1472 if (IEEE80211_IS_CHAN_TURBO(chan))
1473 status |= IFM_IEEE80211_TURBO;
1475 if (IEEE80211_IS_CHAN_HT20(chan))
1476 status |= IFM_IEEE80211_HT20;
1477 if (IEEE80211_IS_CHAN_HT40(chan))
1478 status |= IFM_IEEE80211_HT40;
1484 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1486 struct ieee80211com *ic = ifp->if_l2com;
1487 struct ieee80211vap *vap;
1489 imr->ifm_status = IFM_AVALID;
1490 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1491 if (vap->iv_ifp->if_flags & IFF_UP) {
1492 imr->ifm_status |= IFM_ACTIVE;
1495 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1496 if (imr->ifm_status & IFM_ACTIVE)
1497 imr->ifm_current = imr->ifm_active;
1501 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1503 struct ieee80211vap *vap = ifp->if_softc;
1504 struct ieee80211com *ic = vap->iv_ic;
1505 enum ieee80211_phymode mode;
1507 imr->ifm_status = IFM_AVALID;
1509 * NB: use the current channel's mode to lock down a xmit
1510 * rate only when running; otherwise we may have a mismatch
1511 * in which case the rate will not be convertible.
1513 if (vap->iv_state == IEEE80211_S_RUN ||
1514 vap->iv_state == IEEE80211_S_SLEEP) {
1515 imr->ifm_status |= IFM_ACTIVE;
1516 mode = ieee80211_chan2mode(ic->ic_curchan);
1518 mode = IEEE80211_MODE_AUTO;
1519 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1521 * Calculate a current rate if possible.
1523 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1525 * A fixed rate is set, report that.
1527 imr->ifm_active |= ieee80211_rate2media(ic,
1528 vap->iv_txparms[mode].ucastrate, mode);
1529 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1531 * In station mode report the current transmit rate.
1533 imr->ifm_active |= ieee80211_rate2media(ic,
1534 vap->iv_bss->ni_txrate, mode);
1536 imr->ifm_active |= IFM_AUTO;
1537 if (imr->ifm_status & IFM_ACTIVE)
1538 imr->ifm_current = imr->ifm_active;
1542 * Set the current phy mode and recalculate the active channel
1543 * set based on the available channels for this mode. Also
1544 * select a new default/current channel if the current one is
1545 * inappropriate for this mode.
1548 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1551 * Adjust basic rates in 11b/11g supported rate set.
1552 * Note that if operating on a hal/quarter rate channel
1553 * this is a noop as those rates sets are different
1556 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1557 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1559 ic->ic_curmode = mode;
1560 ieee80211_reset_erp(ic); /* reset ERP state */
1566 * Return the phy mode for with the specified channel.
1568 enum ieee80211_phymode
1569 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1572 if (IEEE80211_IS_CHAN_HTA(chan))
1573 return IEEE80211_MODE_11NA;
1574 else if (IEEE80211_IS_CHAN_HTG(chan))
1575 return IEEE80211_MODE_11NG;
1576 else if (IEEE80211_IS_CHAN_108G(chan))
1577 return IEEE80211_MODE_TURBO_G;
1578 else if (IEEE80211_IS_CHAN_ST(chan))
1579 return IEEE80211_MODE_STURBO_A;
1580 else if (IEEE80211_IS_CHAN_TURBO(chan))
1581 return IEEE80211_MODE_TURBO_A;
1582 else if (IEEE80211_IS_CHAN_HALF(chan))
1583 return IEEE80211_MODE_HALF;
1584 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1585 return IEEE80211_MODE_QUARTER;
1586 else if (IEEE80211_IS_CHAN_A(chan))
1587 return IEEE80211_MODE_11A;
1588 else if (IEEE80211_IS_CHAN_ANYG(chan))
1589 return IEEE80211_MODE_11G;
1590 else if (IEEE80211_IS_CHAN_B(chan))
1591 return IEEE80211_MODE_11B;
1592 else if (IEEE80211_IS_CHAN_FHSS(chan))
1593 return IEEE80211_MODE_FH;
1595 /* NB: should not get here */
1596 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1597 __func__, chan->ic_freq, chan->ic_flags);
1598 return IEEE80211_MODE_11B;
1602 u_int match; /* rate + mode */
1603 u_int media; /* if_media rate */
1607 findmedia(const struct ratemedia rates[], int n, u_int match)
1611 for (i = 0; i < n; i++)
1612 if (rates[i].match == match)
1613 return rates[i].media;
1618 * Convert IEEE80211 rate value to ifmedia subtype.
1619 * Rate is either a legacy rate in units of 0.5Mbps
1623 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1625 static const struct ratemedia rates[] = {
1626 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1627 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1628 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1629 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1630 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1631 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1632 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1633 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1634 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1635 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1636 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1637 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1638 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1639 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1640 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1641 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1642 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1643 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1644 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1645 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1646 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1647 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1648 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1649 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1650 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1651 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1652 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1653 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1654 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1655 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1656 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1658 static const struct ratemedia htrates[] = {
1659 { 0, IFM_IEEE80211_MCS },
1660 { 1, IFM_IEEE80211_MCS },
1661 { 2, IFM_IEEE80211_MCS },
1662 { 3, IFM_IEEE80211_MCS },
1663 { 4, IFM_IEEE80211_MCS },
1664 { 5, IFM_IEEE80211_MCS },
1665 { 6, IFM_IEEE80211_MCS },
1666 { 7, IFM_IEEE80211_MCS },
1667 { 8, IFM_IEEE80211_MCS },
1668 { 9, IFM_IEEE80211_MCS },
1669 { 10, IFM_IEEE80211_MCS },
1670 { 11, IFM_IEEE80211_MCS },
1671 { 12, IFM_IEEE80211_MCS },
1672 { 13, IFM_IEEE80211_MCS },
1673 { 14, IFM_IEEE80211_MCS },
1674 { 15, IFM_IEEE80211_MCS },
1675 { 16, IFM_IEEE80211_MCS },
1676 { 17, IFM_IEEE80211_MCS },
1677 { 18, IFM_IEEE80211_MCS },
1678 { 19, IFM_IEEE80211_MCS },
1679 { 20, IFM_IEEE80211_MCS },
1680 { 21, IFM_IEEE80211_MCS },
1681 { 22, IFM_IEEE80211_MCS },
1682 { 23, IFM_IEEE80211_MCS },
1683 { 24, IFM_IEEE80211_MCS },
1684 { 25, IFM_IEEE80211_MCS },
1685 { 26, IFM_IEEE80211_MCS },
1686 { 27, IFM_IEEE80211_MCS },
1687 { 28, IFM_IEEE80211_MCS },
1688 { 29, IFM_IEEE80211_MCS },
1689 { 30, IFM_IEEE80211_MCS },
1690 { 31, IFM_IEEE80211_MCS },
1691 { 32, IFM_IEEE80211_MCS },
1692 { 33, IFM_IEEE80211_MCS },
1693 { 34, IFM_IEEE80211_MCS },
1694 { 35, IFM_IEEE80211_MCS },
1695 { 36, IFM_IEEE80211_MCS },
1696 { 37, IFM_IEEE80211_MCS },
1697 { 38, IFM_IEEE80211_MCS },
1698 { 39, IFM_IEEE80211_MCS },
1699 { 40, IFM_IEEE80211_MCS },
1700 { 41, IFM_IEEE80211_MCS },
1701 { 42, IFM_IEEE80211_MCS },
1702 { 43, IFM_IEEE80211_MCS },
1703 { 44, IFM_IEEE80211_MCS },
1704 { 45, IFM_IEEE80211_MCS },
1705 { 46, IFM_IEEE80211_MCS },
1706 { 47, IFM_IEEE80211_MCS },
1707 { 48, IFM_IEEE80211_MCS },
1708 { 49, IFM_IEEE80211_MCS },
1709 { 50, IFM_IEEE80211_MCS },
1710 { 51, IFM_IEEE80211_MCS },
1711 { 52, IFM_IEEE80211_MCS },
1712 { 53, IFM_IEEE80211_MCS },
1713 { 54, IFM_IEEE80211_MCS },
1714 { 55, IFM_IEEE80211_MCS },
1715 { 56, IFM_IEEE80211_MCS },
1716 { 57, IFM_IEEE80211_MCS },
1717 { 58, IFM_IEEE80211_MCS },
1718 { 59, IFM_IEEE80211_MCS },
1719 { 60, IFM_IEEE80211_MCS },
1720 { 61, IFM_IEEE80211_MCS },
1721 { 62, IFM_IEEE80211_MCS },
1722 { 63, IFM_IEEE80211_MCS },
1723 { 64, IFM_IEEE80211_MCS },
1724 { 65, IFM_IEEE80211_MCS },
1725 { 66, IFM_IEEE80211_MCS },
1726 { 67, IFM_IEEE80211_MCS },
1727 { 68, IFM_IEEE80211_MCS },
1728 { 69, IFM_IEEE80211_MCS },
1729 { 70, IFM_IEEE80211_MCS },
1730 { 71, IFM_IEEE80211_MCS },
1731 { 72, IFM_IEEE80211_MCS },
1732 { 73, IFM_IEEE80211_MCS },
1733 { 74, IFM_IEEE80211_MCS },
1734 { 75, IFM_IEEE80211_MCS },
1735 { 76, IFM_IEEE80211_MCS },
1740 * Check 11n rates first for match as an MCS.
1742 if (mode == IEEE80211_MODE_11NA) {
1743 if (rate & IEEE80211_RATE_MCS) {
1744 rate &= ~IEEE80211_RATE_MCS;
1745 m = findmedia(htrates, nitems(htrates), rate);
1747 return m | IFM_IEEE80211_11NA;
1749 } else if (mode == IEEE80211_MODE_11NG) {
1750 /* NB: 12 is ambiguous, it will be treated as an MCS */
1751 if (rate & IEEE80211_RATE_MCS) {
1752 rate &= ~IEEE80211_RATE_MCS;
1753 m = findmedia(htrates, nitems(htrates), rate);
1755 return m | IFM_IEEE80211_11NG;
1758 rate &= IEEE80211_RATE_VAL;
1760 case IEEE80211_MODE_11A:
1761 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1762 case IEEE80211_MODE_QUARTER:
1763 case IEEE80211_MODE_11NA:
1764 case IEEE80211_MODE_TURBO_A:
1765 case IEEE80211_MODE_STURBO_A:
1766 return findmedia(rates, nitems(rates),
1767 rate | IFM_IEEE80211_11A);
1768 case IEEE80211_MODE_11B:
1769 return findmedia(rates, nitems(rates),
1770 rate | IFM_IEEE80211_11B);
1771 case IEEE80211_MODE_FH:
1772 return findmedia(rates, nitems(rates),
1773 rate | IFM_IEEE80211_FH);
1774 case IEEE80211_MODE_AUTO:
1775 /* NB: ic may be NULL for some drivers */
1776 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1777 return findmedia(rates, nitems(rates),
1778 rate | IFM_IEEE80211_FH);
1779 /* NB: hack, 11g matches both 11b+11a rates */
1781 case IEEE80211_MODE_11G:
1782 case IEEE80211_MODE_11NG:
1783 case IEEE80211_MODE_TURBO_G:
1784 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1790 ieee80211_media2rate(int mword)
1792 static const int ieeerates[] = {
1796 2, /* IFM_IEEE80211_FH1 */
1797 4, /* IFM_IEEE80211_FH2 */
1798 2, /* IFM_IEEE80211_DS1 */
1799 4, /* IFM_IEEE80211_DS2 */
1800 11, /* IFM_IEEE80211_DS5 */
1801 22, /* IFM_IEEE80211_DS11 */
1802 44, /* IFM_IEEE80211_DS22 */
1803 12, /* IFM_IEEE80211_OFDM6 */
1804 18, /* IFM_IEEE80211_OFDM9 */
1805 24, /* IFM_IEEE80211_OFDM12 */
1806 36, /* IFM_IEEE80211_OFDM18 */
1807 48, /* IFM_IEEE80211_OFDM24 */
1808 72, /* IFM_IEEE80211_OFDM36 */
1809 96, /* IFM_IEEE80211_OFDM48 */
1810 108, /* IFM_IEEE80211_OFDM54 */
1811 144, /* IFM_IEEE80211_OFDM72 */
1812 0, /* IFM_IEEE80211_DS354k */
1813 0, /* IFM_IEEE80211_DS512k */
1814 6, /* IFM_IEEE80211_OFDM3 */
1815 9, /* IFM_IEEE80211_OFDM4 */
1816 54, /* IFM_IEEE80211_OFDM27 */
1817 -1, /* IFM_IEEE80211_MCS */
1819 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
1820 ieeerates[IFM_SUBTYPE(mword)] : 0;
1824 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1825 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1827 #define mix(a, b, c) \
1829 a -= b; a -= c; a ^= (c >> 13); \
1830 b -= c; b -= a; b ^= (a << 8); \
1831 c -= a; c -= b; c ^= (b >> 13); \
1832 a -= b; a -= c; a ^= (c >> 12); \
1833 b -= c; b -= a; b ^= (a << 16); \
1834 c -= a; c -= b; c ^= (b >> 5); \
1835 a -= b; a -= c; a ^= (c >> 3); \
1836 b -= c; b -= a; b ^= (a << 10); \
1837 c -= a; c -= b; c ^= (b >> 15); \
1838 } while (/*CONSTCOND*/0)
1841 ieee80211_mac_hash(const struct ieee80211com *ic,
1842 const uint8_t addr[IEEE80211_ADDR_LEN])
1844 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1860 ieee80211_channel_type_char(const struct ieee80211_channel *c)
1862 if (IEEE80211_IS_CHAN_ST(c))
1864 if (IEEE80211_IS_CHAN_108A(c))
1866 if (IEEE80211_IS_CHAN_108G(c))
1868 if (IEEE80211_IS_CHAN_HT(c))
1870 if (IEEE80211_IS_CHAN_A(c))
1872 if (IEEE80211_IS_CHAN_ANYG(c))
1874 if (IEEE80211_IS_CHAN_B(c))