HaikuDepot: notify work status from main window
[haiku.git] / src / libs / compat / freebsd_wlan / net80211 / ieee80211.c
bloba03904176c7656ef083da1888fdc47c3fadb48f7
1 /*-
2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
33 #include "opt_wlan.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/socket.h>
41 #include <net/if.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>
49 #ifdef IEEE80211_SUPPORT_SUPERG
50 #include <net80211/ieee80211_superg.h>
51 #endif
52 #include <net80211/ieee80211_ratectl.h>
54 #include <net/bpf.h>
56 const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
57 [IEEE80211_MODE_AUTO] = "auto",
58 [IEEE80211_MODE_11A] = "11a",
59 [IEEE80211_MODE_11B] = "11b",
60 [IEEE80211_MODE_11G] = "11g",
61 [IEEE80211_MODE_FH] = "FH",
62 [IEEE80211_MODE_TURBO_A] = "turboA",
63 [IEEE80211_MODE_TURBO_G] = "turboG",
64 [IEEE80211_MODE_STURBO_A] = "sturboA",
65 [IEEE80211_MODE_HALF] = "half",
66 [IEEE80211_MODE_QUARTER] = "quarter",
67 [IEEE80211_MODE_11NA] = "11na",
68 [IEEE80211_MODE_11NG] = "11ng",
70 /* map ieee80211_opmode to the corresponding capability bit */
71 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
72 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
73 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
74 [IEEE80211_M_STA] = IEEE80211_C_STA,
75 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
76 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
77 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
78 #ifdef IEEE80211_SUPPORT_MESH
79 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
80 #endif
83 static const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
84 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
86 static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
87 static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
88 static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
89 static int ieee80211_media_setup(struct ieee80211com *ic,
90 struct ifmedia *media, int caps, int addsta,
91 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
92 static void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
93 static int ieee80211com_media_change(struct ifnet *);
94 static int media_status(enum ieee80211_opmode,
95 const struct ieee80211_channel *);
98 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
100 #define B(r) ((r) | IEEE80211_RATE_BASIC)
101 static const struct ieee80211_rateset ieee80211_rateset_11a =
102 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
103 static const struct ieee80211_rateset ieee80211_rateset_half =
104 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
105 static const struct ieee80211_rateset ieee80211_rateset_quarter =
106 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
107 static const struct ieee80211_rateset ieee80211_rateset_11b =
108 { 4, { B(2), B(4), B(11), B(22) } };
109 /* NB: OFDM rates are handled specially based on mode */
110 static const struct ieee80211_rateset ieee80211_rateset_11g =
111 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
112 #undef B
115 * Fill in 802.11 available channel set, mark
116 * all available channels as active, and pick
117 * a default channel if not already specified.
119 static void
120 ieee80211_chan_init(struct ieee80211com *ic)
122 #define DEFAULTRATES(m, def) do { \
123 if (ic->ic_sup_rates[m].rs_nrates == 0) \
124 ic->ic_sup_rates[m] = def; \
125 } while (0)
126 struct ieee80211_channel *c;
127 int i;
129 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
130 ("invalid number of channels specified: %u", ic->ic_nchans));
131 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
132 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
133 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
134 for (i = 0; i < ic->ic_nchans; i++) {
135 c = &ic->ic_channels[i];
136 KASSERT(c->ic_flags != 0, ("channel with no flags"));
138 * Help drivers that work only with frequencies by filling
139 * in IEEE channel #'s if not already calculated. Note this
140 * mimics similar work done in ieee80211_setregdomain when
141 * changing regulatory state.
143 if (c->ic_ieee == 0)
144 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
145 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
146 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
147 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
148 c->ic_flags);
149 /* default max tx power to max regulatory */
150 if (c->ic_maxpower == 0)
151 c->ic_maxpower = 2*c->ic_maxregpower;
152 setbit(ic->ic_chan_avail, c->ic_ieee);
154 * Identify mode capabilities.
156 if (IEEE80211_IS_CHAN_A(c))
157 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
158 if (IEEE80211_IS_CHAN_B(c))
159 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
160 if (IEEE80211_IS_CHAN_ANYG(c))
161 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
162 if (IEEE80211_IS_CHAN_FHSS(c))
163 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
164 if (IEEE80211_IS_CHAN_108A(c))
165 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
166 if (IEEE80211_IS_CHAN_108G(c))
167 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
168 if (IEEE80211_IS_CHAN_ST(c))
169 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
170 if (IEEE80211_IS_CHAN_HALF(c))
171 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
172 if (IEEE80211_IS_CHAN_QUARTER(c))
173 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
174 if (IEEE80211_IS_CHAN_HTA(c))
175 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
176 if (IEEE80211_IS_CHAN_HTG(c))
177 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
179 /* initialize candidate channels to all available */
180 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
181 sizeof(ic->ic_chan_avail));
183 /* sort channel table to allow lookup optimizations */
184 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
186 /* invalidate any previous state */
187 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
188 ic->ic_prevchan = NULL;
189 ic->ic_csa_newchan = NULL;
190 /* arbitrarily pick the first channel */
191 ic->ic_curchan = &ic->ic_channels[0];
192 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
194 /* fillin well-known rate sets if driver has not specified */
195 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
196 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
197 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
198 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
199 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
200 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
201 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
202 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
203 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
204 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
207 * Setup required information to fill the mcsset field, if driver did
208 * not. Assume a 2T2R setup for historic reasons.
210 if (ic->ic_rxstream == 0)
211 ic->ic_rxstream = 2;
212 if (ic->ic_txstream == 0)
213 ic->ic_txstream = 2;
216 * Set auto mode to reset active channel state and any desired channel.
218 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
219 #undef DEFAULTRATES
222 static void
223 null_update_mcast(struct ifnet *ifp)
225 if_printf(ifp, "need multicast update callback\n");
228 static void
229 null_update_promisc(struct ifnet *ifp)
231 if_printf(ifp, "need promiscuous mode update callback\n");
234 static int
235 null_transmit(struct ifnet *ifp, struct mbuf *m)
237 m_freem(m);
238 ifp->if_oerrors++;
239 return EACCES; /* XXX EIO/EPERM? */
242 static int
243 null_output(struct ifnet *ifp, struct mbuf *m,
244 struct sockaddr *dst, struct route *ro)
246 if_printf(ifp, "discard raw packet\n");
247 return null_transmit(ifp, m);
250 static void
251 null_input(struct ifnet *ifp, struct mbuf *m)
253 if_printf(ifp, "if_input should not be called\n");
254 m_freem(m);
258 * Attach/setup the common net80211 state. Called by
259 * the driver on attach to prior to creating any vap's.
261 void
262 ieee80211_ifattach(struct ieee80211com *ic,
263 const uint8_t macaddr[IEEE80211_ADDR_LEN])
265 struct ifnet *ifp = ic->ic_ifp;
266 struct sockaddr_dl *sdl;
267 struct ifaddr *ifa;
269 KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
271 IEEE80211_LOCK_INIT(ic, ifp->if_xname);
272 TAILQ_INIT(&ic->ic_vaps);
274 /* Create a taskqueue for all state changes */
275 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
276 taskqueue_thread_enqueue, &ic->ic_tq);
277 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s taskq",
278 ifp->if_xname);
280 * Fill in 802.11 available channel set, mark all
281 * available channels as active, and pick a default
282 * channel if not already specified.
284 ieee80211_media_init(ic);
286 ic->ic_update_mcast = null_update_mcast;
287 ic->ic_update_promisc = null_update_promisc;
289 ic->ic_hash_key = arc4random();
290 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
291 ic->ic_lintval = ic->ic_bintval;
292 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
294 ieee80211_crypto_attach(ic);
295 ieee80211_node_attach(ic);
296 ieee80211_power_attach(ic);
297 ieee80211_proto_attach(ic);
298 #ifdef IEEE80211_SUPPORT_SUPERG
299 ieee80211_superg_attach(ic);
300 #endif
301 ieee80211_ht_attach(ic);
302 ieee80211_scan_attach(ic);
303 ieee80211_regdomain_attach(ic);
304 ieee80211_dfs_attach(ic);
305 #if defined(__HAIKU__)
306 ieee80211_ratectl_attach(ic);
307 #endif
309 ieee80211_sysctl_attach(ic);
311 ifp->if_addrlen = IEEE80211_ADDR_LEN;
312 ifp->if_hdrlen = 0;
313 if_attach(ifp);
314 ifp->if_mtu = IEEE80211_MTU_MAX;
315 ifp->if_broadcastaddr = ieee80211broadcastaddr;
316 ifp->if_output = null_output;
317 ifp->if_input = null_input; /* just in case */
318 ifp->if_resolvemulti = NULL; /* NB: callers check */
320 #ifndef __HAIKU__
321 ifa = ifaddr_byindex(ifp->if_index);
322 KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
323 sdl = (struct sockaddr_dl *)ifa->ifa_addr;;
324 #else
325 sdl = &ifp->if_lladdr;
326 #endif
327 sdl->sdl_type = IFT_ETHER; /* XXX IFT_IEEE80211? */
328 sdl->sdl_alen = IEEE80211_ADDR_LEN;
329 IEEE80211_ADDR_COPY(LLADDR(sdl), macaddr);
330 ifa_free(ifa);
334 * Detach net80211 state on device detach. Tear down
335 * all vap's and reclaim all common state prior to the
336 * device state going away. Note we may call back into
337 * driver; it must be prepared for this.
339 void
340 ieee80211_ifdetach(struct ieee80211com *ic)
342 struct ifnet *ifp = ic->ic_ifp;
343 struct ieee80211vap *vap;
345 if_detach(ifp);
347 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
348 ieee80211_vap_destroy(vap);
349 ieee80211_waitfor_parent(ic);
351 ieee80211_sysctl_detach(ic);
352 #if defined(__HAIKU__)
353 ieee80211_ratectl_detach(ic);
354 #endif
355 ieee80211_dfs_detach(ic);
356 ieee80211_regdomain_detach(ic);
357 ieee80211_scan_detach(ic);
358 #ifdef IEEE80211_SUPPORT_SUPERG
359 ieee80211_superg_detach(ic);
360 #endif
361 ieee80211_ht_detach(ic);
362 /* NB: must be called before ieee80211_node_detach */
363 ieee80211_proto_detach(ic);
364 ieee80211_crypto_detach(ic);
365 ieee80211_power_detach(ic);
366 ieee80211_node_detach(ic);
368 ifmedia_removeall(&ic->ic_media);
369 taskqueue_free(ic->ic_tq);
370 IEEE80211_LOCK_DESTROY(ic);
374 * Default reset method for use with the ioctl support. This
375 * method is invoked after any state change in the 802.11
376 * layer that should be propagated to the hardware but not
377 * require re-initialization of the 802.11 state machine (e.g
378 * rescanning for an ap). We always return ENETRESET which
379 * should cause the driver to re-initialize the device. Drivers
380 * can override this method to implement more optimized support.
382 static int
383 default_reset(struct ieee80211vap *vap, u_long cmd)
385 return ENETRESET;
389 * Prepare a vap for use. Drivers use this call to
390 * setup net80211 state in new vap's prior attaching
391 * them with ieee80211_vap_attach (below).
394 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
395 const char name[IFNAMSIZ], int unit, int opmode, int flags,
396 const uint8_t bssid[IEEE80211_ADDR_LEN],
397 const uint8_t macaddr[IEEE80211_ADDR_LEN])
399 struct ifnet *ifp;
401 ifp = if_alloc(IFT_ETHER);
402 if (ifp == NULL) {
403 if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
404 __func__);
405 return ENOMEM;
407 if_initname(ifp, name, unit);
408 ifp->if_softc = vap; /* back pointer */
409 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
410 ifp->if_start = ieee80211_start;
411 ifp->if_ioctl = ieee80211_ioctl;
412 ifp->if_init = ieee80211_init;
413 /* NB: input+output filled in by ether_ifattach */
414 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen);
415 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen;
416 IFQ_SET_READY(&ifp->if_snd);
418 vap->iv_ifp = ifp;
419 vap->iv_ic = ic;
420 vap->iv_flags = ic->ic_flags; /* propagate common flags */
421 vap->iv_flags_ext = ic->ic_flags_ext;
422 vap->iv_flags_ven = ic->ic_flags_ven;
423 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
424 vap->iv_htcaps = ic->ic_htcaps;
425 vap->iv_htextcaps = ic->ic_htextcaps;
426 vap->iv_opmode = opmode;
427 vap->iv_caps |= ieee80211_opcap[opmode];
428 switch (opmode) {
429 case IEEE80211_M_WDS:
431 * WDS links must specify the bssid of the far end.
432 * For legacy operation this is a static relationship.
433 * For non-legacy operation the station must associate
434 * and be authorized to pass traffic. Plumbing the
435 * vap to the proper node happens when the vap
436 * transitions to RUN state.
438 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
439 vap->iv_flags |= IEEE80211_F_DESBSSID;
440 if (flags & IEEE80211_CLONE_WDSLEGACY)
441 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
442 break;
443 #ifdef IEEE80211_SUPPORT_TDMA
444 case IEEE80211_M_AHDEMO:
445 if (flags & IEEE80211_CLONE_TDMA) {
446 /* NB: checked before clone operation allowed */
447 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
448 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
450 * Propagate TDMA capability to mark vap; this
451 * cannot be removed and is used to distinguish
452 * regular ahdemo operation from ahdemo+tdma.
454 vap->iv_caps |= IEEE80211_C_TDMA;
456 break;
457 #endif
459 /* auto-enable s/w beacon miss support */
460 if (flags & IEEE80211_CLONE_NOBEACONS)
461 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
462 /* auto-generated or user supplied MAC address */
463 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
464 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
466 * Enable various functionality by default if we're
467 * capable; the driver can override us if it knows better.
469 if (vap->iv_caps & IEEE80211_C_WME)
470 vap->iv_flags |= IEEE80211_F_WME;
471 if (vap->iv_caps & IEEE80211_C_BURST)
472 vap->iv_flags |= IEEE80211_F_BURST;
473 /* NB: bg scanning only makes sense for station mode right now */
474 #if 0
475 if (vap->iv_opmode == IEEE80211_M_STA &&
476 (vap->iv_caps & IEEE80211_C_BGSCAN))
477 vap->iv_flags |= IEEE80211_F_BGSCAN;
478 #endif
479 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
480 /* NB: DFS support only makes sense for ap mode right now */
481 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
482 (vap->iv_caps & IEEE80211_C_DFS))
483 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
485 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
486 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
487 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
489 * Install a default reset method for the ioctl support;
490 * the driver can override this.
492 vap->iv_reset = default_reset;
494 IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
496 ieee80211_sysctl_vattach(vap);
497 ieee80211_crypto_vattach(vap);
498 ieee80211_node_vattach(vap);
499 ieee80211_power_vattach(vap);
500 ieee80211_proto_vattach(vap);
501 #ifdef IEEE80211_SUPPORT_SUPERG
502 ieee80211_superg_vattach(vap);
503 #endif
504 ieee80211_ht_vattach(vap);
505 ieee80211_scan_vattach(vap);
506 ieee80211_regdomain_vattach(vap);
507 ieee80211_radiotap_vattach(vap);
508 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
510 return 0;
514 * Activate a vap. State should have been prepared with a
515 * call to ieee80211_vap_setup and by the driver. On return
516 * from this call the vap is ready for use.
519 ieee80211_vap_attach(struct ieee80211vap *vap,
520 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
522 struct ifnet *ifp = vap->iv_ifp;
523 struct ieee80211com *ic = vap->iv_ic;
524 struct ifmediareq imr;
525 int maxrate;
527 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
528 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
529 __func__, ieee80211_opmode_name[vap->iv_opmode],
530 ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
533 * Do late attach work that cannot happen until after
534 * the driver has had a chance to override defaults.
536 ieee80211_node_latevattach(vap);
537 ieee80211_power_latevattach(vap);
539 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
540 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
541 ieee80211_media_status(ifp, &imr);
542 /* NB: strip explicit mode; we're actually in autoselect */
543 ifmedia_set(&vap->iv_media,
544 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
545 if (maxrate)
546 ifp->if_baudrate = IF_Mbps(maxrate);
548 ether_ifattach(ifp, vap->iv_myaddr);
549 if (vap->iv_opmode == IEEE80211_M_MONITOR) {
550 /* NB: disallow transmit */
551 ifp->if_transmit = null_transmit;
552 ifp->if_output = null_output;
553 } else {
554 /* hook output method setup by ether_ifattach */
555 vap->iv_output = ifp->if_output;
556 ifp->if_output = ieee80211_output;
558 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
560 IEEE80211_LOCK(ic);
561 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
562 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
563 #ifdef IEEE80211_SUPPORT_SUPERG
564 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
565 #endif
566 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
567 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
568 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
569 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
570 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
571 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
572 IEEE80211_UNLOCK(ic);
574 return 1;
578 * Tear down vap state and reclaim the ifnet.
579 * The driver is assumed to have prepared for
580 * this; e.g. by turning off interrupts for the
581 * underlying device.
583 void
584 ieee80211_vap_detach(struct ieee80211vap *vap)
586 struct ieee80211com *ic = vap->iv_ic;
587 struct ifnet *ifp = vap->iv_ifp;
589 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
590 __func__, ieee80211_opmode_name[vap->iv_opmode],
591 ic->ic_ifp->if_xname);
593 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
594 ether_ifdetach(ifp);
596 ieee80211_stop(vap);
599 * Flush any deferred vap tasks.
601 ieee80211_draintask(ic, &vap->iv_nstate_task);
602 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
604 IEEE80211_LOCK(ic);
605 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
606 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
607 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
608 #ifdef IEEE80211_SUPPORT_SUPERG
609 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
610 #endif
611 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
612 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
613 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
614 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
615 /* NB: this handles the bpfdetach done below */
616 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
617 ieee80211_syncifflag_locked(ic, IFF_PROMISC);
618 ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
619 IEEE80211_UNLOCK(ic);
621 ifmedia_removeall(&vap->iv_media);
623 ieee80211_radiotap_vdetach(vap);
624 ieee80211_regdomain_vdetach(vap);
625 ieee80211_scan_vdetach(vap);
626 #ifdef IEEE80211_SUPPORT_SUPERG
627 ieee80211_superg_vdetach(vap);
628 #endif
629 ieee80211_ht_vdetach(vap);
630 /* NB: must be before ieee80211_node_vdetach */
631 ieee80211_proto_vdetach(vap);
632 ieee80211_crypto_vdetach(vap);
633 ieee80211_power_vdetach(vap);
634 ieee80211_node_vdetach(vap);
635 ieee80211_sysctl_vdetach(vap);
637 if_free(ifp);
641 * Synchronize flag bit state in the parent ifnet structure
642 * according to the state of all vap ifnet's. This is used,
643 * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
645 void
646 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
648 struct ifnet *ifp = ic->ic_ifp;
649 struct ieee80211vap *vap;
650 int bit, oflags;
652 IEEE80211_LOCK_ASSERT(ic);
654 bit = 0;
655 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
656 if (vap->iv_ifp->if_flags & flag) {
658 * XXX the bridge sets PROMISC but we don't want to
659 * enable it on the device, discard here so all the
660 * drivers don't need to special-case it
662 if (flag == IFF_PROMISC &&
663 !(vap->iv_opmode == IEEE80211_M_MONITOR ||
664 (vap->iv_opmode == IEEE80211_M_AHDEMO &&
665 (vap->iv_caps & IEEE80211_C_TDMA) == 0)))
666 continue;
667 bit = 1;
668 break;
670 oflags = ifp->if_flags;
671 if (bit)
672 ifp->if_flags |= flag;
673 else
674 ifp->if_flags &= ~flag;
675 if ((ifp->if_flags ^ oflags) & flag) {
676 /* XXX should we return 1/0 and let caller do this? */
677 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
678 if (flag == IFF_PROMISC)
679 ieee80211_runtask(ic, &ic->ic_promisc_task);
680 else if (flag == IFF_ALLMULTI)
681 ieee80211_runtask(ic, &ic->ic_mcast_task);
687 * Synchronize flag bit state in the com structure
688 * according to the state of all vap's. This is used,
689 * for example, to handle state changes via ioctls.
691 static void
692 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
694 struct ieee80211vap *vap;
695 int bit;
697 IEEE80211_LOCK_ASSERT(ic);
699 bit = 0;
700 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
701 if (vap->iv_flags & flag) {
702 bit = 1;
703 break;
705 if (bit)
706 ic->ic_flags |= flag;
707 else
708 ic->ic_flags &= ~flag;
711 void
712 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
714 struct ieee80211com *ic = vap->iv_ic;
716 IEEE80211_LOCK(ic);
717 if (flag < 0) {
718 flag = -flag;
719 vap->iv_flags &= ~flag;
720 } else
721 vap->iv_flags |= flag;
722 ieee80211_syncflag_locked(ic, flag);
723 IEEE80211_UNLOCK(ic);
727 * Synchronize flags_ht bit state in the com structure
728 * according to the state of all vap's. This is used,
729 * for example, to handle state changes via ioctls.
731 static void
732 ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
734 struct ieee80211vap *vap;
735 int bit;
737 IEEE80211_LOCK_ASSERT(ic);
739 bit = 0;
740 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
741 if (vap->iv_flags_ht & flag) {
742 bit = 1;
743 break;
745 if (bit)
746 ic->ic_flags_ht |= flag;
747 else
748 ic->ic_flags_ht &= ~flag;
751 void
752 ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
754 struct ieee80211com *ic = vap->iv_ic;
756 IEEE80211_LOCK(ic);
757 if (flag < 0) {
758 flag = -flag;
759 vap->iv_flags_ht &= ~flag;
760 } else
761 vap->iv_flags_ht |= flag;
762 ieee80211_syncflag_ht_locked(ic, flag);
763 IEEE80211_UNLOCK(ic);
767 * Synchronize flags_ext bit state in the com structure
768 * according to the state of all vap's. This is used,
769 * for example, to handle state changes via ioctls.
771 static void
772 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
774 struct ieee80211vap *vap;
775 int bit;
777 IEEE80211_LOCK_ASSERT(ic);
779 bit = 0;
780 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
781 if (vap->iv_flags_ext & flag) {
782 bit = 1;
783 break;
785 if (bit)
786 ic->ic_flags_ext |= flag;
787 else
788 ic->ic_flags_ext &= ~flag;
791 void
792 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
794 struct ieee80211com *ic = vap->iv_ic;
796 IEEE80211_LOCK(ic);
797 if (flag < 0) {
798 flag = -flag;
799 vap->iv_flags_ext &= ~flag;
800 } else
801 vap->iv_flags_ext |= flag;
802 ieee80211_syncflag_ext_locked(ic, flag);
803 IEEE80211_UNLOCK(ic);
806 static __inline int
807 mapgsm(u_int freq, u_int flags)
809 freq *= 10;
810 if (flags & IEEE80211_CHAN_QUARTER)
811 freq += 5;
812 else if (flags & IEEE80211_CHAN_HALF)
813 freq += 10;
814 else
815 freq += 20;
816 /* NB: there is no 907/20 wide but leave room */
817 return (freq - 906*10) / 5;
820 static __inline int
821 mappsb(u_int freq, u_int flags)
823 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
827 * Convert MHz frequency to IEEE channel number.
830 ieee80211_mhz2ieee(u_int freq, u_int flags)
832 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
833 if (flags & IEEE80211_CHAN_GSM)
834 return mapgsm(freq, flags);
835 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
836 if (freq == 2484)
837 return 14;
838 if (freq < 2484)
839 return ((int) freq - 2407) / 5;
840 else
841 return 15 + ((freq - 2512) / 20);
842 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
843 if (freq <= 5000) {
844 /* XXX check regdomain? */
845 if (IS_FREQ_IN_PSB(freq))
846 return mappsb(freq, flags);
847 return (freq - 4000) / 5;
848 } else
849 return (freq - 5000) / 5;
850 } else { /* either, guess */
851 if (freq == 2484)
852 return 14;
853 if (freq < 2484) {
854 if (907 <= freq && freq <= 922)
855 return mapgsm(freq, flags);
856 return ((int) freq - 2407) / 5;
858 if (freq < 5000) {
859 if (IS_FREQ_IN_PSB(freq))
860 return mappsb(freq, flags);
861 else if (freq > 4900)
862 return (freq - 4000) / 5;
863 else
864 return 15 + ((freq - 2512) / 20);
866 return (freq - 5000) / 5;
868 #undef IS_FREQ_IN_PSB
872 * Convert channel to IEEE channel number.
875 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
877 if (c == NULL) {
878 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
879 return 0; /* XXX */
881 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
885 * Convert IEEE channel number to MHz frequency.
887 u_int
888 ieee80211_ieee2mhz(u_int chan, u_int flags)
890 if (flags & IEEE80211_CHAN_GSM)
891 return 907 + 5 * (chan / 10);
892 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
893 if (chan == 14)
894 return 2484;
895 if (chan < 14)
896 return 2407 + chan*5;
897 else
898 return 2512 + ((chan-15)*20);
899 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
900 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
901 chan -= 37;
902 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
904 return 5000 + (chan*5);
905 } else { /* either, guess */
906 /* XXX can't distinguish PSB+GSM channels */
907 if (chan == 14)
908 return 2484;
909 if (chan < 14) /* 0-13 */
910 return 2407 + chan*5;
911 if (chan < 27) /* 15-26 */
912 return 2512 + ((chan-15)*20);
913 return 5000 + (chan*5);
918 * Locate a channel given a frequency+flags. We cache
919 * the previous lookup to optimize switching between two
920 * channels--as happens with dynamic turbo.
922 struct ieee80211_channel *
923 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
925 struct ieee80211_channel *c;
926 int i;
928 flags &= IEEE80211_CHAN_ALLTURBO;
929 c = ic->ic_prevchan;
930 if (c != NULL && c->ic_freq == freq &&
931 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
932 return c;
933 /* brute force search */
934 for (i = 0; i < ic->ic_nchans; i++) {
935 c = &ic->ic_channels[i];
936 if (c->ic_freq == freq &&
937 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
938 return c;
940 return NULL;
944 * Locate a channel given a channel number+flags. We cache
945 * the previous lookup to optimize switching between two
946 * channels--as happens with dynamic turbo.
948 struct ieee80211_channel *
949 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
951 struct ieee80211_channel *c;
952 int i;
954 flags &= IEEE80211_CHAN_ALLTURBO;
955 c = ic->ic_prevchan;
956 if (c != NULL && c->ic_ieee == ieee &&
957 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
958 return c;
959 /* brute force search */
960 for (i = 0; i < ic->ic_nchans; i++) {
961 c = &ic->ic_channels[i];
962 if (c->ic_ieee == ieee &&
963 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
964 return c;
966 return NULL;
969 static void
970 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
972 #define ADD(_ic, _s, _o) \
973 ifmedia_add(media, \
974 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
975 static const u_int mopts[IEEE80211_MODE_MAX] = {
976 [IEEE80211_MODE_AUTO] = IFM_AUTO,
977 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
978 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
979 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
980 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
981 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
982 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
983 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
984 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
985 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
986 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
987 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
989 u_int mopt;
991 mopt = mopts[mode];
992 if (addsta)
993 ADD(ic, mword, mopt); /* STA mode has no cap */
994 if (caps & IEEE80211_C_IBSS)
995 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
996 if (caps & IEEE80211_C_HOSTAP)
997 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
998 if (caps & IEEE80211_C_AHDEMO)
999 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1000 if (caps & IEEE80211_C_MONITOR)
1001 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1002 if (caps & IEEE80211_C_WDS)
1003 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1004 if (caps & IEEE80211_C_MBSS)
1005 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1006 #undef ADD
1010 * Setup the media data structures according to the channel and
1011 * rate tables.
1013 static int
1014 ieee80211_media_setup(struct ieee80211com *ic,
1015 struct ifmedia *media, int caps, int addsta,
1016 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1018 int i, j, mode, rate, maxrate, mword, r;
1019 const struct ieee80211_rateset *rs;
1020 struct ieee80211_rateset allrates;
1023 * Fill in media characteristics.
1025 ifmedia_init(media, 0, media_change, media_stat);
1026 maxrate = 0;
1028 * Add media for legacy operating modes.
1030 memset(&allrates, 0, sizeof(allrates));
1031 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1032 if (isclr(ic->ic_modecaps, mode))
1033 continue;
1034 addmedia(media, caps, addsta, mode, IFM_AUTO);
1035 if (mode == IEEE80211_MODE_AUTO)
1036 continue;
1037 rs = &ic->ic_sup_rates[mode];
1038 for (i = 0; i < rs->rs_nrates; i++) {
1039 rate = rs->rs_rates[i];
1040 mword = ieee80211_rate2media(ic, rate, mode);
1041 if (mword == 0)
1042 continue;
1043 addmedia(media, caps, addsta, mode, mword);
1045 * Add legacy rate to the collection of all rates.
1047 r = rate & IEEE80211_RATE_VAL;
1048 for (j = 0; j < allrates.rs_nrates; j++)
1049 if (allrates.rs_rates[j] == r)
1050 break;
1051 if (j == allrates.rs_nrates) {
1052 /* unique, add to the set */
1053 allrates.rs_rates[j] = r;
1054 allrates.rs_nrates++;
1056 rate = (rate & IEEE80211_RATE_VAL) / 2;
1057 if (rate > maxrate)
1058 maxrate = rate;
1061 for (i = 0; i < allrates.rs_nrates; i++) {
1062 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1063 IEEE80211_MODE_AUTO);
1064 if (mword == 0)
1065 continue;
1066 /* NB: remove media options from mword */
1067 addmedia(media, caps, addsta,
1068 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1071 * Add HT/11n media. Note that we do not have enough
1072 * bits in the media subtype to express the MCS so we
1073 * use a "placeholder" media subtype and any fixed MCS
1074 * must be specified with a different mechanism.
1076 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1077 if (isclr(ic->ic_modecaps, mode))
1078 continue;
1079 addmedia(media, caps, addsta, mode, IFM_AUTO);
1080 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1082 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1083 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1084 addmedia(media, caps, addsta,
1085 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1086 i = ic->ic_txstream * 8 - 1;
1087 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1088 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1089 rate = ieee80211_htrates[i].ht40_rate_400ns;
1090 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1091 rate = ieee80211_htrates[i].ht40_rate_800ns;
1092 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1093 rate = ieee80211_htrates[i].ht20_rate_400ns;
1094 else
1095 rate = ieee80211_htrates[i].ht20_rate_800ns;
1096 if (rate > maxrate)
1097 maxrate = rate;
1099 return maxrate;
1102 void
1103 ieee80211_media_init(struct ieee80211com *ic)
1105 struct ifnet *ifp = ic->ic_ifp;
1106 int maxrate;
1108 /* NB: this works because the structure is initialized to zero */
1109 if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
1111 * We are re-initializing the channel list; clear
1112 * the existing media state as the media routines
1113 * don't suppress duplicates.
1115 ifmedia_removeall(&ic->ic_media);
1117 ieee80211_chan_init(ic);
1120 * Recalculate media settings in case new channel list changes
1121 * the set of available modes.
1123 maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1124 ieee80211com_media_change, ieee80211com_media_status);
1125 /* NB: strip explicit mode; we're actually in autoselect */
1126 ifmedia_set(&ic->ic_media,
1127 media_status(ic->ic_opmode, ic->ic_curchan) &~
1128 (IFM_MMASK | IFM_IEEE80211_TURBO));
1129 if (maxrate)
1130 ifp->if_baudrate = IF_Mbps(maxrate);
1132 /* XXX need to propagate new media settings to vap's */
1135 /* XXX inline or eliminate? */
1136 const struct ieee80211_rateset *
1137 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1139 /* XXX does this work for 11ng basic rates? */
1140 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1143 void
1144 ieee80211_announce(struct ieee80211com *ic)
1146 struct ifnet *ifp = ic->ic_ifp;
1147 int i, mode, rate, mword;
1148 const struct ieee80211_rateset *rs;
1150 /* NB: skip AUTO since it has no rates */
1151 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1152 if (isclr(ic->ic_modecaps, mode))
1153 continue;
1154 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1155 rs = &ic->ic_sup_rates[mode];
1156 for (i = 0; i < rs->rs_nrates; i++) {
1157 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1158 if (mword == 0)
1159 continue;
1160 rate = ieee80211_media2rate(mword);
1161 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1162 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1164 printf("\n");
1166 ieee80211_ht_announce(ic);
1169 void
1170 ieee80211_announce_channels(struct ieee80211com *ic)
1172 const struct ieee80211_channel *c;
1173 char type;
1174 int i, cw;
1176 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1177 for (i = 0; i < ic->ic_nchans; i++) {
1178 c = &ic->ic_channels[i];
1179 if (IEEE80211_IS_CHAN_ST(c))
1180 type = 'S';
1181 else if (IEEE80211_IS_CHAN_108A(c))
1182 type = 'T';
1183 else if (IEEE80211_IS_CHAN_108G(c))
1184 type = 'G';
1185 else if (IEEE80211_IS_CHAN_HT(c))
1186 type = 'n';
1187 else if (IEEE80211_IS_CHAN_A(c))
1188 type = 'a';
1189 else if (IEEE80211_IS_CHAN_ANYG(c))
1190 type = 'g';
1191 else if (IEEE80211_IS_CHAN_B(c))
1192 type = 'b';
1193 else
1194 type = 'f';
1195 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1196 cw = 40;
1197 else if (IEEE80211_IS_CHAN_HALF(c))
1198 cw = 10;
1199 else if (IEEE80211_IS_CHAN_QUARTER(c))
1200 cw = 5;
1201 else
1202 cw = 20;
1203 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1204 , c->ic_ieee, c->ic_freq, type
1205 , cw
1206 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1207 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1208 , c->ic_maxregpower
1209 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1210 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1215 static int
1216 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1218 switch (IFM_MODE(ime->ifm_media)) {
1219 case IFM_IEEE80211_11A:
1220 *mode = IEEE80211_MODE_11A;
1221 break;
1222 case IFM_IEEE80211_11B:
1223 *mode = IEEE80211_MODE_11B;
1224 break;
1225 case IFM_IEEE80211_11G:
1226 *mode = IEEE80211_MODE_11G;
1227 break;
1228 case IFM_IEEE80211_FH:
1229 *mode = IEEE80211_MODE_FH;
1230 break;
1231 case IFM_IEEE80211_11NA:
1232 *mode = IEEE80211_MODE_11NA;
1233 break;
1234 case IFM_IEEE80211_11NG:
1235 *mode = IEEE80211_MODE_11NG;
1236 break;
1237 case IFM_AUTO:
1238 *mode = IEEE80211_MODE_AUTO;
1239 break;
1240 default:
1241 return 0;
1244 * Turbo mode is an ``option''.
1245 * XXX does not apply to AUTO
1247 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1248 if (*mode == IEEE80211_MODE_11A) {
1249 if (flags & IEEE80211_F_TURBOP)
1250 *mode = IEEE80211_MODE_TURBO_A;
1251 else
1252 *mode = IEEE80211_MODE_STURBO_A;
1253 } else if (*mode == IEEE80211_MODE_11G)
1254 *mode = IEEE80211_MODE_TURBO_G;
1255 else
1256 return 0;
1258 /* XXX HT40 +/- */
1259 return 1;
1263 * Handle a media change request on the underlying interface.
1266 ieee80211com_media_change(struct ifnet *ifp)
1268 return EINVAL;
1272 * Handle a media change request on the vap interface.
1275 ieee80211_media_change(struct ifnet *ifp)
1277 struct ieee80211vap *vap = ifp->if_softc;
1278 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1279 uint16_t newmode;
1281 if (!media2mode(ime, vap->iv_flags, &newmode))
1282 return EINVAL;
1283 if (vap->iv_des_mode != newmode) {
1284 vap->iv_des_mode = newmode;
1285 /* XXX kick state machine if up+running */
1287 return 0;
1291 * Common code to calculate the media status word
1292 * from the operating mode and channel state.
1294 static int
1295 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1297 int status;
1299 status = IFM_IEEE80211;
1300 switch (opmode) {
1301 case IEEE80211_M_STA:
1302 break;
1303 case IEEE80211_M_IBSS:
1304 status |= IFM_IEEE80211_ADHOC;
1305 break;
1306 case IEEE80211_M_HOSTAP:
1307 status |= IFM_IEEE80211_HOSTAP;
1308 break;
1309 case IEEE80211_M_MONITOR:
1310 status |= IFM_IEEE80211_MONITOR;
1311 break;
1312 case IEEE80211_M_AHDEMO:
1313 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1314 break;
1315 case IEEE80211_M_WDS:
1316 status |= IFM_IEEE80211_WDS;
1317 break;
1318 case IEEE80211_M_MBSS:
1319 status |= IFM_IEEE80211_MBSS;
1320 break;
1322 if (IEEE80211_IS_CHAN_HTA(chan)) {
1323 status |= IFM_IEEE80211_11NA;
1324 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1325 status |= IFM_IEEE80211_11NG;
1326 } else if (IEEE80211_IS_CHAN_A(chan)) {
1327 status |= IFM_IEEE80211_11A;
1328 } else if (IEEE80211_IS_CHAN_B(chan)) {
1329 status |= IFM_IEEE80211_11B;
1330 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1331 status |= IFM_IEEE80211_11G;
1332 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1333 status |= IFM_IEEE80211_FH;
1335 /* XXX else complain? */
1337 if (IEEE80211_IS_CHAN_TURBO(chan))
1338 status |= IFM_IEEE80211_TURBO;
1339 #if 0
1340 if (IEEE80211_IS_CHAN_HT20(chan))
1341 status |= IFM_IEEE80211_HT20;
1342 if (IEEE80211_IS_CHAN_HT40(chan))
1343 status |= IFM_IEEE80211_HT40;
1344 #endif
1345 return status;
1348 static void
1349 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1351 struct ieee80211com *ic = ifp->if_l2com;
1352 struct ieee80211vap *vap;
1354 imr->ifm_status = IFM_AVALID;
1355 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1356 if (vap->iv_ifp->if_flags & IFF_UP) {
1357 imr->ifm_status |= IFM_ACTIVE;
1358 break;
1360 imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1361 if (imr->ifm_status & IFM_ACTIVE)
1362 imr->ifm_current = imr->ifm_active;
1365 void
1366 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1368 struct ieee80211vap *vap = ifp->if_softc;
1369 struct ieee80211com *ic = vap->iv_ic;
1370 enum ieee80211_phymode mode;
1372 imr->ifm_status = IFM_AVALID;
1374 * NB: use the current channel's mode to lock down a xmit
1375 * rate only when running; otherwise we may have a mismatch
1376 * in which case the rate will not be convertible.
1378 if (vap->iv_state == IEEE80211_S_RUN) {
1379 imr->ifm_status |= IFM_ACTIVE;
1380 mode = ieee80211_chan2mode(ic->ic_curchan);
1381 } else
1382 mode = IEEE80211_MODE_AUTO;
1383 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1385 * Calculate a current rate if possible.
1387 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1389 * A fixed rate is set, report that.
1391 imr->ifm_active |= ieee80211_rate2media(ic,
1392 vap->iv_txparms[mode].ucastrate, mode);
1393 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1395 * In station mode report the current transmit rate.
1397 imr->ifm_active |= ieee80211_rate2media(ic,
1398 vap->iv_bss->ni_txrate, mode);
1399 } else
1400 imr->ifm_active |= IFM_AUTO;
1401 if (imr->ifm_status & IFM_ACTIVE)
1402 imr->ifm_current = imr->ifm_active;
1406 * Set the current phy mode and recalculate the active channel
1407 * set based on the available channels for this mode. Also
1408 * select a new default/current channel if the current one is
1409 * inappropriate for this mode.
1412 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1415 * Adjust basic rates in 11b/11g supported rate set.
1416 * Note that if operating on a hal/quarter rate channel
1417 * this is a noop as those rates sets are different
1418 * and used instead.
1420 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1421 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1423 ic->ic_curmode = mode;
1424 ieee80211_reset_erp(ic); /* reset ERP state */
1426 return 0;
1430 * Return the phy mode for with the specified channel.
1432 enum ieee80211_phymode
1433 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1436 if (IEEE80211_IS_CHAN_HTA(chan))
1437 return IEEE80211_MODE_11NA;
1438 else if (IEEE80211_IS_CHAN_HTG(chan))
1439 return IEEE80211_MODE_11NG;
1440 else if (IEEE80211_IS_CHAN_108G(chan))
1441 return IEEE80211_MODE_TURBO_G;
1442 else if (IEEE80211_IS_CHAN_ST(chan))
1443 return IEEE80211_MODE_STURBO_A;
1444 else if (IEEE80211_IS_CHAN_TURBO(chan))
1445 return IEEE80211_MODE_TURBO_A;
1446 else if (IEEE80211_IS_CHAN_HALF(chan))
1447 return IEEE80211_MODE_HALF;
1448 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1449 return IEEE80211_MODE_QUARTER;
1450 else if (IEEE80211_IS_CHAN_A(chan))
1451 return IEEE80211_MODE_11A;
1452 else if (IEEE80211_IS_CHAN_ANYG(chan))
1453 return IEEE80211_MODE_11G;
1454 else if (IEEE80211_IS_CHAN_B(chan))
1455 return IEEE80211_MODE_11B;
1456 else if (IEEE80211_IS_CHAN_FHSS(chan))
1457 return IEEE80211_MODE_FH;
1459 /* NB: should not get here */
1460 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1461 __func__, chan->ic_freq, chan->ic_flags);
1462 return IEEE80211_MODE_11B;
1465 struct ratemedia {
1466 u_int match; /* rate + mode */
1467 u_int media; /* if_media rate */
1470 static int
1471 findmedia(const struct ratemedia rates[], int n, u_int match)
1473 int i;
1475 for (i = 0; i < n; i++)
1476 if (rates[i].match == match)
1477 return rates[i].media;
1478 return IFM_AUTO;
1482 * Convert IEEE80211 rate value to ifmedia subtype.
1483 * Rate is either a legacy rate in units of 0.5Mbps
1484 * or an MCS index.
1487 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1489 #define N(a) (sizeof(a) / sizeof(a[0]))
1490 static const struct ratemedia rates[] = {
1491 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1492 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1493 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1494 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1495 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1496 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1497 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1498 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1499 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1500 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1501 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1502 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1503 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1504 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1505 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1506 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1507 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1508 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1509 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1510 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1511 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1512 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1513 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1514 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1515 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1516 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1517 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1518 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1519 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1520 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1521 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1523 static const struct ratemedia htrates[] = {
1524 { 0, IFM_IEEE80211_MCS },
1525 { 1, IFM_IEEE80211_MCS },
1526 { 2, IFM_IEEE80211_MCS },
1527 { 3, IFM_IEEE80211_MCS },
1528 { 4, IFM_IEEE80211_MCS },
1529 { 5, IFM_IEEE80211_MCS },
1530 { 6, IFM_IEEE80211_MCS },
1531 { 7, IFM_IEEE80211_MCS },
1532 { 8, IFM_IEEE80211_MCS },
1533 { 9, IFM_IEEE80211_MCS },
1534 { 10, IFM_IEEE80211_MCS },
1535 { 11, IFM_IEEE80211_MCS },
1536 { 12, IFM_IEEE80211_MCS },
1537 { 13, IFM_IEEE80211_MCS },
1538 { 14, IFM_IEEE80211_MCS },
1539 { 15, IFM_IEEE80211_MCS },
1540 { 16, IFM_IEEE80211_MCS },
1541 { 17, IFM_IEEE80211_MCS },
1542 { 18, IFM_IEEE80211_MCS },
1543 { 19, IFM_IEEE80211_MCS },
1544 { 20, IFM_IEEE80211_MCS },
1545 { 21, IFM_IEEE80211_MCS },
1546 { 22, IFM_IEEE80211_MCS },
1547 { 23, IFM_IEEE80211_MCS },
1548 { 24, IFM_IEEE80211_MCS },
1549 { 25, IFM_IEEE80211_MCS },
1550 { 26, IFM_IEEE80211_MCS },
1551 { 27, IFM_IEEE80211_MCS },
1552 { 28, IFM_IEEE80211_MCS },
1553 { 29, IFM_IEEE80211_MCS },
1554 { 30, IFM_IEEE80211_MCS },
1555 { 31, IFM_IEEE80211_MCS },
1556 { 32, IFM_IEEE80211_MCS },
1557 { 33, IFM_IEEE80211_MCS },
1558 { 34, IFM_IEEE80211_MCS },
1559 { 35, IFM_IEEE80211_MCS },
1560 { 36, IFM_IEEE80211_MCS },
1561 { 37, IFM_IEEE80211_MCS },
1562 { 38, IFM_IEEE80211_MCS },
1563 { 39, IFM_IEEE80211_MCS },
1564 { 40, IFM_IEEE80211_MCS },
1565 { 41, IFM_IEEE80211_MCS },
1566 { 42, IFM_IEEE80211_MCS },
1567 { 43, IFM_IEEE80211_MCS },
1568 { 44, IFM_IEEE80211_MCS },
1569 { 45, IFM_IEEE80211_MCS },
1570 { 46, IFM_IEEE80211_MCS },
1571 { 47, IFM_IEEE80211_MCS },
1572 { 48, IFM_IEEE80211_MCS },
1573 { 49, IFM_IEEE80211_MCS },
1574 { 50, IFM_IEEE80211_MCS },
1575 { 51, IFM_IEEE80211_MCS },
1576 { 52, IFM_IEEE80211_MCS },
1577 { 53, IFM_IEEE80211_MCS },
1578 { 54, IFM_IEEE80211_MCS },
1579 { 55, IFM_IEEE80211_MCS },
1580 { 56, IFM_IEEE80211_MCS },
1581 { 57, IFM_IEEE80211_MCS },
1582 { 58, IFM_IEEE80211_MCS },
1583 { 59, IFM_IEEE80211_MCS },
1584 { 60, IFM_IEEE80211_MCS },
1585 { 61, IFM_IEEE80211_MCS },
1586 { 62, IFM_IEEE80211_MCS },
1587 { 63, IFM_IEEE80211_MCS },
1588 { 64, IFM_IEEE80211_MCS },
1589 { 65, IFM_IEEE80211_MCS },
1590 { 66, IFM_IEEE80211_MCS },
1591 { 67, IFM_IEEE80211_MCS },
1592 { 68, IFM_IEEE80211_MCS },
1593 { 69, IFM_IEEE80211_MCS },
1594 { 70, IFM_IEEE80211_MCS },
1595 { 71, IFM_IEEE80211_MCS },
1596 { 72, IFM_IEEE80211_MCS },
1597 { 73, IFM_IEEE80211_MCS },
1598 { 74, IFM_IEEE80211_MCS },
1599 { 75, IFM_IEEE80211_MCS },
1600 { 76, IFM_IEEE80211_MCS },
1602 int m;
1605 * Check 11n rates first for match as an MCS.
1607 if (mode == IEEE80211_MODE_11NA) {
1608 if (rate & IEEE80211_RATE_MCS) {
1609 rate &= ~IEEE80211_RATE_MCS;
1610 m = findmedia(htrates, N(htrates), rate);
1611 if (m != IFM_AUTO)
1612 return m | IFM_IEEE80211_11NA;
1614 } else if (mode == IEEE80211_MODE_11NG) {
1615 /* NB: 12 is ambiguous, it will be treated as an MCS */
1616 if (rate & IEEE80211_RATE_MCS) {
1617 rate &= ~IEEE80211_RATE_MCS;
1618 m = findmedia(htrates, N(htrates), rate);
1619 if (m != IFM_AUTO)
1620 return m | IFM_IEEE80211_11NG;
1623 rate &= IEEE80211_RATE_VAL;
1624 switch (mode) {
1625 case IEEE80211_MODE_11A:
1626 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1627 case IEEE80211_MODE_QUARTER:
1628 case IEEE80211_MODE_11NA:
1629 case IEEE80211_MODE_TURBO_A:
1630 case IEEE80211_MODE_STURBO_A:
1631 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1632 case IEEE80211_MODE_11B:
1633 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1634 case IEEE80211_MODE_FH:
1635 return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1636 case IEEE80211_MODE_AUTO:
1637 /* NB: ic may be NULL for some drivers */
1638 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1639 return findmedia(rates, N(rates),
1640 rate | IFM_IEEE80211_FH);
1641 /* NB: hack, 11g matches both 11b+11a rates */
1642 /* fall thru... */
1643 case IEEE80211_MODE_11G:
1644 case IEEE80211_MODE_11NG:
1645 case IEEE80211_MODE_TURBO_G:
1646 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1648 return IFM_AUTO;
1649 #undef N
1653 ieee80211_media2rate(int mword)
1655 #define N(a) (sizeof(a) / sizeof(a[0]))
1656 static const int ieeerates[] = {
1657 -1, /* IFM_AUTO */
1658 0, /* IFM_MANUAL */
1659 0, /* IFM_NONE */
1660 2, /* IFM_IEEE80211_FH1 */
1661 4, /* IFM_IEEE80211_FH2 */
1662 2, /* IFM_IEEE80211_DS1 */
1663 4, /* IFM_IEEE80211_DS2 */
1664 11, /* IFM_IEEE80211_DS5 */
1665 22, /* IFM_IEEE80211_DS11 */
1666 44, /* IFM_IEEE80211_DS22 */
1667 12, /* IFM_IEEE80211_OFDM6 */
1668 18, /* IFM_IEEE80211_OFDM9 */
1669 24, /* IFM_IEEE80211_OFDM12 */
1670 36, /* IFM_IEEE80211_OFDM18 */
1671 48, /* IFM_IEEE80211_OFDM24 */
1672 72, /* IFM_IEEE80211_OFDM36 */
1673 96, /* IFM_IEEE80211_OFDM48 */
1674 108, /* IFM_IEEE80211_OFDM54 */
1675 144, /* IFM_IEEE80211_OFDM72 */
1676 0, /* IFM_IEEE80211_DS354k */
1677 0, /* IFM_IEEE80211_DS512k */
1678 6, /* IFM_IEEE80211_OFDM3 */
1679 9, /* IFM_IEEE80211_OFDM4 */
1680 54, /* IFM_IEEE80211_OFDM27 */
1681 -1, /* IFM_IEEE80211_MCS */
1683 return IFM_SUBTYPE(mword) < N(ieeerates) ?
1684 ieeerates[IFM_SUBTYPE(mword)] : 0;
1685 #undef N
1689 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
1690 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
1692 #define mix(a, b, c) \
1693 do { \
1694 a -= b; a -= c; a ^= (c >> 13); \
1695 b -= c; b -= a; b ^= (a << 8); \
1696 c -= a; c -= b; c ^= (b >> 13); \
1697 a -= b; a -= c; a ^= (c >> 12); \
1698 b -= c; b -= a; b ^= (a << 16); \
1699 c -= a; c -= b; c ^= (b >> 5); \
1700 a -= b; a -= c; a ^= (c >> 3); \
1701 b -= c; b -= a; b ^= (a << 10); \
1702 c -= a; c -= b; c ^= (b >> 15); \
1703 } while (/*CONSTCOND*/0)
1705 uint32_t
1706 ieee80211_mac_hash(const struct ieee80211com *ic,
1707 const uint8_t addr[IEEE80211_ADDR_LEN])
1709 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
1711 b += addr[5] << 8;
1712 b += addr[4];
1713 a += addr[3] << 24;
1714 a += addr[2] << 16;
1715 a += addr[1] << 8;
1716 a += addr[0];
1718 mix(a, b, c);
1720 return c;
1722 #undef mix