]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/net80211/ieee80211.c
Merge tcpdump 4.0.0 from the vendor branch.
[FreeBSD/FreeBSD.git] / sys / net80211 / ieee80211.c
1 /*-
2  * Copyright (c) 2001 Atsushi Onoe
3  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  *
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.
14  *
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.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * IEEE 802.11 generic handler
32  */
33 #include "opt_wlan.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h> 
37 #include <sys/kernel.h>
38
39 #include <sys/socket.h>
40
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>
46
47 #include <net80211/ieee80211_var.h>
48 #include <net80211/ieee80211_regdomain.h>
49
50 #include <net/bpf.h>
51
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_HALF]     = "half",
62         [IEEE80211_MODE_QUARTER]  = "quarter",
63         [IEEE80211_MODE_11NA]     = "11na",
64         [IEEE80211_MODE_11NG]     = "11ng",
65 };
66 /* map ieee80211_opmode to the corresponding capability bit */
67 const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
68         [IEEE80211_M_IBSS]      = IEEE80211_C_IBSS,
69         [IEEE80211_M_WDS]       = IEEE80211_C_WDS,
70         [IEEE80211_M_STA]       = IEEE80211_C_STA,
71         [IEEE80211_M_AHDEMO]    = IEEE80211_C_AHDEMO,
72         [IEEE80211_M_HOSTAP]    = IEEE80211_C_HOSTAP,
73         [IEEE80211_M_MONITOR]   = IEEE80211_C_MONITOR,
74 };
75
76 static const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
77         { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
78
79 static  void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
80 static  void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
81 static  int ieee80211_media_setup(struct ieee80211com *ic,
82                 struct ifmedia *media, int caps, int addsta,
83                 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
84 static  void ieee80211com_media_status(struct ifnet *, struct ifmediareq *);
85 static  int ieee80211com_media_change(struct ifnet *);
86 static  int media_status(enum ieee80211_opmode,
87                 const struct ieee80211_channel *);
88
89 MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
90
91 /*
92  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
93  */
94 #define B(r)    ((r) | IEEE80211_RATE_BASIC)
95 static const struct ieee80211_rateset ieee80211_rateset_11a =
96         { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
97 static const struct ieee80211_rateset ieee80211_rateset_half =
98         { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
99 static const struct ieee80211_rateset ieee80211_rateset_quarter =
100         { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
101 static const struct ieee80211_rateset ieee80211_rateset_11b =
102         { 4, { B(2), B(4), B(11), B(22) } };
103 /* NB: OFDM rates are handled specially based on mode */
104 static const struct ieee80211_rateset ieee80211_rateset_11g =
105         { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
106 #undef B
107
108 /*
109  * Fill in 802.11 available channel set, mark
110  * all available channels as active, and pick
111  * a default channel if not already specified.
112  */
113 static void
114 ieee80211_chan_init(struct ieee80211com *ic)
115 {
116 #define DEFAULTRATES(m, def) do { \
117         if (ic->ic_sup_rates[m].rs_nrates == 0) \
118                 ic->ic_sup_rates[m] = def; \
119 } while (0)
120         struct ieee80211_channel *c;
121         int i;
122
123         KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
124                 ("invalid number of channels specified: %u", ic->ic_nchans));
125         memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
126         memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
127         setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
128         for (i = 0; i < ic->ic_nchans; i++) {
129                 c = &ic->ic_channels[i];
130                 KASSERT(c->ic_flags != 0, ("channel with no flags"));
131                 /*
132                  * Help drivers that work only with frequencies by filling
133                  * in IEEE channel #'s if not already calculated.  Note this
134                  * mimics similar work done in ieee80211_setregdomain when
135                  * changing regulatory state.
136                  */
137                 if (c->ic_ieee == 0)
138                         c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
139                 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
140                         c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
141                             (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
142                             c->ic_flags);
143                 /* default max tx power to max regulatory */
144                 if (c->ic_maxpower == 0)
145                         c->ic_maxpower = 2*c->ic_maxregpower;
146                 setbit(ic->ic_chan_avail, c->ic_ieee);
147                 /*
148                  * Identify mode capabilities.
149                  */
150                 if (IEEE80211_IS_CHAN_A(c))
151                         setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
152                 if (IEEE80211_IS_CHAN_B(c))
153                         setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
154                 if (IEEE80211_IS_CHAN_ANYG(c))
155                         setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
156                 if (IEEE80211_IS_CHAN_FHSS(c))
157                         setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
158                 if (IEEE80211_IS_CHAN_108A(c))
159                         setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
160                 if (IEEE80211_IS_CHAN_108G(c))
161                         setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
162                 if (IEEE80211_IS_CHAN_ST(c))
163                         setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
164                 if (IEEE80211_IS_CHAN_HALF(c))
165                         setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
166                 if (IEEE80211_IS_CHAN_QUARTER(c))
167                         setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
168                 if (IEEE80211_IS_CHAN_HTA(c))
169                         setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
170                 if (IEEE80211_IS_CHAN_HTG(c))
171                         setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
172         }
173         /* initialize candidate channels to all available */
174         memcpy(ic->ic_chan_active, ic->ic_chan_avail,
175                 sizeof(ic->ic_chan_avail));
176
177         /* sort channel table to allow lookup optimizations */
178         ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
179
180         /* invalidate any previous state */
181         ic->ic_bsschan = IEEE80211_CHAN_ANYC;
182         ic->ic_prevchan = NULL;
183         ic->ic_csa_newchan = NULL;
184         /* arbitrarily pick the first channel */
185         ic->ic_curchan = &ic->ic_channels[0];
186
187         /* fillin well-known rate sets if driver has not specified */
188         DEFAULTRATES(IEEE80211_MODE_11B,         ieee80211_rateset_11b);
189         DEFAULTRATES(IEEE80211_MODE_11G,         ieee80211_rateset_11g);
190         DEFAULTRATES(IEEE80211_MODE_11A,         ieee80211_rateset_11a);
191         DEFAULTRATES(IEEE80211_MODE_TURBO_A,     ieee80211_rateset_11a);
192         DEFAULTRATES(IEEE80211_MODE_TURBO_G,     ieee80211_rateset_11g);
193         DEFAULTRATES(IEEE80211_MODE_STURBO_A,    ieee80211_rateset_11a);
194         DEFAULTRATES(IEEE80211_MODE_HALF,        ieee80211_rateset_half);
195         DEFAULTRATES(IEEE80211_MODE_QUARTER,     ieee80211_rateset_quarter);
196         DEFAULTRATES(IEEE80211_MODE_11NA,        ieee80211_rateset_11a);
197         DEFAULTRATES(IEEE80211_MODE_11NG,        ieee80211_rateset_11g);
198
199         /*
200          * Set auto mode to reset active channel state and any desired channel.
201          */
202         (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
203 #undef DEFAULTRATES
204 }
205
206 static void
207 null_update_mcast(struct ifnet *ifp)
208 {
209         if_printf(ifp, "need multicast update callback\n");
210 }
211
212 static void
213 null_update_promisc(struct ifnet *ifp)
214 {
215         if_printf(ifp, "need promiscuous mode update callback\n");
216 }
217
218 static int
219 null_output(struct ifnet *ifp, struct mbuf *m,
220         struct sockaddr *dst, struct rtentry *rt0)
221 {
222         if_printf(ifp, "discard raw packet\n");
223         m_freem(m);
224         return EIO;
225 }
226
227 static void
228 null_input(struct ifnet *ifp, struct mbuf *m)
229 {
230         if_printf(ifp, "if_input should not be called\n");
231         m_freem(m);
232 }
233
234 /*
235  * Attach/setup the common net80211 state.  Called by
236  * the driver on attach to prior to creating any vap's.
237  */
238 void
239 ieee80211_ifattach(struct ieee80211com *ic)
240 {
241         struct ifnet *ifp = ic->ic_ifp;
242         struct sockaddr_dl *sdl;
243         struct ifaddr *ifa;
244
245         KASSERT(ifp->if_type == IFT_IEEE80211, ("if_type %d", ifp->if_type));
246
247         IEEE80211_LOCK_INIT(ic, ifp->if_xname);
248         TAILQ_INIT(&ic->ic_vaps);
249         /*
250          * Fill in 802.11 available channel set, mark all
251          * available channels as active, and pick a default
252          * channel if not already specified.
253          */
254         ieee80211_media_init(ic);
255
256         ic->ic_update_mcast = null_update_mcast;
257         ic->ic_update_promisc = null_update_promisc;
258
259         ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
260         ic->ic_lintval = ic->ic_bintval;
261         ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
262
263         ieee80211_crypto_attach(ic);
264         ieee80211_node_attach(ic);
265         ieee80211_power_attach(ic);
266         ieee80211_proto_attach(ic);
267         ieee80211_ht_attach(ic);
268         ieee80211_scan_attach(ic);
269         ieee80211_regdomain_attach(ic);
270
271         ieee80211_sysctl_attach(ic);
272
273         ifp->if_addrlen = IEEE80211_ADDR_LEN;
274         ifp->if_hdrlen = 0;
275         if_attach(ifp);
276         ifp->if_mtu = IEEE80211_MTU_MAX;
277         ifp->if_broadcastaddr = ieee80211broadcastaddr;
278         ifp->if_output = null_output;
279         ifp->if_input = null_input;     /* just in case */
280         ifp->if_resolvemulti = NULL;    /* NB: callers check */
281
282         ifa = ifaddr_byindex(ifp->if_index);
283         KASSERT(ifa != NULL, ("%s: no lladdr!\n", __func__));
284         sdl = (struct sockaddr_dl *)ifa->ifa_addr;
285         sdl->sdl_type = IFT_ETHER;              /* XXX IFT_IEEE80211? */
286         sdl->sdl_alen = IEEE80211_ADDR_LEN;
287         IEEE80211_ADDR_COPY(LLADDR(sdl), ic->ic_myaddr);
288 }
289
290 /*
291  * Detach net80211 state on device detach.  Tear down
292  * all vap's and reclaim all common state prior to the
293  * device state going away.  Note we may call back into
294  * driver; it must be prepared for this.
295  */
296 void
297 ieee80211_ifdetach(struct ieee80211com *ic)
298 {
299         struct ifnet *ifp = ic->ic_ifp;
300         struct ieee80211vap *vap;
301
302         while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
303                 ieee80211_vap_destroy(vap);
304         ieee80211_waitfor_parent(ic);
305
306         ieee80211_sysctl_detach(ic);
307         ieee80211_regdomain_detach(ic);
308         ieee80211_scan_detach(ic);
309         ieee80211_ht_detach(ic);
310         /* NB: must be called before ieee80211_node_detach */
311         ieee80211_proto_detach(ic);
312         ieee80211_crypto_detach(ic);
313         ieee80211_power_detach(ic);
314         ieee80211_node_detach(ic);
315         ifmedia_removeall(&ic->ic_media);
316
317         IEEE80211_LOCK_DESTROY(ic);
318         if_detach(ifp);
319 }
320
321 /*
322  * Default reset method for use with the ioctl support.  This
323  * method is invoked after any state change in the 802.11
324  * layer that should be propagated to the hardware but not
325  * require re-initialization of the 802.11 state machine (e.g
326  * rescanning for an ap).  We always return ENETRESET which
327  * should cause the driver to re-initialize the device. Drivers
328  * can override this method to implement more optimized support.
329  */
330 static int
331 default_reset(struct ieee80211vap *vap, u_long cmd)
332 {
333         return ENETRESET;
334 }
335
336 /*
337  * Prepare a vap for use.  Drivers use this call to
338  * setup net80211 state in new vap's prior attaching
339  * them with ieee80211_vap_attach (below).
340  */
341 int
342 ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
343         const char name[IFNAMSIZ], int unit, int opmode, int flags,
344         const uint8_t bssid[IEEE80211_ADDR_LEN],
345         const uint8_t macaddr[IEEE80211_ADDR_LEN])
346 {
347         struct ifnet *ifp;
348
349         ifp = if_alloc(IFT_ETHER);
350         if (ifp == NULL) {
351                 if_printf(ic->ic_ifp, "%s: unable to allocate ifnet\n",
352                     __func__);
353                 return ENOMEM;
354         }
355         if_initname(ifp, name, unit);
356         ifp->if_softc = vap;                    /* back pointer */
357         ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
358         ifp->if_start = ieee80211_start;
359         ifp->if_ioctl = ieee80211_ioctl;
360         ifp->if_watchdog = NULL;                /* NB: no watchdog routine */
361         ifp->if_init = ieee80211_init;
362         /* NB: input+output filled in by ether_ifattach */
363         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
364         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
365         IFQ_SET_READY(&ifp->if_snd);
366
367         vap->iv_ifp = ifp;
368         vap->iv_ic = ic;
369         vap->iv_flags = ic->ic_flags;           /* propagate common flags */
370         vap->iv_flags_ext = ic->ic_flags_ext;
371         vap->iv_flags_ven = ic->ic_flags_ven;
372         vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
373         vap->iv_htcaps = ic->ic_htcaps;
374         vap->iv_opmode = opmode;
375         vap->iv_caps |= ieee80211_opcap[opmode];
376         switch (opmode) {
377         case IEEE80211_M_WDS:
378                 /*
379                  * WDS links must specify the bssid of the far end.
380                  * For legacy operation this is a static relationship.
381                  * For non-legacy operation the station must associate
382                  * and be authorized to pass traffic.  Plumbing the
383                  * vap to the proper node happens when the vap
384                  * transitions to RUN state.
385                  */
386                 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
387                 vap->iv_flags |= IEEE80211_F_DESBSSID;
388                 if (flags & IEEE80211_CLONE_WDSLEGACY)
389                         vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
390                 break;
391 #ifdef IEEE80211_SUPPORT_TDMA
392         case IEEE80211_M_AHDEMO:
393                 if (flags & IEEE80211_CLONE_TDMA) {
394                         /* NB: checked before clone operation allowed */
395                         KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
396                             ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
397                         /*
398                          * Propagate TDMA capability to mark vap; this
399                          * cannot be removed and is used to distinguish
400                          * regular ahdemo operation from ahdemo+tdma.
401                          */
402                         vap->iv_caps |= IEEE80211_C_TDMA;
403                 }
404                 break;
405 #endif
406         }
407         /* auto-enable s/w beacon miss support */
408         if (flags & IEEE80211_CLONE_NOBEACONS)
409                 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
410         /*
411          * Enable various functionality by default if we're
412          * capable; the driver can override us if it knows better.
413          */
414         if (vap->iv_caps & IEEE80211_C_WME)
415                 vap->iv_flags |= IEEE80211_F_WME;
416         if (vap->iv_caps & IEEE80211_C_BURST)
417                 vap->iv_flags |= IEEE80211_F_BURST;
418         if (vap->iv_caps & IEEE80211_C_FF)
419                 vap->iv_flags |= IEEE80211_F_FF;
420         if (vap->iv_caps & IEEE80211_C_TURBOP)
421                 vap->iv_flags |= IEEE80211_F_TURBOP;
422         /* NB: bg scanning only makes sense for station mode right now */
423         if (vap->iv_opmode == IEEE80211_M_STA &&
424             (vap->iv_caps & IEEE80211_C_BGSCAN))
425                 vap->iv_flags |= IEEE80211_F_BGSCAN;
426         vap->iv_flags |= IEEE80211_F_DOTH;      /* XXX no cap, just ena */
427         /* NB: DFS support only makes sense for ap mode right now */
428         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
429             (vap->iv_caps & IEEE80211_C_DFS))
430                 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
431
432         vap->iv_des_chan = IEEE80211_CHAN_ANYC;         /* any channel is ok */
433         vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
434         vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
435         /*
436          * Install a default reset method for the ioctl support;
437          * the driver can override this.
438          */
439         vap->iv_reset = default_reset;
440
441         IEEE80211_ADDR_COPY(vap->iv_myaddr, macaddr);
442
443         ieee80211_sysctl_vattach(vap);
444         ieee80211_crypto_vattach(vap);
445         ieee80211_node_vattach(vap);
446         ieee80211_power_vattach(vap);
447         ieee80211_proto_vattach(vap);
448         ieee80211_ht_vattach(vap);
449         ieee80211_scan_vattach(vap);
450         ieee80211_regdomain_vattach(vap);
451
452         return 0;
453 }
454
455 /*
456  * Activate a vap.  State should have been prepared with a
457  * call to ieee80211_vap_setup and by the driver.  On return
458  * from this call the vap is ready for use.
459  */
460 int
461 ieee80211_vap_attach(struct ieee80211vap *vap,
462         ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
463 {
464         struct ifnet *ifp = vap->iv_ifp;
465         struct ieee80211com *ic = vap->iv_ic;
466         struct ifmediareq imr;
467         int maxrate;
468
469         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
470             "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
471             __func__, ieee80211_opmode_name[vap->iv_opmode],
472             ic->ic_ifp->if_xname, vap->iv_flags, vap->iv_flags_ext);
473
474         /*
475          * Do late attach work that cannot happen until after
476          * the driver has had a chance to override defaults.
477          */
478         ieee80211_node_latevattach(vap);
479         ieee80211_power_latevattach(vap);
480
481         maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
482             vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
483         ieee80211_media_status(ifp, &imr);
484         /* NB: strip explicit mode; we're actually in autoselect */
485         ifmedia_set(&vap->iv_media,
486             imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
487         if (maxrate)
488                 ifp->if_baudrate = IF_Mbps(maxrate);
489
490         ether_ifattach(ifp, vap->iv_myaddr);
491         /* hook output method setup by ether_ifattach */
492         vap->iv_output = ifp->if_output;
493         ifp->if_output = ieee80211_output;
494         /* NB: if_mtu set by ether_ifattach to ETHERMTU */
495         bpfattach2(ifp, DLT_IEEE802_11, ifp->if_hdrlen, &vap->iv_rawbpf);
496
497         IEEE80211_LOCK(ic);
498         TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
499         ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
500         ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
501         ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
502         ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
503         ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
504         ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
505         ieee80211_syncifflag_locked(ic, IFF_PROMISC);
506         ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
507         IEEE80211_UNLOCK(ic);
508
509         return 1;
510 }
511
512 /* 
513  * Tear down vap state and reclaim the ifnet.
514  * The driver is assumed to have prepared for
515  * this; e.g. by turning off interrupts for the
516  * underlying device.
517  */
518 void
519 ieee80211_vap_detach(struct ieee80211vap *vap)
520 {
521         struct ieee80211com *ic = vap->iv_ic;
522         struct ifnet *ifp = vap->iv_ifp;
523
524         IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
525             __func__, ieee80211_opmode_name[vap->iv_opmode],
526             ic->ic_ifp->if_xname);
527
528         IEEE80211_LOCK(ic);
529         /* block traffic from above */
530         ifp->if_drv_flags |= IFF_DRV_OACTIVE;
531         /*
532          * Evil hack.  Clear the backpointer from the ifnet to the
533          * vap so any requests from above will return an error or
534          * be ignored.  In particular this short-circuits requests
535          * by the bridge to turn off promiscuous mode as a result
536          * of calling ether_ifdetach.
537          */
538         ifp->if_softc = NULL;
539         /*
540          * Stop the vap before detaching the ifnet.  Ideally we'd
541          * do this in the other order so the ifnet is inaccessible
542          * while we cleanup internal state but that is hard.
543          */
544         ieee80211_stop_locked(vap);
545
546         TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
547         ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
548         ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
549         ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
550         ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
551         ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_HT);
552         ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_USEHT40);
553         ieee80211_syncifflag_locked(ic, IFF_PROMISC);
554         ieee80211_syncifflag_locked(ic, IFF_ALLMULTI);
555         IEEE80211_UNLOCK(ic);
556
557         /* XXX can't hold com lock */
558         /* NB: bpfattach is called by ether_ifdetach and claims all taps */
559         ether_ifdetach(ifp);
560
561         ifmedia_removeall(&vap->iv_media);
562
563         ieee80211_regdomain_vdetach(vap);
564         ieee80211_scan_vdetach(vap);
565         ieee80211_ht_vdetach(vap);
566         /* NB: must be before ieee80211_node_vdetach */
567         ieee80211_proto_vdetach(vap);
568         ieee80211_crypto_vdetach(vap);
569         ieee80211_power_vdetach(vap);
570         ieee80211_node_vdetach(vap);
571         ieee80211_sysctl_vdetach(vap);
572
573         if_free(ifp);
574 }
575
576 /*
577  * Synchronize flag bit state in the parent ifnet structure
578  * according to the state of all vap ifnet's.  This is used,
579  * for example, to handle IFF_PROMISC and IFF_ALLMULTI.
580  */
581 void
582 ieee80211_syncifflag_locked(struct ieee80211com *ic, int flag)
583 {
584         struct ifnet *ifp = ic->ic_ifp;
585         struct ieee80211vap *vap;
586         int bit, oflags;
587
588         IEEE80211_LOCK_ASSERT(ic);
589
590         bit = 0;
591         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
592                 if (vap->iv_ifp->if_flags & flag) {
593                         /*
594                          * XXX the bridge sets PROMISC but we don't want to
595                          * enable it on the device, discard here so all the
596                          * drivers don't need to special-case it
597                          */
598                         if (flag == IFF_PROMISC &&
599                             vap->iv_opmode == IEEE80211_M_HOSTAP)
600                                 continue;
601                         bit = 1;
602                         break;
603                 }
604         oflags = ifp->if_flags;
605         if (bit)
606                 ifp->if_flags |= flag;
607         else
608                 ifp->if_flags &= ~flag;
609         if ((ifp->if_flags ^ oflags) & flag) {
610                 /* XXX should we return 1/0 and let caller do this? */
611                 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
612                         if (flag == IFF_PROMISC)
613                                 ic->ic_update_promisc(ifp);
614                         else if (flag == IFF_ALLMULTI)
615                                 ic->ic_update_mcast(ifp);
616                 }
617         }
618 }
619
620 /*
621  * Synchronize flag bit state in the com structure
622  * according to the state of all vap's.  This is used,
623  * for example, to handle state changes via ioctls.
624  */
625 static void
626 ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
627 {
628         struct ieee80211vap *vap;
629         int bit;
630
631         IEEE80211_LOCK_ASSERT(ic);
632
633         bit = 0;
634         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
635                 if (vap->iv_flags & flag) {
636                         bit = 1;
637                         break;
638                 }
639         if (bit)
640                 ic->ic_flags |= flag;
641         else
642                 ic->ic_flags &= ~flag;
643 }
644
645 void
646 ieee80211_syncflag(struct ieee80211vap *vap, int flag)
647 {
648         struct ieee80211com *ic = vap->iv_ic;
649
650         IEEE80211_LOCK(ic);
651         if (flag < 0) {
652                 flag = -flag;
653                 vap->iv_flags &= ~flag;
654         } else
655                 vap->iv_flags |= flag;
656         ieee80211_syncflag_locked(ic, flag);
657         IEEE80211_UNLOCK(ic);
658 }
659
660 /*
661  * Synchronize flag bit state in the com structure
662  * according to the state of all vap's.  This is used,
663  * for example, to handle state changes via ioctls.
664  */
665 static void
666 ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
667 {
668         struct ieee80211vap *vap;
669         int bit;
670
671         IEEE80211_LOCK_ASSERT(ic);
672
673         bit = 0;
674         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
675                 if (vap->iv_flags_ext & flag) {
676                         bit = 1;
677                         break;
678                 }
679         if (bit)
680                 ic->ic_flags_ext |= flag;
681         else
682                 ic->ic_flags_ext &= ~flag;
683 }
684
685 void
686 ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
687 {
688         struct ieee80211com *ic = vap->iv_ic;
689
690         IEEE80211_LOCK(ic);
691         if (flag < 0) {
692                 flag = -flag;
693                 vap->iv_flags_ext &= ~flag;
694         } else
695                 vap->iv_flags_ext |= flag;
696         ieee80211_syncflag_ext_locked(ic, flag);
697         IEEE80211_UNLOCK(ic);
698 }
699
700 static __inline int
701 mapgsm(u_int freq, u_int flags)
702 {
703         freq *= 10;
704         if (flags & IEEE80211_CHAN_QUARTER)
705                 freq += 5;
706         else if (flags & IEEE80211_CHAN_HALF)
707                 freq += 10;
708         else
709                 freq += 20;
710         /* NB: there is no 907/20 wide but leave room */
711         return (freq - 906*10) / 5;
712 }
713
714 static __inline int
715 mappsb(u_int freq, u_int flags)
716 {
717         return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
718 }
719
720 /*
721  * Convert MHz frequency to IEEE channel number.
722  */
723 int
724 ieee80211_mhz2ieee(u_int freq, u_int flags)
725 {
726 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
727         if (flags & IEEE80211_CHAN_GSM)
728                 return mapgsm(freq, flags);
729         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
730                 if (freq == 2484)
731                         return 14;
732                 if (freq < 2484)
733                         return ((int) freq - 2407) / 5;
734                 else
735                         return 15 + ((freq - 2512) / 20);
736         } else if (flags & IEEE80211_CHAN_5GHZ) {       /* 5Ghz band */
737                 if (freq <= 5000) {
738                         /* XXX check regdomain? */
739                         if (IS_FREQ_IN_PSB(freq))
740                                 return mappsb(freq, flags);
741                         return (freq - 4000) / 5;
742                 } else
743                         return (freq - 5000) / 5;
744         } else {                                /* either, guess */
745                 if (freq == 2484)
746                         return 14;
747                 if (freq < 2484) {
748                         if (907 <= freq && freq <= 922)
749                                 return mapgsm(freq, flags);
750                         return ((int) freq - 2407) / 5;
751                 }
752                 if (freq < 5000) {
753                         if (IS_FREQ_IN_PSB(freq))
754                                 return mappsb(freq, flags);
755                         else if (freq > 4900)
756                                 return (freq - 4000) / 5;
757                         else
758                                 return 15 + ((freq - 2512) / 20);
759                 }
760                 return (freq - 5000) / 5;
761         }
762 #undef IS_FREQ_IN_PSB
763 }
764
765 /*
766  * Convert channel to IEEE channel number.
767  */
768 int
769 ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
770 {
771         if (c == NULL) {
772                 if_printf(ic->ic_ifp, "invalid channel (NULL)\n");
773                 return 0;               /* XXX */
774         }
775         return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
776 }
777
778 /*
779  * Convert IEEE channel number to MHz frequency.
780  */
781 u_int
782 ieee80211_ieee2mhz(u_int chan, u_int flags)
783 {
784         if (flags & IEEE80211_CHAN_GSM)
785                 return 907 + 5 * (chan / 10);
786         if (flags & IEEE80211_CHAN_2GHZ) {      /* 2GHz band */
787                 if (chan == 14)
788                         return 2484;
789                 if (chan < 14)
790                         return 2407 + chan*5;
791                 else
792                         return 2512 + ((chan-15)*20);
793         } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
794                 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
795                         chan -= 37;
796                         return 4940 + chan*5 + (chan % 5 ? 2 : 0);
797                 }
798                 return 5000 + (chan*5);
799         } else {                                /* either, guess */
800                 /* XXX can't distinguish PSB+GSM channels */
801                 if (chan == 14)
802                         return 2484;
803                 if (chan < 14)                  /* 0-13 */
804                         return 2407 + chan*5;
805                 if (chan < 27)                  /* 15-26 */
806                         return 2512 + ((chan-15)*20);
807                 return 5000 + (chan*5);
808         }
809 }
810
811 /*
812  * Locate a channel given a frequency+flags.  We cache
813  * the previous lookup to optimize switching between two
814  * channels--as happens with dynamic turbo.
815  */
816 struct ieee80211_channel *
817 ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
818 {
819         struct ieee80211_channel *c;
820         int i;
821
822         flags &= IEEE80211_CHAN_ALLTURBO;
823         c = ic->ic_prevchan;
824         if (c != NULL && c->ic_freq == freq &&
825             (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
826                 return c;
827         /* brute force search */
828         for (i = 0; i < ic->ic_nchans; i++) {
829                 c = &ic->ic_channels[i];
830                 if (c->ic_freq == freq &&
831                     (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
832                         return c;
833         }
834         return NULL;
835 }
836
837 /*
838  * Locate a channel given a channel number+flags.  We cache
839  * the previous lookup to optimize switching between two
840  * channels--as happens with dynamic turbo.
841  */
842 struct ieee80211_channel *
843 ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
844 {
845         struct ieee80211_channel *c;
846         int i;
847
848         flags &= IEEE80211_CHAN_ALLTURBO;
849         c = ic->ic_prevchan;
850         if (c != NULL && c->ic_ieee == ieee &&
851             (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
852                 return c;
853         /* brute force search */
854         for (i = 0; i < ic->ic_nchans; i++) {
855                 c = &ic->ic_channels[i];
856                 if (c->ic_ieee == ieee &&
857                     (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
858                         return c;
859         }
860         return NULL;
861 }
862
863 static void
864 addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
865 {
866 #define ADD(_ic, _s, _o) \
867         ifmedia_add(media, \
868                 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
869         static const u_int mopts[IEEE80211_MODE_MAX] = { 
870             [IEEE80211_MODE_AUTO]       = IFM_AUTO,
871             [IEEE80211_MODE_11A]        = IFM_IEEE80211_11A,
872             [IEEE80211_MODE_11B]        = IFM_IEEE80211_11B,
873             [IEEE80211_MODE_11G]        = IFM_IEEE80211_11G,
874             [IEEE80211_MODE_FH]         = IFM_IEEE80211_FH,
875             [IEEE80211_MODE_TURBO_A]    = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
876             [IEEE80211_MODE_TURBO_G]    = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
877             [IEEE80211_MODE_STURBO_A]   = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
878             [IEEE80211_MODE_HALF]       = IFM_IEEE80211_11A,    /* XXX */
879             [IEEE80211_MODE_QUARTER]    = IFM_IEEE80211_11A,    /* XXX */
880             [IEEE80211_MODE_11NA]       = IFM_IEEE80211_11NA,
881             [IEEE80211_MODE_11NG]       = IFM_IEEE80211_11NG,
882         };
883         u_int mopt;
884
885         mopt = mopts[mode];
886         if (addsta)
887                 ADD(ic, mword, mopt);   /* STA mode has no cap */
888         if (caps & IEEE80211_C_IBSS)
889                 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
890         if (caps & IEEE80211_C_HOSTAP)
891                 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
892         if (caps & IEEE80211_C_AHDEMO)
893                 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
894         if (caps & IEEE80211_C_MONITOR)
895                 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
896         if (caps & IEEE80211_C_WDS)
897                 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
898 #undef ADD
899 }
900
901 /*
902  * Setup the media data structures according to the channel and
903  * rate tables.
904  */
905 static int
906 ieee80211_media_setup(struct ieee80211com *ic,
907         struct ifmedia *media, int caps, int addsta,
908         ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
909 {
910         int i, j, mode, rate, maxrate, mword, r;
911         const struct ieee80211_rateset *rs;
912         struct ieee80211_rateset allrates;
913
914         /*
915          * Fill in media characteristics.
916          */
917         ifmedia_init(media, 0, media_change, media_stat);
918         maxrate = 0;
919         /*
920          * Add media for legacy operating modes.
921          */
922         memset(&allrates, 0, sizeof(allrates));
923         for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
924                 if (isclr(ic->ic_modecaps, mode))
925                         continue;
926                 addmedia(media, caps, addsta, mode, IFM_AUTO);
927                 if (mode == IEEE80211_MODE_AUTO)
928                         continue;
929                 rs = &ic->ic_sup_rates[mode];
930                 for (i = 0; i < rs->rs_nrates; i++) {
931                         rate = rs->rs_rates[i];
932                         mword = ieee80211_rate2media(ic, rate, mode);
933                         if (mword == 0)
934                                 continue;
935                         addmedia(media, caps, addsta, mode, mword);
936                         /*
937                          * Add legacy rate to the collection of all rates.
938                          */
939                         r = rate & IEEE80211_RATE_VAL;
940                         for (j = 0; j < allrates.rs_nrates; j++)
941                                 if (allrates.rs_rates[j] == r)
942                                         break;
943                         if (j == allrates.rs_nrates) {
944                                 /* unique, add to the set */
945                                 allrates.rs_rates[j] = r;
946                                 allrates.rs_nrates++;
947                         }
948                         rate = (rate & IEEE80211_RATE_VAL) / 2;
949                         if (rate > maxrate)
950                                 maxrate = rate;
951                 }
952         }
953         for (i = 0; i < allrates.rs_nrates; i++) {
954                 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
955                                 IEEE80211_MODE_AUTO);
956                 if (mword == 0)
957                         continue;
958                 /* NB: remove media options from mword */
959                 addmedia(media, caps, addsta,
960                     IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
961         }
962         /*
963          * Add HT/11n media.  Note that we do not have enough
964          * bits in the media subtype to express the MCS so we
965          * use a "placeholder" media subtype and any fixed MCS
966          * must be specified with a different mechanism.
967          */
968         for (; mode <= IEEE80211_MODE_11NG; mode++) {
969                 if (isclr(ic->ic_modecaps, mode))
970                         continue;
971                 addmedia(media, caps, addsta, mode, IFM_AUTO);
972                 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
973         }
974         if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
975             isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
976                 addmedia(media, caps, addsta,
977                     IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
978                 /* XXX could walk htrates */
979                 /* XXX known array size */
980                 if (ieee80211_htrates[15].ht40_rate_400ns > maxrate)
981                         maxrate = ieee80211_htrates[15].ht40_rate_400ns;
982         }
983         return maxrate;
984 }
985
986 void
987 ieee80211_media_init(struct ieee80211com *ic)
988 {
989         struct ifnet *ifp = ic->ic_ifp;
990         int maxrate;
991
992         /* NB: this works because the structure is initialized to zero */
993         if (!LIST_EMPTY(&ic->ic_media.ifm_list)) {
994                 /*
995                  * We are re-initializing the channel list; clear
996                  * the existing media state as the media routines
997                  * don't suppress duplicates.
998                  */
999                 ifmedia_removeall(&ic->ic_media);
1000         }
1001         ieee80211_chan_init(ic);
1002
1003         /*
1004          * Recalculate media settings in case new channel list changes
1005          * the set of available modes.
1006          */
1007         maxrate = ieee80211_media_setup(ic, &ic->ic_media, ic->ic_caps, 1,
1008                 ieee80211com_media_change, ieee80211com_media_status);
1009         /* NB: strip explicit mode; we're actually in autoselect */
1010         ifmedia_set(&ic->ic_media,
1011             media_status(ic->ic_opmode, ic->ic_curchan) &~
1012                 (IFM_MMASK | IFM_IEEE80211_TURBO));
1013         if (maxrate)
1014                 ifp->if_baudrate = IF_Mbps(maxrate);
1015
1016         /* XXX need to propagate new media settings to vap's */
1017 }
1018
1019 /* XXX inline or eliminate? */
1020 const struct ieee80211_rateset *
1021 ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1022 {
1023         /* XXX does this work for 11ng basic rates? */
1024         return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1025 }
1026
1027 void
1028 ieee80211_announce(struct ieee80211com *ic)
1029 {
1030         struct ifnet *ifp = ic->ic_ifp;
1031         int i, mode, rate, mword;
1032         const struct ieee80211_rateset *rs;
1033
1034         /* NB: skip AUTO since it has no rates */
1035         for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1036                 if (isclr(ic->ic_modecaps, mode))
1037                         continue;
1038                 if_printf(ifp, "%s rates: ", ieee80211_phymode_name[mode]);
1039                 rs = &ic->ic_sup_rates[mode];
1040                 for (i = 0; i < rs->rs_nrates; i++) {
1041                         mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1042                         if (mword == 0)
1043                                 continue;
1044                         rate = ieee80211_media2rate(mword);
1045                         printf("%s%d%sMbps", (i != 0 ? " " : ""),
1046                             rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1047                 }
1048                 printf("\n");
1049         }
1050         ieee80211_ht_announce(ic);
1051 }
1052
1053 void
1054 ieee80211_announce_channels(struct ieee80211com *ic)
1055 {
1056         const struct ieee80211_channel *c;
1057         char type;
1058         int i, cw;
1059
1060         printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
1061         for (i = 0; i < ic->ic_nchans; i++) {
1062                 c = &ic->ic_channels[i];
1063                 if (IEEE80211_IS_CHAN_ST(c))
1064                         type = 'S';
1065                 else if (IEEE80211_IS_CHAN_108A(c))
1066                         type = 'T';
1067                 else if (IEEE80211_IS_CHAN_108G(c))
1068                         type = 'G';
1069                 else if (IEEE80211_IS_CHAN_HT(c))
1070                         type = 'n';
1071                 else if (IEEE80211_IS_CHAN_A(c))
1072                         type = 'a';
1073                 else if (IEEE80211_IS_CHAN_ANYG(c))
1074                         type = 'g';
1075                 else if (IEEE80211_IS_CHAN_B(c))
1076                         type = 'b';
1077                 else
1078                         type = 'f';
1079                 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1080                         cw = 40;
1081                 else if (IEEE80211_IS_CHAN_HALF(c))
1082                         cw = 10;
1083                 else if (IEEE80211_IS_CHAN_QUARTER(c))
1084                         cw = 5;
1085                 else
1086                         cw = 20;
1087                 printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
1088                         , c->ic_ieee, c->ic_freq, type
1089                         , cw
1090                         , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1091                           IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1092                         , c->ic_maxregpower
1093                         , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1094                         , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1095                 );
1096         }
1097 }
1098
1099 static int
1100 media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1101 {
1102         switch (IFM_MODE(ime->ifm_media)) {
1103         case IFM_IEEE80211_11A:
1104                 *mode = IEEE80211_MODE_11A;
1105                 break;
1106         case IFM_IEEE80211_11B:
1107                 *mode = IEEE80211_MODE_11B;
1108                 break;
1109         case IFM_IEEE80211_11G:
1110                 *mode = IEEE80211_MODE_11G;
1111                 break;
1112         case IFM_IEEE80211_FH:
1113                 *mode = IEEE80211_MODE_FH;
1114                 break;
1115         case IFM_IEEE80211_11NA:
1116                 *mode = IEEE80211_MODE_11NA;
1117                 break;
1118         case IFM_IEEE80211_11NG:
1119                 *mode = IEEE80211_MODE_11NG;
1120                 break;
1121         case IFM_AUTO:
1122                 *mode = IEEE80211_MODE_AUTO;
1123                 break;
1124         default:
1125                 return 0;
1126         }
1127         /*
1128          * Turbo mode is an ``option''.
1129          * XXX does not apply to AUTO
1130          */
1131         if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1132                 if (*mode == IEEE80211_MODE_11A) {
1133                         if (flags & IEEE80211_F_TURBOP)
1134                                 *mode = IEEE80211_MODE_TURBO_A;
1135                         else
1136                                 *mode = IEEE80211_MODE_STURBO_A;
1137                 } else if (*mode == IEEE80211_MODE_11G)
1138                         *mode = IEEE80211_MODE_TURBO_G;
1139                 else
1140                         return 0;
1141         }
1142         /* XXX HT40 +/- */
1143         return 1;
1144 }
1145
1146 /*
1147  * Handle a media change request on the underlying interface.
1148  */
1149 int
1150 ieee80211com_media_change(struct ifnet *ifp)
1151 {
1152         return EINVAL;
1153 }
1154
1155 /*
1156  * Handle a media change request on the vap interface.
1157  */
1158 int
1159 ieee80211_media_change(struct ifnet *ifp)
1160 {
1161         struct ieee80211vap *vap = ifp->if_softc;
1162         struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1163         uint16_t newmode;
1164
1165         if (!media2mode(ime, vap->iv_flags, &newmode))
1166                 return EINVAL;
1167         if (vap->iv_des_mode != newmode) {
1168                 vap->iv_des_mode = newmode;
1169                 return ENETRESET;
1170         }
1171         return 0;
1172 }
1173
1174 /*
1175  * Common code to calculate the media status word
1176  * from the operating mode and channel state.
1177  */
1178 static int
1179 media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1180 {
1181         int status;
1182
1183         status = IFM_IEEE80211;
1184         switch (opmode) {
1185         case IEEE80211_M_STA:
1186                 break;
1187         case IEEE80211_M_IBSS:
1188                 status |= IFM_IEEE80211_ADHOC;
1189                 break;
1190         case IEEE80211_M_HOSTAP:
1191                 status |= IFM_IEEE80211_HOSTAP;
1192                 break;
1193         case IEEE80211_M_MONITOR:
1194                 status |= IFM_IEEE80211_MONITOR;
1195                 break;
1196         case IEEE80211_M_AHDEMO:
1197                 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1198                 break;
1199         case IEEE80211_M_WDS:
1200                 status |= IFM_IEEE80211_WDS;
1201                 break;
1202         }
1203         if (IEEE80211_IS_CHAN_HTA(chan)) {
1204                 status |= IFM_IEEE80211_11NA;
1205         } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1206                 status |= IFM_IEEE80211_11NG;
1207         } else if (IEEE80211_IS_CHAN_A(chan)) {
1208                 status |= IFM_IEEE80211_11A;
1209         } else if (IEEE80211_IS_CHAN_B(chan)) {
1210                 status |= IFM_IEEE80211_11B;
1211         } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1212                 status |= IFM_IEEE80211_11G;
1213         } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1214                 status |= IFM_IEEE80211_FH;
1215         }
1216         /* XXX else complain? */
1217
1218         if (IEEE80211_IS_CHAN_TURBO(chan))
1219                 status |= IFM_IEEE80211_TURBO;
1220 #if 0
1221         if (IEEE80211_IS_CHAN_HT20(chan))
1222                 status |= IFM_IEEE80211_HT20;
1223         if (IEEE80211_IS_CHAN_HT40(chan))
1224                 status |= IFM_IEEE80211_HT40;
1225 #endif
1226         return status;
1227 }
1228
1229 static void
1230 ieee80211com_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1231 {
1232         struct ieee80211com *ic = ifp->if_l2com;
1233         struct ieee80211vap *vap;
1234
1235         imr->ifm_status = IFM_AVALID;
1236         TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
1237                 if (vap->iv_ifp->if_flags & IFF_UP) {
1238                         imr->ifm_status |= IFM_ACTIVE;
1239                         break;
1240                 }
1241         imr->ifm_active = media_status(ic->ic_opmode, ic->ic_curchan);
1242         if (imr->ifm_status & IFM_ACTIVE)
1243                 imr->ifm_current = imr->ifm_active;
1244 }
1245
1246 void
1247 ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1248 {
1249         struct ieee80211vap *vap = ifp->if_softc;
1250         struct ieee80211com *ic = vap->iv_ic;
1251         enum ieee80211_phymode mode;
1252
1253         imr->ifm_status = IFM_AVALID;
1254         /*
1255          * NB: use the current channel's mode to lock down a xmit
1256          * rate only when running; otherwise we may have a mismatch
1257          * in which case the rate will not be convertible.
1258          */
1259         if (vap->iv_state == IEEE80211_S_RUN) {
1260                 imr->ifm_status |= IFM_ACTIVE;
1261                 mode = ieee80211_chan2mode(ic->ic_curchan);
1262         } else
1263                 mode = IEEE80211_MODE_AUTO;
1264         imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1265         /*
1266          * Calculate a current rate if possible.
1267          */
1268         if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1269                 /*
1270                  * A fixed rate is set, report that.
1271                  */
1272                 imr->ifm_active |= ieee80211_rate2media(ic,
1273                         vap->iv_txparms[mode].ucastrate, mode);
1274         } else if (vap->iv_opmode == IEEE80211_M_STA) {
1275                 /*
1276                  * In station mode report the current transmit rate.
1277                  */
1278                 imr->ifm_active |= ieee80211_rate2media(ic,
1279                         vap->iv_bss->ni_txrate, mode);
1280         } else
1281                 imr->ifm_active |= IFM_AUTO;
1282         if (imr->ifm_status & IFM_ACTIVE)
1283                 imr->ifm_current = imr->ifm_active;
1284 }
1285
1286 /*
1287  * Set the current phy mode and recalculate the active channel
1288  * set based on the available channels for this mode.  Also
1289  * select a new default/current channel if the current one is
1290  * inappropriate for this mode.
1291  */
1292 int
1293 ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1294 {
1295         /*
1296          * Adjust basic rates in 11b/11g supported rate set.
1297          * Note that if operating on a hal/quarter rate channel
1298          * this is a noop as those rates sets are different
1299          * and used instead.
1300          */
1301         if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1302                 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1303
1304         ic->ic_curmode = mode;
1305         ieee80211_reset_erp(ic);        /* reset ERP state */
1306
1307         return 0;
1308 }
1309
1310 /*
1311  * Return the phy mode for with the specified channel.
1312  */
1313 enum ieee80211_phymode
1314 ieee80211_chan2mode(const struct ieee80211_channel *chan)
1315 {
1316
1317         if (IEEE80211_IS_CHAN_HTA(chan))
1318                 return IEEE80211_MODE_11NA;
1319         else if (IEEE80211_IS_CHAN_HTG(chan))
1320                 return IEEE80211_MODE_11NG;
1321         else if (IEEE80211_IS_CHAN_108G(chan))
1322                 return IEEE80211_MODE_TURBO_G;
1323         else if (IEEE80211_IS_CHAN_ST(chan))
1324                 return IEEE80211_MODE_STURBO_A;
1325         else if (IEEE80211_IS_CHAN_TURBO(chan))
1326                 return IEEE80211_MODE_TURBO_A;
1327         else if (IEEE80211_IS_CHAN_HALF(chan))
1328                 return IEEE80211_MODE_HALF;
1329         else if (IEEE80211_IS_CHAN_QUARTER(chan))
1330                 return IEEE80211_MODE_QUARTER;
1331         else if (IEEE80211_IS_CHAN_A(chan))
1332                 return IEEE80211_MODE_11A;
1333         else if (IEEE80211_IS_CHAN_ANYG(chan))
1334                 return IEEE80211_MODE_11G;
1335         else if (IEEE80211_IS_CHAN_B(chan))
1336                 return IEEE80211_MODE_11B;
1337         else if (IEEE80211_IS_CHAN_FHSS(chan))
1338                 return IEEE80211_MODE_FH;
1339
1340         /* NB: should not get here */
1341         printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1342                 __func__, chan->ic_freq, chan->ic_flags);
1343         return IEEE80211_MODE_11B;
1344 }
1345
1346 struct ratemedia {
1347         u_int   match;  /* rate + mode */
1348         u_int   media;  /* if_media rate */
1349 };
1350
1351 static int
1352 findmedia(const struct ratemedia rates[], int n, u_int match)
1353 {
1354         int i;
1355
1356         for (i = 0; i < n; i++)
1357                 if (rates[i].match == match)
1358                         return rates[i].media;
1359         return IFM_AUTO;
1360 }
1361
1362 /*
1363  * Convert IEEE80211 rate value to ifmedia subtype.
1364  * Rate is either a legacy rate in units of 0.5Mbps
1365  * or an MCS index.
1366  */
1367 int
1368 ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1369 {
1370 #define N(a)    (sizeof(a) / sizeof(a[0]))
1371         static const struct ratemedia rates[] = {
1372                 {   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1373                 {   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1374                 {   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1375                 {   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1376                 {  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1377                 {  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1378                 {  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1379                 {  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1380                 {  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1381                 {  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1382                 {  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1383                 {  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1384                 {  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1385                 {  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1386                 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1387                 {   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1388                 {   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1389                 {  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1390                 {  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1391                 {  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1392                 {  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1393                 {  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1394                 {  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1395                 {  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1396                 {  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1397                 {  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1398                 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1399                 {   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1400                 {   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1401                 {  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1402                 /* NB: OFDM72 doesn't realy exist so we don't handle it */
1403         };
1404         static const struct ratemedia htrates[] = {
1405                 {   0, IFM_IEEE80211_MCS },
1406                 {   1, IFM_IEEE80211_MCS },
1407                 {   2, IFM_IEEE80211_MCS },
1408                 {   3, IFM_IEEE80211_MCS },
1409                 {   4, IFM_IEEE80211_MCS },
1410                 {   5, IFM_IEEE80211_MCS },
1411                 {   6, IFM_IEEE80211_MCS },
1412                 {   7, IFM_IEEE80211_MCS },
1413                 {   8, IFM_IEEE80211_MCS },
1414                 {   9, IFM_IEEE80211_MCS },
1415                 {  10, IFM_IEEE80211_MCS },
1416                 {  11, IFM_IEEE80211_MCS },
1417                 {  12, IFM_IEEE80211_MCS },
1418                 {  13, IFM_IEEE80211_MCS },
1419                 {  14, IFM_IEEE80211_MCS },
1420                 {  15, IFM_IEEE80211_MCS },
1421         };
1422         int m;
1423
1424         /*
1425          * Check 11n rates first for match as an MCS.
1426          */
1427         if (mode == IEEE80211_MODE_11NA) {
1428                 if (rate & IEEE80211_RATE_MCS) {
1429                         rate &= ~IEEE80211_RATE_MCS;
1430                         m = findmedia(htrates, N(htrates), rate);
1431                         if (m != IFM_AUTO)
1432                                 return m | IFM_IEEE80211_11NA;
1433                 }
1434         } else if (mode == IEEE80211_MODE_11NG) {
1435                 /* NB: 12 is ambiguous, it will be treated as an MCS */
1436                 if (rate & IEEE80211_RATE_MCS) {
1437                         rate &= ~IEEE80211_RATE_MCS;
1438                         m = findmedia(htrates, N(htrates), rate);
1439                         if (m != IFM_AUTO)
1440                                 return m | IFM_IEEE80211_11NG;
1441                 }
1442         }
1443         rate &= IEEE80211_RATE_VAL;
1444         switch (mode) {
1445         case IEEE80211_MODE_11A:
1446         case IEEE80211_MODE_HALF:               /* XXX good 'nuf */
1447         case IEEE80211_MODE_QUARTER:
1448         case IEEE80211_MODE_11NA:
1449         case IEEE80211_MODE_TURBO_A:
1450         case IEEE80211_MODE_STURBO_A:
1451                 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11A);
1452         case IEEE80211_MODE_11B:
1453                 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11B);
1454         case IEEE80211_MODE_FH:
1455                 return findmedia(rates, N(rates), rate | IFM_IEEE80211_FH);
1456         case IEEE80211_MODE_AUTO:
1457                 /* NB: ic may be NULL for some drivers */
1458                 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1459                         return findmedia(rates, N(rates),
1460                             rate | IFM_IEEE80211_FH);
1461                 /* NB: hack, 11g matches both 11b+11a rates */
1462                 /* fall thru... */
1463         case IEEE80211_MODE_11G:
1464         case IEEE80211_MODE_11NG:
1465         case IEEE80211_MODE_TURBO_G:
1466                 return findmedia(rates, N(rates), rate | IFM_IEEE80211_11G);
1467         }
1468         return IFM_AUTO;
1469 #undef N
1470 }
1471
1472 int
1473 ieee80211_media2rate(int mword)
1474 {
1475 #define N(a)    (sizeof(a) / sizeof(a[0]))
1476         static const int ieeerates[] = {
1477                 -1,             /* IFM_AUTO */
1478                 0,              /* IFM_MANUAL */
1479                 0,              /* IFM_NONE */
1480                 2,              /* IFM_IEEE80211_FH1 */
1481                 4,              /* IFM_IEEE80211_FH2 */
1482                 2,              /* IFM_IEEE80211_DS1 */
1483                 4,              /* IFM_IEEE80211_DS2 */
1484                 11,             /* IFM_IEEE80211_DS5 */
1485                 22,             /* IFM_IEEE80211_DS11 */
1486                 44,             /* IFM_IEEE80211_DS22 */
1487                 12,             /* IFM_IEEE80211_OFDM6 */
1488                 18,             /* IFM_IEEE80211_OFDM9 */
1489                 24,             /* IFM_IEEE80211_OFDM12 */
1490                 36,             /* IFM_IEEE80211_OFDM18 */
1491                 48,             /* IFM_IEEE80211_OFDM24 */
1492                 72,             /* IFM_IEEE80211_OFDM36 */
1493                 96,             /* IFM_IEEE80211_OFDM48 */
1494                 108,            /* IFM_IEEE80211_OFDM54 */
1495                 144,            /* IFM_IEEE80211_OFDM72 */
1496                 0,              /* IFM_IEEE80211_DS354k */
1497                 0,              /* IFM_IEEE80211_DS512k */
1498                 6,              /* IFM_IEEE80211_OFDM3 */
1499                 9,              /* IFM_IEEE80211_OFDM4 */
1500                 54,             /* IFM_IEEE80211_OFDM27 */
1501                 -1,             /* IFM_IEEE80211_MCS */
1502         };
1503         return IFM_SUBTYPE(mword) < N(ieeerates) ?
1504                 ieeerates[IFM_SUBTYPE(mword)] : 0;
1505 #undef N
1506 }