2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2008 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_dl.h>
43 #include <net/if_media.h>
44 #include <net/if_types.h>
45 #include <net/ethernet.h>
47 #include <net80211/ieee80211_var.h>
48 #include <net80211/ieee80211_regdomain.h>
52 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
53 [IEEE80211_MODE_AUTO] = "auto",
54 [IEEE80211_MODE_11A] = "11a",
55 [IEEE80211_MODE_11B] = "11b",
56 [IEEE80211_MODE_11G] = "11g",
57 [IEEE80211_MODE_FH] = "FH",
58 [IEEE80211_MODE_TURBO_A] = "turboA",
59 [IEEE80211_MODE_TURBO_G] = "turboG",
60 [IEEE80211_MODE_STURBO_A] = "sturboA",
61 [IEEE80211_MODE_11NA] = "11na",
62 [IEEE80211_MODE_11NG] = "11ng",
64 /* map ieee80211_opmode to the corresponding capability bit */
65 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
66 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
67 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
68 [IEEE80211_M_STA] = IEEE80211_C_STA,
69 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
70 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
71 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
74 static const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
75 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
77 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
78 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
79 static int ieee80211_media_setup(struct ieee80211com *ic,
80 struct ifmedia *media, int caps, int addsta,
81 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
82 static void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
83 static int ieee80211com_media_change(struct ifnet *);
84 static int media_status(enum ieee80211_opmode,
85 const struct ieee80211_channel *);
87 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
90 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
92 #define B(r) ((r) | IEEE80211_RATE_BASIC)
93 static const struct ieee80211_rateset ieee80211_rateset_11a =
94 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
95 static const struct ieee80211_rateset ieee80211_rateset_half =
96 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
97 static const struct ieee80211_rateset ieee80211_rateset_quarter =
98 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
99 static const struct ieee80211_rateset ieee80211_rateset_11b =
100 { 4, { B(2), B(4), B(11), B(22) } };
101 /* NB: OFDM rates are handled specially based on mode */
102 static const struct ieee80211_rateset ieee80211_rateset_11g =
103 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
107 * Fill in 802.11 available channel set, mark
108 * all available channels as active, and pick
109 * a default channel if not already specified.
112 ieee80211_chan_init(struct ieee80211com *ic)
114 #define DEFAULTRATES(m, def) do { \
115 if (isset(ic->ic_modecaps, m) && ic->ic_sup_rates[m].rs_nrates == 0) \
116 ic->ic_sup_rates[m] = def; \
118 struct ieee80211_channel *c;
121 KASSERT(0 < ic->ic_nchans && ic->ic_nchans < IEEE80211_CHAN_MAX,
122 ("invalid number of channels specified: %u", ic->ic_nchans));
123 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
124 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
125 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
126 for (i = 0; i < ic->ic_nchans; i++) {
127 c = &ic->ic_channels[i];
128 KASSERT(c->ic_flags != 0, ("channel with no flags"));
129 KASSERT(c->ic_ieee < IEEE80211_CHAN_MAX,
130 ("channel with bogus ieee number %u", c->ic_ieee));
131 setbit(ic->ic_chan_avail, c->ic_ieee);
133 * Identify mode capabilities.
135 if (IEEE80211_IS_CHAN_A(c))
136 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
137 if (IEEE80211_IS_CHAN_B(c))
138 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
139 if (IEEE80211_IS_CHAN_ANYG(c))
140 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
141 if (IEEE80211_IS_CHAN_FHSS(c))
142 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
143 if (IEEE80211_IS_CHAN_108A(c))
144 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
145 if (IEEE80211_IS_CHAN_108G(c))
146 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
147 if (IEEE80211_IS_CHAN_ST(c))
148 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
149 if (IEEE80211_IS_CHAN_HTA(c))
150 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
151 if (IEEE80211_IS_CHAN_HTG(c))
152 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
154 /* initialize candidate channels to all available */
155 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
156 sizeof(ic->ic_chan_avail));
158 /* sort channel table to allow lookup optimizations */
159 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
161 /* invalidate any previous state */
162 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
163 ic->ic_prevchan = NULL;
164 ic->ic_csa_newchan = NULL;
165 /* arbitrarily pick the first channel */
166 ic->ic_curchan = &ic->ic_channels[0];
168 /* fillin well-known rate sets if driver has not specified */
169 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
170 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
171 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
172 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
173 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
176 * Set auto mode to reset active channel state and any desired channel.
178 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
183 null_update_mcast(struct ifnet *ifp)
185 if_printf(ifp, "need multicast update callback\n");
189 null_update_promisc(struct ifnet *ifp)
191 if_printf(ifp, "need promiscuous mode update callback\n");
195 null_output(struct ifnet *ifp, struct mbuf *m,
196 struct sockaddr *dst, struct rtentry *rt0)
198 if_printf(ifp, "discard raw packet\n");
204 null_input(struct ifnet *ifp, struct mbuf *m)
206 if_printf(ifp, "if_input should not be called\n");
211 * Attach/setup the common net80211 state. Called by
212 * the driver on attach to prior to creating any vap's.
215 ieee80211_ifattach(struct ieee80211com *ic)
217 struct ifnet *ifp = ic->ic_ifp;
218 struct sockaddr_dl *sdl;
221 KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
223 IEEE80211_LOCK_INIT(ic, ifp->if_xname);
224 TAILQ_INIT(&ic->ic_vaps);
226 * Fill in 802.11 available channel set, mark all
227 * available channels as active, and pick a default
228 * channel if not already specified.
230 ieee80211_media_init(ic);
232 ic->ic_update_mcast = null_update_mcast;
233 ic->ic_update_promisc = null_update_promisc;
235 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
236 ic->ic_lintval = ic->ic_bintval;
237 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
239 ieee80211_crypto_attach(ic);
240 ieee80211_node_attach(ic);
241 ieee80211_power_attach(ic);
242 ieee80211_proto_attach(ic);
243 ieee80211_ht_attach(ic);
244 ieee80211_scan_attach(ic);
245 ieee80211_regdomain_attach(ic);
247 ieee80211_sysctl_attach(ic);
249 ifp->if_addrlen = IEEE80211_ADDR_LEN;
252 ifp->if_mtu = IEEE80211_MTU_MAX;
253 ifp->if_broadcastaddr = ieee80211broadcastaddr;
254 ifp->if_output = null_output;
255 ifp->if_input = null_input; /* just in case */
256 ifp->if_resolvemulti = NULL; /* NB: callers check */
258 ifa = ifaddr_byindex(ifp->if_index);
259 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
260 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
261 sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */
262 sdl->sdl_alen = IEEE80211_ADDR_LEN;
263 IEEE80211_ADDR_COPY(LLADDR(sdl), ic->ic_myaddr);
267 * Detach net80211 state on device detach. Tear down
268 * all vap's and reclaim all common state prior to the
269 * device state going away. Note we may call back into
270 * driver; it must be prepared for this.
273 ieee80211_ifdetach(struct ieee80211com *ic)
275 struct ifnet *ifp = ic->ic_ifp;
276 struct ieee80211vap *vap;
278 /* XXX ieee80211_stop_all? */
279 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
280 ieee80211_vap_destroy(vap);
282 ieee80211_sysctl_detach(ic);
283 ieee80211_regdomain_detach(ic);
284 ieee80211_scan_detach(ic);
285 ieee80211_ht_detach(ic);
286 /* NB: must be called before ieee80211_node_detach */
287 ieee80211_proto_detach(ic);
288 ieee80211_crypto_detach(ic);
289 ieee80211_power_detach(ic);
290 ieee80211_node_detach(ic);
291 ifmedia_removeall(&ic->ic_media);
293 IEEE80211_LOCK_DESTROY(ic);
298 * Default reset method for use with the ioctl support. This
299 * method is invoked after any state change in the 802.11
300 * layer that should be propagated to the hardware but not
301 * require re-initialization of the 802.11 state machine (e.g
302 * rescanning for an ap). We always return ENETRESET which
303 * should cause the driver to re-initialize the device. Drivers
304 * can override this method to implement more optimized support.
307 default_reset(struct ieee80211vap *vap, u_long cmd)
313 * Prepare a vap for use. Drivers use this call to
314 * setup net80211 state in new vap's prior attaching
315 * them with ieee80211_vap_attach (below).
318 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
319 const char name[IFNAMSIZ], int unit, int opmode, int flags,
320 const uint8_t bssid[IEEE80211_ADDR_LEN],
321 const uint8_t macaddr[IEEE80211_ADDR_LEN])
325 ifp = if_alloc(IFT_ETHER);
327 if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
331 if_initname(ifp, name, unit);
332 ifp->if_softc = vap; /* back pointer */
333 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
334 ifp->if_start = ieee80211_start;
335 ifp->if_ioctl = ieee80211_ioctl;
336 ifp->if_watchdog = NULL; /* NB: no watchdog routine */
337 ifp->if_init = ieee80211_init;
338 /* NB: input+output filled in by ether_ifattach */
339 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
340 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
341 IFQ_SET_READY(&ifp->if_snd);
345 vap->iv_flags = ic->ic_flags; /* propagate common flags */
346 vap->iv_flags_ext = ic->ic_flags_ext;
347 vap->iv_flags_ven = ic->ic_flags_ven;
348 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
349 vap->iv_htcaps = ic->ic_htcaps;
350 vap->iv_opmode = opmode;
351 vap->iv_caps |= ieee80211_opcap[opmode];
353 case IEEE80211_M_STA:
354 /* auto-enable s/w beacon miss support */
355 if (flags & IEEE80211_CLONE_NOBEACONS)
356 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
358 case IEEE80211_M_WDS:
360 * WDS links must specify the bssid of the far end.
361 * For legacy operation this is a static relationship.
362 * For non-legacy operation the station must associate
363 * and be authorized to pass traffic. Plumbing the
364 * vap to the proper node happens when the vap
365 * transitions to RUN state.
367 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
368 vap->iv_flags |= IEEE80211_F_DESBSSID;
369 if (flags & IEEE80211_CLONE_WDSLEGACY)
370 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
374 * Enable various functionality by default if we're
375 * capable; the driver can override us if it knows better.
377 if (vap->iv_caps & IEEE80211_C_WME)
378 vap->iv_flags |= IEEE80211_F_WME;
379 if (vap->iv_caps & IEEE80211_C_BURST)
380 vap->iv_flags |= IEEE80211_F_BURST;
381 if (vap->iv_caps & IEEE80211_C_FF)
382 vap->iv_flags |= IEEE80211_F_FF;
383 if (vap->iv_caps & IEEE80211_C_TURBOP)
384 vap->iv_flags |= IEEE80211_F_TURBOP;
385 /* NB: bg scanning only makes sense for station mode right now */
386 if (vap->iv_opmode == IEEE80211_M_STA &&
387 (vap->iv_caps & IEEE80211_C_BGSCAN))
388 vap->iv_flags |= IEEE80211_F_BGSCAN;
389 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
390 /* NB: DFS support only makes sense for ap mode right now */
391 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
392 (vap->iv_caps & IEEE80211_C_DFS))
393 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
395 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
396 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
397 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
399 * Install a default reset method for the ioctl support;
400 * the driver can override this.
402 vap->iv_reset = default_reset;
404 IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
406 ieee80211_sysctl_vattach(vap);
407 ieee80211_crypto_vattach(vap);
408 ieee80211_node_vattach(vap);
409 ieee80211_power_vattach(vap);
410 ieee80211_proto_vattach(vap);
411 ieee80211_ht_vattach(vap);
412 ieee80211_scan_vattach(vap);
413 ieee80211_regdomain_vattach(vap);
419 * Activate a vap. State should have been prepared with a
420 * call to ieee80211_vap_setup and by the driver. On return
421 * from this call the vap is ready for use.
424 ieee80211_vap_attach(struct ieee80211vap *vap,
425 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
427 struct ifnet *ifp = vap->iv_ifp;
428 struct ieee80211com *ic = vap->iv_ic;
429 struct ifmediareq imr;
432 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
433 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
434 __func__, ieee80211_opmode_name[vap->iv_opmode],
435 ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
438 * Do late attach work that cannot happen until after
439 * the driver has had a chance to override defaults.
441 ieee80211_node_latevattach(vap);
442 ieee80211_power_latevattach(vap);
444 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
445 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
446 ieee80211_media_status(ifp, &imr);
447 /* NB: strip explicit mode; we're actually in autoselect */
448 ifmedia_set(&vap->iv_media, imr.ifm_active &~ IFM_MMASK);
450 ifp->if_baudrate = IF_Mbps(maxrate);
452 ether_ifattach(ifp, vap->iv_myaddr);
453 /* hook output method setup by ether_ifattach */
454 vap->iv_output = ifp->if_output;
455 ifp->if_output = ieee80211_output;
456 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
457 bpfattach2(ifp, DLT_IEEE802_11, ifp->if_hdrlen, &vap->iv_rawbpf);
460 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
461 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
462 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
463 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
464 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
465 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
466 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
467 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
468 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
469 IEEE80211_UNLOCK(ic);
475 * Tear down vap state and reclaim the ifnet.
476 * The driver is assumed to have prepared for
477 * this; e.g. by turning off interrupts for the
481 ieee80211_vap_detach(struct ieee80211vap *vap)
483 struct ieee80211com *ic = vap->iv_ic;
484 struct ifnet *ifp = vap->iv_ifp;
486 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
487 __func__, ieee80211_opmode_name[vap->iv_opmode],
488 ic->ic_ifp->if_xname);
491 /* block traffic from above */
492 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
494 * Evil hack. Clear the backpointer from the ifnet to the
495 * vap so any requests from above will return an error or
496 * be ignored. In particular this short-circuits requests
497 * by the bridge to turn off promiscuous mode as a result
498 * of calling ether_ifdetach.
500 ifp->if_softc = NULL;
502 * Stop the vap before detaching the ifnet. Ideally we'd
503 * do this in the other order so the ifnet is inaccessible
504 * while we cleanup internal state but that is hard.
506 ieee80211_stop_locked(vap);
508 /* XXX accumulate iv_stats in ic_stats? */
509 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
510 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
511 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
512 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
513 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
514 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
515 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
516 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
517 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
518 IEEE80211_UNLOCK(ic);
520 /* XXX can't hold com lock */
521 /* NB: bpfattach is called by ether_ifdetach and claims all taps */
524 ifmedia_removeall(&vap->iv_media);
526 ieee80211_regdomain_vdetach(vap);
527 ieee80211_scan_vdetach(vap);
528 ieee80211_ht_vdetach(vap);
529 /* NB: must be before ieee80211_node_vdetach */
530 ieee80211_proto_vdetach(vap);
531 ieee80211_crypto_vdetach(vap);
532 ieee80211_power_vdetach(vap);
533 ieee80211_node_vdetach(vap);
534 ieee80211_sysctl_vdetach(vap);
540 * Synchronize flag bit state in the parent ifnet structure
541 * according to the state of all vap ifnet's. This is used,
542 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
545 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
547 struct ifnet *ifp = ic->ic_ifp;
548 struct ieee80211vap *vap;
551 IEEE80211_LOCK_ASSERT(ic);
554 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
555 if (vap->iv_ifp->if_flags & flag) {
557 * XXX the bridge sets PROMISC but we don't want to
558 * enable it on the device, discard here so all the
559 * drivers don't need to special-case it
561 if (flag == IFF_PROMISC &&
562 vap->iv_opmode == IEEE80211_M_HOSTAP)
567 oflags = ifp->if_flags;
569 ifp->if_flags |= flag;
571 ifp->if_flags &= ~flag;
572 if ((ifp->if_flags ^ oflags) & flag) {
573 /* XXX should we return 1/0 and let caller do this? */
574 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
575 if (flag == IFF_PROMISC)
576 ic->ic_update_promisc(ifp);
577 else if (flag == IFF_ALLMULTI)
578 ic->ic_update_mcast(ifp);
584 * Synchronize flag bit state in the com structure
585 * according to the state of all vap's. This is used,
586 * for example, to handle state changes via ioctls.
589 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
591 struct ieee80211vap *vap;
594 IEEE80211_LOCK_ASSERT(ic);
597 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
598 if (vap->iv_flags & flag) {
603 ic->ic_flags |= flag;
605 ic->ic_flags &= ~flag;
609 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
611 struct ieee80211com *ic = vap->iv_ic;
616 vap->iv_flags &= ~flag;
618 vap->iv_flags |= flag;
619 ieee80211_syncflag_locked(ic, flag);
620 IEEE80211_UNLOCK(ic);
624 * Synchronize flag bit state in the com structure
625 * according to the state of all vap's. This is used,
626 * for example, to handle state changes via ioctls.
629 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
631 struct ieee80211vap *vap;
634 IEEE80211_LOCK_ASSERT(ic);
637 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
638 if (vap->iv_flags_ext & flag) {
643 ic->ic_flags_ext |= flag;
645 ic->ic_flags_ext &= ~flag;
649 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
651 struct ieee80211com *ic = vap->iv_ic;
656 vap->iv_flags_ext &= ~flag;
658 vap->iv_flags_ext |= flag;
659 ieee80211_syncflag_ext_locked(ic, flag);
660 IEEE80211_UNLOCK(ic);
664 mapgsm(u_int freq, u_int flags)
667 if (flags & IEEE80211_CHAN_QUARTER)
669 else if (flags & IEEE80211_CHAN_HALF)
673 /* NB: there is no 907/20 wide but leave room */
674 return (freq - 906*10) / 5;
678 mappsb(u_int freq, u_int flags)
680 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
684 * Convert MHz frequency to IEEE channel number.
687 ieee80211_mhz2ieee(u_int freq, u_int flags)
689 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
690 if (flags & IEEE80211_CHAN_GSM)
691 return mapgsm(freq, flags);
692 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
696 return ((int) freq - 2407) / 5;
698 return 15 + ((freq - 2512) / 20);
699 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
701 /* XXX check regdomain? */
702 if (IS_FREQ_IN_PSB(freq))
703 return mappsb(freq, flags);
704 return (freq - 4000) / 5;
706 return (freq - 5000) / 5;
707 } else { /* either, guess */
711 if (907 <= freq && freq <= 922)
712 return mapgsm(freq, flags);
713 return ((int) freq - 2407) / 5;
716 if (IS_FREQ_IN_PSB(freq))
717 return mappsb(freq, flags);
718 else if (freq > 4900)
719 return (freq - 4000) / 5;
721 return 15 + ((freq - 2512) / 20);
723 return (freq - 5000) / 5;
725 #undef IS_FREQ_IN_PSB
729 * Convert channel to IEEE channel number.
732 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
735 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
738 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
742 * Convert IEEE channel number to MHz frequency.
745 ieee80211_ieee2mhz(u_int chan, u_int flags)
747 if (flags & IEEE80211_CHAN_GSM)
748 return 907 + 5 * (chan / 10);
749 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
753 return 2407 + chan*5;
755 return 2512 + ((chan-15)*20);
756 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
757 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
759 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
761 return 5000 + (chan*5);
762 } else { /* either, guess */
763 /* XXX can't distinguish PSB+GSM channels */
766 if (chan < 14) /* 0-13 */
767 return 2407 + chan*5;
768 if (chan < 27) /* 15-26 */
769 return 2512 + ((chan-15)*20);
770 return 5000 + (chan*5);
775 * Locate a channel given a frequency+flags. We cache
776 * the previous lookup to optimize switching between two
777 * channels--as happens with dynamic turbo.
779 struct ieee80211_channel *
780 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
782 struct ieee80211_channel *c;
785 flags &= IEEE80211_CHAN_ALLTURBO;
787 if (c != NULL && c->ic_freq == freq &&
788 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
790 /* brute force search */
791 for (i = 0; i < ic->ic_nchans; i++) {
792 c = &ic->ic_channels[i];
793 if (c->ic_freq == freq &&
794 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
801 * Locate a channel given a channel number+flags. We cache
802 * the previous lookup to optimize switching between two
803 * channels--as happens with dynamic turbo.
805 struct ieee80211_channel *
806 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
808 struct ieee80211_channel *c;
811 flags &= IEEE80211_CHAN_ALLTURBO;
813 if (c != NULL && c->ic_ieee == ieee &&
814 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
816 /* brute force search */
817 for (i = 0; i < ic->ic_nchans; i++) {
818 c = &ic->ic_channels[i];
819 if (c->ic_ieee == ieee &&
820 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
827 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
829 #define ADD(_ic, _s, _o) \
831 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
832 static const u_int mopts[IEEE80211_MODE_MAX] = {
838 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
839 IFM_IEEE80211_11G | IFM_IEEE80211_TURBO,
840 IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
848 ADD(ic, mword, mopt); /* STA mode has no cap */
849 if (caps & IEEE80211_C_IBSS)
850 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
851 if (caps & IEEE80211_C_HOSTAP)
852 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
853 if (caps & IEEE80211_C_AHDEMO)
854 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
855 if (caps & IEEE80211_C_MONITOR)
856 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
857 if (caps & IEEE80211_C_WDS)
858 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
863 * Setup the media data structures according to the channel and
867 ieee80211_media_setup(struct ieee80211com *ic,
868 struct ifmedia *media, int caps, int addsta,
869 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
871 int i, j, mode, rate, maxrate, mword, r;
872 const struct ieee80211_rateset *rs;
873 struct ieee80211_rateset allrates;
876 * Fill in media characteristics.
878 ifmedia_init(media, 0, media_change, media_stat);
881 * Add media for legacy operating modes.
883 memset(&allrates, 0, sizeof(allrates));
884 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
885 if (isclr(ic->ic_modecaps, mode))
887 addmedia(media, caps, addsta, mode, IFM_AUTO);
888 if (mode == IEEE80211_MODE_AUTO)
890 rs = &ic->ic_sup_rates[mode];
891 for (i = 0; i < rs->rs_nrates; i++) {
892 rate = rs->rs_rates[i];
893 mword = ieee80211_rate2media(ic, rate, mode);
896 addmedia(media, caps, addsta, mode, mword);
898 * Add legacy rate to the collection of all rates.
900 r = rate & IEEE80211_RATE_VAL;
901 for (j = 0; j < allrates.rs_nrates; j++)
902 if (allrates.rs_rates[j] == r)
904 if (j == allrates.rs_nrates) {
905 /* unique, add to the set */
906 allrates.rs_rates[j] = r;
907 allrates.rs_nrates++;
909 rate = (rate & IEEE80211_RATE_VAL) / 2;
914 for (i = 0; i < allrates.rs_nrates; i++) {
915 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
916 IEEE80211_MODE_AUTO);
919 /* NB: remove media options from mword */
920 addmedia(media, caps, addsta,
921 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
924 * Add HT/11n media. Note that we do not have enough
925 * bits in the media subtype to express the MCS so we
926 * use a "placeholder" media subtype and any fixed MCS
927 * must be specified with a different mechanism.
929 for (; mode < IEEE80211_MODE_MAX; mode++) {
930 if (isclr(ic->ic_modecaps, mode))
932 addmedia(media, caps, addsta, mode, IFM_AUTO);
933 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
935 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
936 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
937 addmedia(media, caps, addsta,
938 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
939 /* XXX could walk htrates */
940 /* XXX known array size */
941 if (ieee80211_htrates[15].ht40_rate_400ns > maxrate)
942 maxrate = ieee80211_htrates[15].ht40_rate_400ns;
948 ieee80211_media_init(struct ieee80211com *ic)
950 struct ifnet *ifp = ic->ic_ifp;
953 /* NB: this works because the structure is initialized to zero */
954 if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
956 * We are re-initializing the channel list; clear
957 * the existing media state as the media routines
958 * don't suppress duplicates.
960 ifmedia_removeall(&ic->ic_media);
962 ieee80211_chan_init(ic);
965 * Recalculate media settings in case new channel list changes
966 * the set of available modes.
968 maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
969 ieee80211com_media_change, ieee80211com_media_status);
970 /* NB: strip explicit mode; we're actually in autoselect */
971 ifmedia_set(&ic->ic_media,
972 media_status(ic->ic_opmode, ic->ic_curchan) &~ IFM_MMASK);
974 ifp->if_baudrate = IF_Mbps(maxrate);
976 /* XXX need to propagate new media settings to vap's */
979 const struct ieee80211_rateset *
980 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
982 if (IEEE80211_IS_CHAN_HALF(c))
983 return &ieee80211_rateset_half;
984 if (IEEE80211_IS_CHAN_QUARTER(c))
985 return &ieee80211_rateset_quarter;
986 if (IEEE80211_IS_CHAN_HTA(c))
987 return &ic->ic_sup_rates[IEEE80211_MODE_11A];
988 if (IEEE80211_IS_CHAN_HTG(c)) {
989 /* XXX does this work for basic rates? */
990 return &ic->ic_sup_rates[IEEE80211_MODE_11G];
992 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
996 ieee80211_announce(struct ieee80211com *ic)
998 struct ifnet *ifp = ic->ic_ifp;
999 int i, mode, rate, mword;
1000 const struct ieee80211_rateset *rs;
1002 /* NB: skip AUTO since it has no rates */
1003 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1004 if (isclr(ic->ic_modecaps, mode))
1006 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1007 rs = &ic->ic_sup_rates[mode];
1008 for (i = 0; i < rs->rs_nrates; i++) {
1009 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1012 rate = ieee80211_media2rate(mword);
1013 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1014 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1018 ieee80211_ht_announce(ic);
1022 ieee80211_announce_channels(struct ieee80211com *ic)
1024 const struct ieee80211_channel *c;
1028 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1029 for (i = 0; i < ic->ic_nchans; i++) {
1030 c = &ic->ic_channels[i];
1031 if (IEEE80211_IS_CHAN_ST(c))
1033 else if (IEEE80211_IS_CHAN_108A(c))
1035 else if (IEEE80211_IS_CHAN_108G(c))
1037 else if (IEEE80211_IS_CHAN_HT(c))
1039 else if (IEEE80211_IS_CHAN_A(c))
1041 else if (IEEE80211_IS_CHAN_ANYG(c))
1043 else if (IEEE80211_IS_CHAN_B(c))
1047 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1049 else if (IEEE80211_IS_CHAN_HALF(c))
1051 else if (IEEE80211_IS_CHAN_QUARTER(c))
1055 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1056 , c->ic_ieee, c->ic_freq, type
1058 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1059 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1061 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1062 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1068 media2mode(const struct ieee80211com *ic,
1069 const struct ifmedia_entry *ime, enum ieee80211_phymode *mode)
1071 switch (IFM_MODE(ime->ifm_media)) {
1072 case IFM_IEEE80211_11A:
1073 *mode = IEEE80211_MODE_11A;
1075 case IFM_IEEE80211_11B:
1076 *mode = IEEE80211_MODE_11B;
1078 case IFM_IEEE80211_11G:
1079 *mode = IEEE80211_MODE_11G;
1081 case IFM_IEEE80211_FH:
1082 *mode = IEEE80211_MODE_FH;
1084 case IFM_IEEE80211_11NA:
1085 *mode = IEEE80211_MODE_11NA;
1087 case IFM_IEEE80211_11NG:
1088 *mode = IEEE80211_MODE_11NG;
1091 *mode = IEEE80211_MODE_AUTO;
1097 * Turbo mode is an ``option''.
1098 * XXX does not apply to AUTO
1100 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1101 if (*mode == IEEE80211_MODE_11A) {
1102 if (ic->ic_flags & IEEE80211_F_TURBOP)
1103 *mode = IEEE80211_MODE_TURBO_A;
1105 *mode = IEEE80211_MODE_STURBO_A;
1106 } else if (*mode == IEEE80211_MODE_11G)
1107 *mode = IEEE80211_MODE_TURBO_G;
1116 * Handle a media change request on the underlying
1117 * interface; we accept mode changes only.
1120 ieee80211com_media_change(struct ifnet *ifp)
1122 struct ieee80211com *ic = ifp->if_l2com;
1123 struct ifmedia_entry *ime = ic->ic_media.ifm_cur;
1124 enum ieee80211_phymode newphymode;
1128 * First, identify the phy mode.
1130 if (!media2mode(ic, ime, &newphymode))
1132 /* NB: mode must be supported, no need to check */
1135 * Handle phy mode change.
1138 if (ic->ic_curmode != newphymode) { /* change phy mode */
1139 struct ieee80211vap *vap;
1141 (void) ieee80211_setmode(ic, newphymode);
1143 * Propagate new state to each vap.
1145 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
1148 IEEE80211_UNLOCK(ic);
1153 findrate(const struct ieee80211com *ic, enum ieee80211_phymode m, int r)
1157 for (i = 0, nrates = ic->ic_sup_rates[m].rs_nrates; i < nrates; i++)
1158 if ((ic->ic_sup_rates[m].rs_rates[i] & IEEE80211_RATE_VAL) == r)
1164 * Handle a media change request on the vap interface.
1167 ieee80211_media_change(struct ifnet *ifp)
1169 struct ieee80211vap *vap = ifp->if_softc;
1170 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1171 struct ieee80211com *ic = vap->iv_ic;
1174 /* XXX this won't work unless ic_curmode is != IEEE80211_MODE_AUTO */
1175 if (ic->ic_curmode == IEEE80211_MODE_AUTO)
1177 if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
1179 * NB: this can only be used to specify a legacy rate.
1181 newrate = ieee80211_media2rate(ime->ifm_media);
1184 if (findrate(ic, ic->ic_curmode, newrate) == -1)
1187 newrate = IEEE80211_FIXED_RATE_NONE;
1189 if (newrate != vap->iv_txparms[ic->ic_curmode].ucastrate) {
1190 vap->iv_txparms[ic->ic_curmode].ucastrate = newrate;
1197 * Common code to calculate the media status word
1198 * from the operating mode and channel state.
1201 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1205 status = IFM_IEEE80211;
1207 case IEEE80211_M_STA:
1209 case IEEE80211_M_IBSS:
1210 status |= IFM_IEEE80211_ADHOC;
1212 case IEEE80211_M_HOSTAP:
1213 status |= IFM_IEEE80211_HOSTAP;
1215 case IEEE80211_M_MONITOR:
1216 status |= IFM_IEEE80211_MONITOR;
1218 case IEEE80211_M_AHDEMO:
1219 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1221 case IEEE80211_M_WDS:
1222 status |= IFM_IEEE80211_WDS;
1225 if (IEEE80211_IS_CHAN_HTA(chan)) {
1226 status |= IFM_IEEE80211_11NA;
1227 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1228 status |= IFM_IEEE80211_11NG;
1229 } else if (IEEE80211_IS_CHAN_A(chan)) {
1230 status |= IFM_IEEE80211_11A;
1231 } else if (IEEE80211_IS_CHAN_B(chan)) {
1232 status |= IFM_IEEE80211_11B;
1233 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1234 status |= IFM_IEEE80211_11G;
1235 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1236 status |= IFM_IEEE80211_FH;
1238 /* XXX else complain? */
1240 if (IEEE80211_IS_CHAN_TURBO(chan))
1241 status |= IFM_IEEE80211_TURBO;
1243 if (IEEE80211_IS_CHAN_HT20(chan))
1244 status |= IFM_IEEE80211_HT20;
1245 if (IEEE80211_IS_CHAN_HT40(chan))
1246 status |= IFM_IEEE80211_HT40;
1252 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1254 struct ieee80211com *ic = ifp->if_l2com;
1255 struct ieee80211vap *vap;
1257 imr->ifm_status = IFM_AVALID;
1258 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1259 if (vap->iv_ifp->if_flags & IFF_UP) {
1260 imr->ifm_status |= IFM_ACTIVE;
1263 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1264 if (imr->ifm_status & IFM_ACTIVE)
1265 imr->ifm_current = imr->ifm_active;
1269 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1271 struct ieee80211vap *vap = ifp->if_softc;
1272 struct ieee80211com *ic = vap->iv_ic;
1273 enum ieee80211_phymode mode;
1275 imr->ifm_status = IFM_AVALID;
1277 * NB: use the current channel's mode to lock down a xmit
1278 * rate only when running; otherwise we may have a mismatch
1279 * in which case the rate will not be convertible.
1281 if (vap->iv_state == IEEE80211_S_RUN) {
1282 imr->ifm_status |= IFM_ACTIVE;
1283 mode = ieee80211_chan2mode(ic->ic_curchan);
1285 mode = IEEE80211_MODE_AUTO;
1286 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1288 * Calculate a current rate if possible.
1290 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1292 * A fixed rate is set, report that.
1294 imr->ifm_active |= ieee80211_rate2media(ic,
1295 vap->iv_txparms[mode].ucastrate, mode);
1296 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1298 * In station mode report the current transmit rate.
1300 imr->ifm_active |= ieee80211_rate2media(ic,
1301 vap->iv_bss->ni_txrate, mode);
1303 imr->ifm_active |= IFM_AUTO;
1304 if (imr->ifm_status & IFM_ACTIVE)
1305 imr->ifm_current = imr->ifm_active;
1309 * Set the current phy mode and recalculate the active channel
1310 * set based on the available channels for this mode. Also
1311 * select a new default/current channel if the current one is
1312 * inappropriate for this mode.
1315 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1318 * Adjust basic rates in 11b/11g supported rate set.
1319 * Note that if operating on a hal/quarter rate channel
1320 * this is a noop as those rates sets are different
1323 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1324 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1326 ic->ic_curmode = mode;
1327 ieee80211_reset_erp(ic); /* reset ERP state */
1333 * Return the phy mode for with the specified channel.
1335 enum ieee80211_phymode
1336 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1339 if (IEEE80211_IS_CHAN_HTA(chan))
1340 return IEEE80211_MODE_11NA;
1341 else if (IEEE80211_IS_CHAN_HTG(chan))
1342 return IEEE80211_MODE_11NG;
1343 else if (IEEE80211_IS_CHAN_108G(chan))
1344 return IEEE80211_MODE_TURBO_G;
1345 else if (IEEE80211_IS_CHAN_ST(chan))
1346 return IEEE80211_MODE_STURBO_A;
1347 else if (IEEE80211_IS_CHAN_TURBO(chan))
1348 return IEEE80211_MODE_TURBO_A;
1349 else if (IEEE80211_IS_CHAN_A(chan))
1350 return IEEE80211_MODE_11A;
1351 else if (IEEE80211_IS_CHAN_ANYG(chan))
1352 return IEEE80211_MODE_11G;
1353 else if (IEEE80211_IS_CHAN_B(chan))
1354 return IEEE80211_MODE_11B;
1355 else if (IEEE80211_IS_CHAN_FHSS(chan))
1356 return IEEE80211_MODE_FH;
1358 /* NB: should not get here */
1359 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1360 __func__, chan->ic_freq, chan->ic_flags);
1361 return IEEE80211_MODE_11B;
1365 u_int match; /* rate + mode */
1366 u_int media; /* if_media rate */
1370 findmedia(const struct ratemedia rates[], int n, u_int match)
1374 for (i = 0; i < n; i++)
1375 if (rates[i].match == match)
1376 return rates[i].media;
1381 * Convert IEEE80211 rate value to ifmedia subtype.
1382 * Rate is either a legacy rate in units of 0.5Mbps
1386 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1388 #define N(a) (sizeof(a) / sizeof(a[0]))
1389 static const struct ratemedia rates[] = {
1390 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1391 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1392 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1393 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1394 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1395 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1396 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1397 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1398 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1399 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1400 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1401 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1402 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1403 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1404 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1405 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1406 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1407 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1408 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1409 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1410 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1411 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1412 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1413 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1414 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1415 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1416 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1417 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1418 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1419 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1420 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1422 static const struct ratemedia htrates[] = {
1423 { 0, IFM_IEEE80211_MCS },
1424 { 1, IFM_IEEE80211_MCS },
1425 { 2, IFM_IEEE80211_MCS },
1426 { 3, IFM_IEEE80211_MCS },
1427 { 4, IFM_IEEE80211_MCS },
1428 { 5, IFM_IEEE80211_MCS },
1429 { 6, IFM_IEEE80211_MCS },
1430 { 7, IFM_IEEE80211_MCS },
1431 { 8, IFM_IEEE80211_MCS },
1432 { 9, IFM_IEEE80211_MCS },
1433 { 10, IFM_IEEE80211_MCS },
1434 { 11, IFM_IEEE80211_MCS },
1435 { 12, IFM_IEEE80211_MCS },
1436 { 13, IFM_IEEE80211_MCS },
1437 { 14, IFM_IEEE80211_MCS },
1438 { 15, IFM_IEEE80211_MCS },
1443 * Check 11n rates first for match as an MCS.
1445 if (mode == IEEE80211_MODE_11NA) {
1446 if (rate & IEEE80211_RATE_MCS) {
1447 rate &= ~IEEE80211_RATE_MCS;
1448 m = findmedia(htrates, N(htrates), rate);
1450 return m | IFM_IEEE80211_11NA;
1452 } else if (mode == IEEE80211_MODE_11NG) {
1453 /* NB: 12 is ambiguous, it will be treated as an MCS */
1454 if (rate & IEEE80211_RATE_MCS) {
1455 rate &= ~IEEE80211_RATE_MCS;
1456 m = findmedia(htrates, N(htrates), rate);
1458 return m | IFM_IEEE80211_11NG;
1461 rate &= IEEE80211_RATE_VAL;
1463 case IEEE80211_MODE_11A:
1464 case IEEE80211_MODE_11NA:
1465 case IEEE80211_MODE_TURBO_A:
1466 case IEEE80211_MODE_STURBO_A:
1467 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1468 case IEEE80211_MODE_11B:
1469 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1470 case IEEE80211_MODE_FH:
1471 return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1472 case IEEE80211_MODE_AUTO:
1473 /* NB: ic may be NULL for some drivers */
1474 if (ic && ic->ic_phytype == IEEE80211_T_FH)
1475 return findmedia(rates, N(rates),
1476 rate | IFM_IEEE80211_FH);
1477 /* NB: hack, 11g matches both 11b+11a rates */
1479 case IEEE80211_MODE_11G:
1480 case IEEE80211_MODE_11NG:
1481 case IEEE80211_MODE_TURBO_G:
1482 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1489 ieee80211_media2rate(int mword)
1491 #define N(a) (sizeof(a) / sizeof(a[0]))
1492 static const int ieeerates[] = {
1496 2, /* IFM_IEEE80211_FH1 */
1497 4, /* IFM_IEEE80211_FH2 */
1498 2, /* IFM_IEEE80211_DS1 */
1499 4, /* IFM_IEEE80211_DS2 */
1500 11, /* IFM_IEEE80211_DS5 */
1501 22, /* IFM_IEEE80211_DS11 */
1502 44, /* IFM_IEEE80211_DS22 */
1503 12, /* IFM_IEEE80211_OFDM6 */
1504 18, /* IFM_IEEE80211_OFDM9 */
1505 24, /* IFM_IEEE80211_OFDM12 */
1506 36, /* IFM_IEEE80211_OFDM18 */
1507 48, /* IFM_IEEE80211_OFDM24 */
1508 72, /* IFM_IEEE80211_OFDM36 */
1509 96, /* IFM_IEEE80211_OFDM48 */
1510 108, /* IFM_IEEE80211_OFDM54 */
1511 144, /* IFM_IEEE80211_OFDM72 */
1512 0, /* IFM_IEEE80211_DS354k */
1513 0, /* IFM_IEEE80211_DS512k */
1514 6, /* IFM_IEEE80211_OFDM3 */
1515 9, /* IFM_IEEE80211_OFDM4 */
1516 54, /* IFM_IEEE80211_OFDM27 */
1517 -1, /* IFM_IEEE80211_MCS */
1519 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1520 ieeerates[IFM_SUBTYPE(mword)] : 0;