Deleted Added
full compact
ieee80211.c (302408) ieee80211.c (337950)
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>
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: stable/11/sys/net80211/ieee80211.c 300232 2016-05-19 21:08:33Z avos $");
28__FBSDID("$FreeBSD: stable/11/sys/net80211/ieee80211.c 337950 2018-08-17 03:03:27Z kevans $");
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#include <sys/malloc.h>
39#include <sys/socket.h>
40#include <sys/sbuf.h>
41
42#include <machine/stdarg.h>
43
44#include <net/if.h>
45#include <net/if_var.h>
46#include <net/if_dl.h>
47#include <net/if_media.h>
48#include <net/if_types.h>
49#include <net/ethernet.h>
50
51#include <net80211/ieee80211_var.h>
52#include <net80211/ieee80211_regdomain.h>
53#ifdef IEEE80211_SUPPORT_SUPERG
54#include <net80211/ieee80211_superg.h>
55#endif
56#include <net80211/ieee80211_ratectl.h>
57
58#include <net/bpf.h>
59
60const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
61 [IEEE80211_MODE_AUTO] = "auto",
62 [IEEE80211_MODE_11A] = "11a",
63 [IEEE80211_MODE_11B] = "11b",
64 [IEEE80211_MODE_11G] = "11g",
65 [IEEE80211_MODE_FH] = "FH",
66 [IEEE80211_MODE_TURBO_A] = "turboA",
67 [IEEE80211_MODE_TURBO_G] = "turboG",
68 [IEEE80211_MODE_STURBO_A] = "sturboA",
69 [IEEE80211_MODE_HALF] = "half",
70 [IEEE80211_MODE_QUARTER] = "quarter",
71 [IEEE80211_MODE_11NA] = "11na",
72 [IEEE80211_MODE_11NG] = "11ng",
73};
74/* map ieee80211_opmode to the corresponding capability bit */
75const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
76 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
77 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
78 [IEEE80211_M_STA] = IEEE80211_C_STA,
79 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
80 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
81 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
82#ifdef IEEE80211_SUPPORT_MESH
83 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
84#endif
85};
86
87const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
88 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
89
90static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
91static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
92static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
93static int ieee80211_media_setup(struct ieee80211com *ic,
94 struct ifmedia *media, int caps, int addsta,
95 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
96static int media_status(enum ieee80211_opmode,
97 const struct ieee80211_channel *);
98static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
99
100MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
101
102/*
103 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
104 */
105#define B(r) ((r) | IEEE80211_RATE_BASIC)
106static const struct ieee80211_rateset ieee80211_rateset_11a =
107 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
108static const struct ieee80211_rateset ieee80211_rateset_half =
109 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
110static const struct ieee80211_rateset ieee80211_rateset_quarter =
111 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112static const struct ieee80211_rateset ieee80211_rateset_11b =
113 { 4, { B(2), B(4), B(11), B(22) } };
114/* NB: OFDM rates are handled specially based on mode */
115static const struct ieee80211_rateset ieee80211_rateset_11g =
116 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
117#undef B
118
119/*
120 * Fill in 802.11 available channel set, mark
121 * all available channels as active, and pick
122 * a default channel if not already specified.
123 */
124void
125ieee80211_chan_init(struct ieee80211com *ic)
126{
127#define DEFAULTRATES(m, def) do { \
128 if (ic->ic_sup_rates[m].rs_nrates == 0) \
129 ic->ic_sup_rates[m] = def; \
130} while (0)
131 struct ieee80211_channel *c;
132 int i;
133
134 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
135 ("invalid number of channels specified: %u", ic->ic_nchans));
136 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
137 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
138 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
139 for (i = 0; i < ic->ic_nchans; i++) {
140 c = &ic->ic_channels[i];
141 KASSERT(c->ic_flags != 0, ("channel with no flags"));
142 /*
143 * Help drivers that work only with frequencies by filling
144 * in IEEE channel #'s if not already calculated. Note this
145 * mimics similar work done in ieee80211_setregdomain when
146 * changing regulatory state.
147 */
148 if (c->ic_ieee == 0)
149 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
150 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
151 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
152 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
153 c->ic_flags);
154 /* default max tx power to max regulatory */
155 if (c->ic_maxpower == 0)
156 c->ic_maxpower = 2*c->ic_maxregpower;
157 setbit(ic->ic_chan_avail, c->ic_ieee);
158 /*
159 * Identify mode capabilities.
160 */
161 if (IEEE80211_IS_CHAN_A(c))
162 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
163 if (IEEE80211_IS_CHAN_B(c))
164 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
165 if (IEEE80211_IS_CHAN_ANYG(c))
166 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
167 if (IEEE80211_IS_CHAN_FHSS(c))
168 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
169 if (IEEE80211_IS_CHAN_108A(c))
170 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
171 if (IEEE80211_IS_CHAN_108G(c))
172 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
173 if (IEEE80211_IS_CHAN_ST(c))
174 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
175 if (IEEE80211_IS_CHAN_HALF(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
177 if (IEEE80211_IS_CHAN_QUARTER(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
179 if (IEEE80211_IS_CHAN_HTA(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
181 if (IEEE80211_IS_CHAN_HTG(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
183 }
184 /* initialize candidate channels to all available */
185 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
186 sizeof(ic->ic_chan_avail));
187
188 /* sort channel table to allow lookup optimizations */
189 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
190
191 /* invalidate any previous state */
192 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
193 ic->ic_prevchan = NULL;
194 ic->ic_csa_newchan = NULL;
195 /* arbitrarily pick the first channel */
196 ic->ic_curchan = &ic->ic_channels[0];
197 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
198
199 /* fillin well-known rate sets if driver has not specified */
200 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
201 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
202 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
203 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
204 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
205 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
206 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
207 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
208 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
209 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
210
211 /*
212 * Setup required information to fill the mcsset field, if driver did
213 * not. Assume a 2T2R setup for historic reasons.
214 */
215 if (ic->ic_rxstream == 0)
216 ic->ic_rxstream = 2;
217 if (ic->ic_txstream == 0)
218 ic->ic_txstream = 2;
219
220 /*
221 * Set auto mode to reset active channel state and any desired channel.
222 */
223 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
224#undef DEFAULTRATES
225}
226
227static void
228null_update_mcast(struct ieee80211com *ic)
229{
230
231 ic_printf(ic, "need multicast update callback\n");
232}
233
234static void
235null_update_promisc(struct ieee80211com *ic)
236{
237
238 ic_printf(ic, "need promiscuous mode update callback\n");
239}
240
241static void
242null_update_chw(struct ieee80211com *ic)
243{
244
245 ic_printf(ic, "%s: need callback\n", __func__);
246}
247
248int
249ic_printf(struct ieee80211com *ic, const char * fmt, ...)
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#include <sys/malloc.h>
39#include <sys/socket.h>
40#include <sys/sbuf.h>
41
42#include <machine/stdarg.h>
43
44#include <net/if.h>
45#include <net/if_var.h>
46#include <net/if_dl.h>
47#include <net/if_media.h>
48#include <net/if_types.h>
49#include <net/ethernet.h>
50
51#include <net80211/ieee80211_var.h>
52#include <net80211/ieee80211_regdomain.h>
53#ifdef IEEE80211_SUPPORT_SUPERG
54#include <net80211/ieee80211_superg.h>
55#endif
56#include <net80211/ieee80211_ratectl.h>
57
58#include <net/bpf.h>
59
60const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
61 [IEEE80211_MODE_AUTO] = "auto",
62 [IEEE80211_MODE_11A] = "11a",
63 [IEEE80211_MODE_11B] = "11b",
64 [IEEE80211_MODE_11G] = "11g",
65 [IEEE80211_MODE_FH] = "FH",
66 [IEEE80211_MODE_TURBO_A] = "turboA",
67 [IEEE80211_MODE_TURBO_G] = "turboG",
68 [IEEE80211_MODE_STURBO_A] = "sturboA",
69 [IEEE80211_MODE_HALF] = "half",
70 [IEEE80211_MODE_QUARTER] = "quarter",
71 [IEEE80211_MODE_11NA] = "11na",
72 [IEEE80211_MODE_11NG] = "11ng",
73};
74/* map ieee80211_opmode to the corresponding capability bit */
75const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
76 [IEEE80211_M_IBSS] = IEEE80211_C_IBSS,
77 [IEEE80211_M_WDS] = IEEE80211_C_WDS,
78 [IEEE80211_M_STA] = IEEE80211_C_STA,
79 [IEEE80211_M_AHDEMO] = IEEE80211_C_AHDEMO,
80 [IEEE80211_M_HOSTAP] = IEEE80211_C_HOSTAP,
81 [IEEE80211_M_MONITOR] = IEEE80211_C_MONITOR,
82#ifdef IEEE80211_SUPPORT_MESH
83 [IEEE80211_M_MBSS] = IEEE80211_C_MBSS,
84#endif
85};
86
87const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
88 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
89
90static void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
91static void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
92static void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
93static int ieee80211_media_setup(struct ieee80211com *ic,
94 struct ifmedia *media, int caps, int addsta,
95 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
96static int media_status(enum ieee80211_opmode,
97 const struct ieee80211_channel *);
98static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
99
100MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
101
102/*
103 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
104 */
105#define B(r) ((r) | IEEE80211_RATE_BASIC)
106static const struct ieee80211_rateset ieee80211_rateset_11a =
107 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
108static const struct ieee80211_rateset ieee80211_rateset_half =
109 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
110static const struct ieee80211_rateset ieee80211_rateset_quarter =
111 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
112static const struct ieee80211_rateset ieee80211_rateset_11b =
113 { 4, { B(2), B(4), B(11), B(22) } };
114/* NB: OFDM rates are handled specially based on mode */
115static const struct ieee80211_rateset ieee80211_rateset_11g =
116 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
117#undef B
118
119/*
120 * Fill in 802.11 available channel set, mark
121 * all available channels as active, and pick
122 * a default channel if not already specified.
123 */
124void
125ieee80211_chan_init(struct ieee80211com *ic)
126{
127#define DEFAULTRATES(m, def) do { \
128 if (ic->ic_sup_rates[m].rs_nrates == 0) \
129 ic->ic_sup_rates[m] = def; \
130} while (0)
131 struct ieee80211_channel *c;
132 int i;
133
134 KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
135 ("invalid number of channels specified: %u", ic->ic_nchans));
136 memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
137 memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
138 setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
139 for (i = 0; i < ic->ic_nchans; i++) {
140 c = &ic->ic_channels[i];
141 KASSERT(c->ic_flags != 0, ("channel with no flags"));
142 /*
143 * Help drivers that work only with frequencies by filling
144 * in IEEE channel #'s if not already calculated. Note this
145 * mimics similar work done in ieee80211_setregdomain when
146 * changing regulatory state.
147 */
148 if (c->ic_ieee == 0)
149 c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
150 if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
151 c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
152 (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
153 c->ic_flags);
154 /* default max tx power to max regulatory */
155 if (c->ic_maxpower == 0)
156 c->ic_maxpower = 2*c->ic_maxregpower;
157 setbit(ic->ic_chan_avail, c->ic_ieee);
158 /*
159 * Identify mode capabilities.
160 */
161 if (IEEE80211_IS_CHAN_A(c))
162 setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
163 if (IEEE80211_IS_CHAN_B(c))
164 setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
165 if (IEEE80211_IS_CHAN_ANYG(c))
166 setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
167 if (IEEE80211_IS_CHAN_FHSS(c))
168 setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
169 if (IEEE80211_IS_CHAN_108A(c))
170 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
171 if (IEEE80211_IS_CHAN_108G(c))
172 setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
173 if (IEEE80211_IS_CHAN_ST(c))
174 setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
175 if (IEEE80211_IS_CHAN_HALF(c))
176 setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
177 if (IEEE80211_IS_CHAN_QUARTER(c))
178 setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
179 if (IEEE80211_IS_CHAN_HTA(c))
180 setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
181 if (IEEE80211_IS_CHAN_HTG(c))
182 setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
183 }
184 /* initialize candidate channels to all available */
185 memcpy(ic->ic_chan_active, ic->ic_chan_avail,
186 sizeof(ic->ic_chan_avail));
187
188 /* sort channel table to allow lookup optimizations */
189 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
190
191 /* invalidate any previous state */
192 ic->ic_bsschan = IEEE80211_CHAN_ANYC;
193 ic->ic_prevchan = NULL;
194 ic->ic_csa_newchan = NULL;
195 /* arbitrarily pick the first channel */
196 ic->ic_curchan = &ic->ic_channels[0];
197 ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
198
199 /* fillin well-known rate sets if driver has not specified */
200 DEFAULTRATES(IEEE80211_MODE_11B, ieee80211_rateset_11b);
201 DEFAULTRATES(IEEE80211_MODE_11G, ieee80211_rateset_11g);
202 DEFAULTRATES(IEEE80211_MODE_11A, ieee80211_rateset_11a);
203 DEFAULTRATES(IEEE80211_MODE_TURBO_A, ieee80211_rateset_11a);
204 DEFAULTRATES(IEEE80211_MODE_TURBO_G, ieee80211_rateset_11g);
205 DEFAULTRATES(IEEE80211_MODE_STURBO_A, ieee80211_rateset_11a);
206 DEFAULTRATES(IEEE80211_MODE_HALF, ieee80211_rateset_half);
207 DEFAULTRATES(IEEE80211_MODE_QUARTER, ieee80211_rateset_quarter);
208 DEFAULTRATES(IEEE80211_MODE_11NA, ieee80211_rateset_11a);
209 DEFAULTRATES(IEEE80211_MODE_11NG, ieee80211_rateset_11g);
210
211 /*
212 * Setup required information to fill the mcsset field, if driver did
213 * not. Assume a 2T2R setup for historic reasons.
214 */
215 if (ic->ic_rxstream == 0)
216 ic->ic_rxstream = 2;
217 if (ic->ic_txstream == 0)
218 ic->ic_txstream = 2;
219
220 /*
221 * Set auto mode to reset active channel state and any desired channel.
222 */
223 (void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
224#undef DEFAULTRATES
225}
226
227static void
228null_update_mcast(struct ieee80211com *ic)
229{
230
231 ic_printf(ic, "need multicast update callback\n");
232}
233
234static void
235null_update_promisc(struct ieee80211com *ic)
236{
237
238 ic_printf(ic, "need promiscuous mode update callback\n");
239}
240
241static void
242null_update_chw(struct ieee80211com *ic)
243{
244
245 ic_printf(ic, "%s: need callback\n", __func__);
246}
247
248int
249ic_printf(struct ieee80211com *ic, const char * fmt, ...)
250{
250{
251 va_list ap;
252 int retval;
253
254 retval = printf("%s: ", ic->ic_name);
255 va_start(ap, fmt);
256 retval += vprintf(fmt, ap);
251 va_list ap;
252 int retval;
253
254 retval = printf("%s: ", ic->ic_name);
255 va_start(ap, fmt);
256 retval += vprintf(fmt, ap);
257 va_end(ap);
257 va_end(ap);
258 return (retval);
259}
260
261static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
262static struct mtx ic_list_mtx;
263MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
264
265static int
266sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
267{
268 struct ieee80211com *ic;
269 struct sbuf sb;
270 char *sp;
271 int error;
272
273 error = sysctl_wire_old_buffer(req, 0);
274 if (error)
275 return (error);
276 sbuf_new_for_sysctl(&sb, NULL, 8, req);
277 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
278 sp = "";
279 mtx_lock(&ic_list_mtx);
280 LIST_FOREACH(ic, &ic_head, ic_next) {
281 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
282 sp = " ";
283 }
284 mtx_unlock(&ic_list_mtx);
285 error = sbuf_finish(&sb);
286 sbuf_delete(&sb);
287 return (error);
288}
289
290SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
291 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
292 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
293
294/*
295 * Attach/setup the common net80211 state. Called by
296 * the driver on attach to prior to creating any vap's.
297 */
298void
299ieee80211_ifattach(struct ieee80211com *ic)
300{
301
302 IEEE80211_LOCK_INIT(ic, ic->ic_name);
303 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
304 TAILQ_INIT(&ic->ic_vaps);
305
306 /* Create a taskqueue for all state changes */
307 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
308 taskqueue_thread_enqueue, &ic->ic_tq);
309 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
310 ic->ic_name);
311 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
312 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
313 /*
314 * Fill in 802.11 available channel set, mark all
315 * available channels as active, and pick a default
316 * channel if not already specified.
317 */
318 ieee80211_chan_init(ic);
319
320 ic->ic_update_mcast = null_update_mcast;
321 ic->ic_update_promisc = null_update_promisc;
322 ic->ic_update_chw = null_update_chw;
323
324 ic->ic_hash_key = arc4random();
325 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
326 ic->ic_lintval = ic->ic_bintval;
327 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
328
329 ieee80211_crypto_attach(ic);
330 ieee80211_node_attach(ic);
331 ieee80211_power_attach(ic);
332 ieee80211_proto_attach(ic);
333#ifdef IEEE80211_SUPPORT_SUPERG
334 ieee80211_superg_attach(ic);
335#endif
336 ieee80211_ht_attach(ic);
337 ieee80211_scan_attach(ic);
338 ieee80211_regdomain_attach(ic);
339 ieee80211_dfs_attach(ic);
340
341 ieee80211_sysctl_attach(ic);
342
343 mtx_lock(&ic_list_mtx);
344 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
345 mtx_unlock(&ic_list_mtx);
346}
347
348/*
349 * Detach net80211 state on device detach. Tear down
350 * all vap's and reclaim all common state prior to the
351 * device state going away. Note we may call back into
352 * driver; it must be prepared for this.
353 */
354void
355ieee80211_ifdetach(struct ieee80211com *ic)
356{
357 struct ieee80211vap *vap;
358
258 return (retval);
259}
260
261static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
262static struct mtx ic_list_mtx;
263MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
264
265static int
266sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
267{
268 struct ieee80211com *ic;
269 struct sbuf sb;
270 char *sp;
271 int error;
272
273 error = sysctl_wire_old_buffer(req, 0);
274 if (error)
275 return (error);
276 sbuf_new_for_sysctl(&sb, NULL, 8, req);
277 sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
278 sp = "";
279 mtx_lock(&ic_list_mtx);
280 LIST_FOREACH(ic, &ic_head, ic_next) {
281 sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
282 sp = " ";
283 }
284 mtx_unlock(&ic_list_mtx);
285 error = sbuf_finish(&sb);
286 sbuf_delete(&sb);
287 return (error);
288}
289
290SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
291 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
292 sysctl_ieee80211coms, "A", "names of available 802.11 devices");
293
294/*
295 * Attach/setup the common net80211 state. Called by
296 * the driver on attach to prior to creating any vap's.
297 */
298void
299ieee80211_ifattach(struct ieee80211com *ic)
300{
301
302 IEEE80211_LOCK_INIT(ic, ic->ic_name);
303 IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
304 TAILQ_INIT(&ic->ic_vaps);
305
306 /* Create a taskqueue for all state changes */
307 ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
308 taskqueue_thread_enqueue, &ic->ic_tq);
309 taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
310 ic->ic_name);
311 ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
312 ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
313 /*
314 * Fill in 802.11 available channel set, mark all
315 * available channels as active, and pick a default
316 * channel if not already specified.
317 */
318 ieee80211_chan_init(ic);
319
320 ic->ic_update_mcast = null_update_mcast;
321 ic->ic_update_promisc = null_update_promisc;
322 ic->ic_update_chw = null_update_chw;
323
324 ic->ic_hash_key = arc4random();
325 ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
326 ic->ic_lintval = ic->ic_bintval;
327 ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
328
329 ieee80211_crypto_attach(ic);
330 ieee80211_node_attach(ic);
331 ieee80211_power_attach(ic);
332 ieee80211_proto_attach(ic);
333#ifdef IEEE80211_SUPPORT_SUPERG
334 ieee80211_superg_attach(ic);
335#endif
336 ieee80211_ht_attach(ic);
337 ieee80211_scan_attach(ic);
338 ieee80211_regdomain_attach(ic);
339 ieee80211_dfs_attach(ic);
340
341 ieee80211_sysctl_attach(ic);
342
343 mtx_lock(&ic_list_mtx);
344 LIST_INSERT_HEAD(&ic_head, ic, ic_next);
345 mtx_unlock(&ic_list_mtx);
346}
347
348/*
349 * Detach net80211 state on device detach. Tear down
350 * all vap's and reclaim all common state prior to the
351 * device state going away. Note we may call back into
352 * driver; it must be prepared for this.
353 */
354void
355ieee80211_ifdetach(struct ieee80211com *ic)
356{
357 struct ieee80211vap *vap;
358
359 /*
360 * We use this as an indicator that ifattach never had a chance to be
361 * called, e.g. early driver attach failed and ifdetach was called
362 * during subsequent detach. Never fear, for we have nothing to do
363 * here.
364 */
365 if (ic->ic_tq == NULL)
366 return;
367
359 mtx_lock(&ic_list_mtx);
360 LIST_REMOVE(ic, ic_next);
361 mtx_unlock(&ic_list_mtx);
362
363 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
364
365 /*
366 * The VAP is responsible for setting and clearing
367 * the VIMAGE context.
368 */
369 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
370 ieee80211_vap_destroy(vap);
371 ieee80211_waitfor_parent(ic);
372
373 ieee80211_sysctl_detach(ic);
374 ieee80211_dfs_detach(ic);
375 ieee80211_regdomain_detach(ic);
376 ieee80211_scan_detach(ic);
377#ifdef IEEE80211_SUPPORT_SUPERG
378 ieee80211_superg_detach(ic);
379#endif
380 ieee80211_ht_detach(ic);
381 /* NB: must be called before ieee80211_node_detach */
382 ieee80211_proto_detach(ic);
383 ieee80211_crypto_detach(ic);
384 ieee80211_power_detach(ic);
385 ieee80211_node_detach(ic);
386
387 counter_u64_free(ic->ic_ierrors);
388 counter_u64_free(ic->ic_oerrors);
389
390 taskqueue_free(ic->ic_tq);
391 IEEE80211_TX_LOCK_DESTROY(ic);
392 IEEE80211_LOCK_DESTROY(ic);
393}
394
395struct ieee80211com *
396ieee80211_find_com(const char *name)
397{
398 struct ieee80211com *ic;
399
400 mtx_lock(&ic_list_mtx);
401 LIST_FOREACH(ic, &ic_head, ic_next)
402 if (strcmp(ic->ic_name, name) == 0)
403 break;
404 mtx_unlock(&ic_list_mtx);
405
406 return (ic);
407}
408
409void
410ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
411{
412 struct ieee80211com *ic;
413
414 mtx_lock(&ic_list_mtx);
415 LIST_FOREACH(ic, &ic_head, ic_next)
416 (*f)(arg, ic);
417 mtx_unlock(&ic_list_mtx);
418}
419
420/*
421 * Default reset method for use with the ioctl support. This
422 * method is invoked after any state change in the 802.11
423 * layer that should be propagated to the hardware but not
424 * require re-initialization of the 802.11 state machine (e.g
425 * rescanning for an ap). We always return ENETRESET which
426 * should cause the driver to re-initialize the device. Drivers
427 * can override this method to implement more optimized support.
428 */
429static int
430default_reset(struct ieee80211vap *vap, u_long cmd)
431{
432 return ENETRESET;
433}
434
435/*
436 * Add underlying device errors to vap errors.
437 */
438static uint64_t
439ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
440{
441 struct ieee80211vap *vap = ifp->if_softc;
442 struct ieee80211com *ic = vap->iv_ic;
443 uint64_t rv;
444
445 rv = if_get_counter_default(ifp, cnt);
446 switch (cnt) {
447 case IFCOUNTER_OERRORS:
448 rv += counter_u64_fetch(ic->ic_oerrors);
449 break;
450 case IFCOUNTER_IERRORS:
451 rv += counter_u64_fetch(ic->ic_ierrors);
452 break;
453 default:
454 break;
455 }
456
457 return (rv);
458}
459
460/*
461 * Prepare a vap for use. Drivers use this call to
462 * setup net80211 state in new vap's prior attaching
463 * them with ieee80211_vap_attach (below).
464 */
465int
466ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
467 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
468 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
469{
470 struct ifnet *ifp;
471
472 ifp = if_alloc(IFT_ETHER);
473 if (ifp == NULL) {
474 ic_printf(ic, "%s: unable to allocate ifnet\n",
475 __func__);
476 return ENOMEM;
477 }
478 if_initname(ifp, name, unit);
479 ifp->if_softc = vap; /* back pointer */
480 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
481 ifp->if_transmit = ieee80211_vap_transmit;
482 ifp->if_qflush = ieee80211_vap_qflush;
483 ifp->if_ioctl = ieee80211_ioctl;
484 ifp->if_init = ieee80211_init;
485 ifp->if_get_counter = ieee80211_get_counter;
486
487 vap->iv_ifp = ifp;
488 vap->iv_ic = ic;
489 vap->iv_flags = ic->ic_flags; /* propagate common flags */
490 vap->iv_flags_ext = ic->ic_flags_ext;
491 vap->iv_flags_ven = ic->ic_flags_ven;
492 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
493 vap->iv_htcaps = ic->ic_htcaps;
494 vap->iv_htextcaps = ic->ic_htextcaps;
495 vap->iv_opmode = opmode;
496 vap->iv_caps |= ieee80211_opcap[opmode];
497 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
498 switch (opmode) {
499 case IEEE80211_M_WDS:
500 /*
501 * WDS links must specify the bssid of the far end.
502 * For legacy operation this is a static relationship.
503 * For non-legacy operation the station must associate
504 * and be authorized to pass traffic. Plumbing the
505 * vap to the proper node happens when the vap
506 * transitions to RUN state.
507 */
508 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
509 vap->iv_flags |= IEEE80211_F_DESBSSID;
510 if (flags & IEEE80211_CLONE_WDSLEGACY)
511 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
512 break;
513#ifdef IEEE80211_SUPPORT_TDMA
514 case IEEE80211_M_AHDEMO:
515 if (flags & IEEE80211_CLONE_TDMA) {
516 /* NB: checked before clone operation allowed */
517 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
518 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
519 /*
520 * Propagate TDMA capability to mark vap; this
521 * cannot be removed and is used to distinguish
522 * regular ahdemo operation from ahdemo+tdma.
523 */
524 vap->iv_caps |= IEEE80211_C_TDMA;
525 }
526 break;
527#endif
528 default:
529 break;
530 }
531 /* auto-enable s/w beacon miss support */
532 if (flags & IEEE80211_CLONE_NOBEACONS)
533 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
534 /* auto-generated or user supplied MAC address */
535 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
536 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
537 /*
538 * Enable various functionality by default if we're
539 * capable; the driver can override us if it knows better.
540 */
541 if (vap->iv_caps & IEEE80211_C_WME)
542 vap->iv_flags |= IEEE80211_F_WME;
543 if (vap->iv_caps & IEEE80211_C_BURST)
544 vap->iv_flags |= IEEE80211_F_BURST;
545 /* NB: bg scanning only makes sense for station mode right now */
546 if (vap->iv_opmode == IEEE80211_M_STA &&
547 (vap->iv_caps & IEEE80211_C_BGSCAN))
548 vap->iv_flags |= IEEE80211_F_BGSCAN;
549 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
550 /* NB: DFS support only makes sense for ap mode right now */
551 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
552 (vap->iv_caps & IEEE80211_C_DFS))
553 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
554
555 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
556 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
557 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
558 /*
559 * Install a default reset method for the ioctl support;
560 * the driver can override this.
561 */
562 vap->iv_reset = default_reset;
563
564 ieee80211_sysctl_vattach(vap);
565 ieee80211_crypto_vattach(vap);
566 ieee80211_node_vattach(vap);
567 ieee80211_power_vattach(vap);
568 ieee80211_proto_vattach(vap);
569#ifdef IEEE80211_SUPPORT_SUPERG
570 ieee80211_superg_vattach(vap);
571#endif
572 ieee80211_ht_vattach(vap);
573 ieee80211_scan_vattach(vap);
574 ieee80211_regdomain_vattach(vap);
575 ieee80211_radiotap_vattach(vap);
576 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
577
578 return 0;
579}
580
581/*
582 * Activate a vap. State should have been prepared with a
583 * call to ieee80211_vap_setup and by the driver. On return
584 * from this call the vap is ready for use.
585 */
586int
587ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
588 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
589{
590 struct ifnet *ifp = vap->iv_ifp;
591 struct ieee80211com *ic = vap->iv_ic;
592 struct ifmediareq imr;
593 int maxrate;
594
595 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
596 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
597 __func__, ieee80211_opmode_name[vap->iv_opmode],
598 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
599
600 /*
601 * Do late attach work that cannot happen until after
602 * the driver has had a chance to override defaults.
603 */
604 ieee80211_node_latevattach(vap);
605 ieee80211_power_latevattach(vap);
606
607 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
608 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
609 ieee80211_media_status(ifp, &imr);
610 /* NB: strip explicit mode; we're actually in autoselect */
611 ifmedia_set(&vap->iv_media,
612 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
613 if (maxrate)
614 ifp->if_baudrate = IF_Mbps(maxrate);
615
616 ether_ifattach(ifp, macaddr);
617 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
618 /* hook output method setup by ether_ifattach */
619 vap->iv_output = ifp->if_output;
620 ifp->if_output = ieee80211_output;
621 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
622
623 IEEE80211_LOCK(ic);
624 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
625 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
626#ifdef IEEE80211_SUPPORT_SUPERG
627 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
628#endif
629 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
630 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
631 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
632 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
633 IEEE80211_UNLOCK(ic);
634
635 return 1;
636}
637
368 mtx_lock(&ic_list_mtx);
369 LIST_REMOVE(ic, ic_next);
370 mtx_unlock(&ic_list_mtx);
371
372 taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
373
374 /*
375 * The VAP is responsible for setting and clearing
376 * the VIMAGE context.
377 */
378 while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
379 ieee80211_vap_destroy(vap);
380 ieee80211_waitfor_parent(ic);
381
382 ieee80211_sysctl_detach(ic);
383 ieee80211_dfs_detach(ic);
384 ieee80211_regdomain_detach(ic);
385 ieee80211_scan_detach(ic);
386#ifdef IEEE80211_SUPPORT_SUPERG
387 ieee80211_superg_detach(ic);
388#endif
389 ieee80211_ht_detach(ic);
390 /* NB: must be called before ieee80211_node_detach */
391 ieee80211_proto_detach(ic);
392 ieee80211_crypto_detach(ic);
393 ieee80211_power_detach(ic);
394 ieee80211_node_detach(ic);
395
396 counter_u64_free(ic->ic_ierrors);
397 counter_u64_free(ic->ic_oerrors);
398
399 taskqueue_free(ic->ic_tq);
400 IEEE80211_TX_LOCK_DESTROY(ic);
401 IEEE80211_LOCK_DESTROY(ic);
402}
403
404struct ieee80211com *
405ieee80211_find_com(const char *name)
406{
407 struct ieee80211com *ic;
408
409 mtx_lock(&ic_list_mtx);
410 LIST_FOREACH(ic, &ic_head, ic_next)
411 if (strcmp(ic->ic_name, name) == 0)
412 break;
413 mtx_unlock(&ic_list_mtx);
414
415 return (ic);
416}
417
418void
419ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
420{
421 struct ieee80211com *ic;
422
423 mtx_lock(&ic_list_mtx);
424 LIST_FOREACH(ic, &ic_head, ic_next)
425 (*f)(arg, ic);
426 mtx_unlock(&ic_list_mtx);
427}
428
429/*
430 * Default reset method for use with the ioctl support. This
431 * method is invoked after any state change in the 802.11
432 * layer that should be propagated to the hardware but not
433 * require re-initialization of the 802.11 state machine (e.g
434 * rescanning for an ap). We always return ENETRESET which
435 * should cause the driver to re-initialize the device. Drivers
436 * can override this method to implement more optimized support.
437 */
438static int
439default_reset(struct ieee80211vap *vap, u_long cmd)
440{
441 return ENETRESET;
442}
443
444/*
445 * Add underlying device errors to vap errors.
446 */
447static uint64_t
448ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
449{
450 struct ieee80211vap *vap = ifp->if_softc;
451 struct ieee80211com *ic = vap->iv_ic;
452 uint64_t rv;
453
454 rv = if_get_counter_default(ifp, cnt);
455 switch (cnt) {
456 case IFCOUNTER_OERRORS:
457 rv += counter_u64_fetch(ic->ic_oerrors);
458 break;
459 case IFCOUNTER_IERRORS:
460 rv += counter_u64_fetch(ic->ic_ierrors);
461 break;
462 default:
463 break;
464 }
465
466 return (rv);
467}
468
469/*
470 * Prepare a vap for use. Drivers use this call to
471 * setup net80211 state in new vap's prior attaching
472 * them with ieee80211_vap_attach (below).
473 */
474int
475ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
476 const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
477 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
478{
479 struct ifnet *ifp;
480
481 ifp = if_alloc(IFT_ETHER);
482 if (ifp == NULL) {
483 ic_printf(ic, "%s: unable to allocate ifnet\n",
484 __func__);
485 return ENOMEM;
486 }
487 if_initname(ifp, name, unit);
488 ifp->if_softc = vap; /* back pointer */
489 ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
490 ifp->if_transmit = ieee80211_vap_transmit;
491 ifp->if_qflush = ieee80211_vap_qflush;
492 ifp->if_ioctl = ieee80211_ioctl;
493 ifp->if_init = ieee80211_init;
494 ifp->if_get_counter = ieee80211_get_counter;
495
496 vap->iv_ifp = ifp;
497 vap->iv_ic = ic;
498 vap->iv_flags = ic->ic_flags; /* propagate common flags */
499 vap->iv_flags_ext = ic->ic_flags_ext;
500 vap->iv_flags_ven = ic->ic_flags_ven;
501 vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
502 vap->iv_htcaps = ic->ic_htcaps;
503 vap->iv_htextcaps = ic->ic_htextcaps;
504 vap->iv_opmode = opmode;
505 vap->iv_caps |= ieee80211_opcap[opmode];
506 IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
507 switch (opmode) {
508 case IEEE80211_M_WDS:
509 /*
510 * WDS links must specify the bssid of the far end.
511 * For legacy operation this is a static relationship.
512 * For non-legacy operation the station must associate
513 * and be authorized to pass traffic. Plumbing the
514 * vap to the proper node happens when the vap
515 * transitions to RUN state.
516 */
517 IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
518 vap->iv_flags |= IEEE80211_F_DESBSSID;
519 if (flags & IEEE80211_CLONE_WDSLEGACY)
520 vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
521 break;
522#ifdef IEEE80211_SUPPORT_TDMA
523 case IEEE80211_M_AHDEMO:
524 if (flags & IEEE80211_CLONE_TDMA) {
525 /* NB: checked before clone operation allowed */
526 KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
527 ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
528 /*
529 * Propagate TDMA capability to mark vap; this
530 * cannot be removed and is used to distinguish
531 * regular ahdemo operation from ahdemo+tdma.
532 */
533 vap->iv_caps |= IEEE80211_C_TDMA;
534 }
535 break;
536#endif
537 default:
538 break;
539 }
540 /* auto-enable s/w beacon miss support */
541 if (flags & IEEE80211_CLONE_NOBEACONS)
542 vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
543 /* auto-generated or user supplied MAC address */
544 if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
545 vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
546 /*
547 * Enable various functionality by default if we're
548 * capable; the driver can override us if it knows better.
549 */
550 if (vap->iv_caps & IEEE80211_C_WME)
551 vap->iv_flags |= IEEE80211_F_WME;
552 if (vap->iv_caps & IEEE80211_C_BURST)
553 vap->iv_flags |= IEEE80211_F_BURST;
554 /* NB: bg scanning only makes sense for station mode right now */
555 if (vap->iv_opmode == IEEE80211_M_STA &&
556 (vap->iv_caps & IEEE80211_C_BGSCAN))
557 vap->iv_flags |= IEEE80211_F_BGSCAN;
558 vap->iv_flags |= IEEE80211_F_DOTH; /* XXX no cap, just ena */
559 /* NB: DFS support only makes sense for ap mode right now */
560 if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
561 (vap->iv_caps & IEEE80211_C_DFS))
562 vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
563
564 vap->iv_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
565 vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
566 vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
567 /*
568 * Install a default reset method for the ioctl support;
569 * the driver can override this.
570 */
571 vap->iv_reset = default_reset;
572
573 ieee80211_sysctl_vattach(vap);
574 ieee80211_crypto_vattach(vap);
575 ieee80211_node_vattach(vap);
576 ieee80211_power_vattach(vap);
577 ieee80211_proto_vattach(vap);
578#ifdef IEEE80211_SUPPORT_SUPERG
579 ieee80211_superg_vattach(vap);
580#endif
581 ieee80211_ht_vattach(vap);
582 ieee80211_scan_vattach(vap);
583 ieee80211_regdomain_vattach(vap);
584 ieee80211_radiotap_vattach(vap);
585 ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
586
587 return 0;
588}
589
590/*
591 * Activate a vap. State should have been prepared with a
592 * call to ieee80211_vap_setup and by the driver. On return
593 * from this call the vap is ready for use.
594 */
595int
596ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
597 ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
598{
599 struct ifnet *ifp = vap->iv_ifp;
600 struct ieee80211com *ic = vap->iv_ic;
601 struct ifmediareq imr;
602 int maxrate;
603
604 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
605 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
606 __func__, ieee80211_opmode_name[vap->iv_opmode],
607 ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
608
609 /*
610 * Do late attach work that cannot happen until after
611 * the driver has had a chance to override defaults.
612 */
613 ieee80211_node_latevattach(vap);
614 ieee80211_power_latevattach(vap);
615
616 maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
617 vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
618 ieee80211_media_status(ifp, &imr);
619 /* NB: strip explicit mode; we're actually in autoselect */
620 ifmedia_set(&vap->iv_media,
621 imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
622 if (maxrate)
623 ifp->if_baudrate = IF_Mbps(maxrate);
624
625 ether_ifattach(ifp, macaddr);
626 IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
627 /* hook output method setup by ether_ifattach */
628 vap->iv_output = ifp->if_output;
629 ifp->if_output = ieee80211_output;
630 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
631
632 IEEE80211_LOCK(ic);
633 TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
634 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
635#ifdef IEEE80211_SUPPORT_SUPERG
636 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
637#endif
638 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
639 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
640 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
641 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
642 IEEE80211_UNLOCK(ic);
643
644 return 1;
645}
646
638/*
647/*
639 * Tear down vap state and reclaim the ifnet.
640 * The driver is assumed to have prepared for
641 * this; e.g. by turning off interrupts for the
642 * underlying device.
643 */
644void
645ieee80211_vap_detach(struct ieee80211vap *vap)
646{
647 struct ieee80211com *ic = vap->iv_ic;
648 struct ifnet *ifp = vap->iv_ifp;
649
650 CURVNET_SET(ifp->if_vnet);
651
652 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
653 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
654
655 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
656 ether_ifdetach(ifp);
657
658 ieee80211_stop(vap);
659
660 /*
661 * Flush any deferred vap tasks.
662 */
663 ieee80211_draintask(ic, &vap->iv_nstate_task);
664 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
665
666 /* XXX band-aid until ifnet handles this for us */
667 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
668
669 IEEE80211_LOCK(ic);
670 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
671 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
672 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
673#ifdef IEEE80211_SUPPORT_SUPERG
674 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
675#endif
676 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
677 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
678 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
679 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
680 /* NB: this handles the bpfdetach done below */
681 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
682 if (vap->iv_ifflags & IFF_PROMISC)
683 ieee80211_promisc(vap, false);
684 if (vap->iv_ifflags & IFF_ALLMULTI)
685 ieee80211_allmulti(vap, false);
686 IEEE80211_UNLOCK(ic);
687
688 ifmedia_removeall(&vap->iv_media);
689
690 ieee80211_radiotap_vdetach(vap);
691 ieee80211_regdomain_vdetach(vap);
692 ieee80211_scan_vdetach(vap);
693#ifdef IEEE80211_SUPPORT_SUPERG
694 ieee80211_superg_vdetach(vap);
695#endif
696 ieee80211_ht_vdetach(vap);
697 /* NB: must be before ieee80211_node_vdetach */
698 ieee80211_proto_vdetach(vap);
699 ieee80211_crypto_vdetach(vap);
700 ieee80211_power_vdetach(vap);
701 ieee80211_node_vdetach(vap);
702 ieee80211_sysctl_vdetach(vap);
703
704 if_free(ifp);
705
706 CURVNET_RESTORE();
707}
708
709/*
710 * Count number of vaps in promisc, and issue promisc on
711 * parent respectively.
712 */
713void
714ieee80211_promisc(struct ieee80211vap *vap, bool on)
715{
716 struct ieee80211com *ic = vap->iv_ic;
717
718 IEEE80211_LOCK_ASSERT(ic);
719
720 if (on) {
721 if (++ic->ic_promisc == 1)
722 ieee80211_runtask(ic, &ic->ic_promisc_task);
723 } else {
724 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
725 __func__, ic));
726 if (--ic->ic_promisc == 0)
727 ieee80211_runtask(ic, &ic->ic_promisc_task);
728 }
729}
730
731/*
732 * Count number of vaps in allmulti, and issue allmulti on
733 * parent respectively.
734 */
735void
736ieee80211_allmulti(struct ieee80211vap *vap, bool on)
737{
738 struct ieee80211com *ic = vap->iv_ic;
739
740 IEEE80211_LOCK_ASSERT(ic);
741
742 if (on) {
743 if (++ic->ic_allmulti == 1)
744 ieee80211_runtask(ic, &ic->ic_mcast_task);
745 } else {
746 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
747 __func__, ic));
748 if (--ic->ic_allmulti == 0)
749 ieee80211_runtask(ic, &ic->ic_mcast_task);
750 }
751}
752
753/*
754 * Synchronize flag bit state in the com structure
755 * according to the state of all vap's. This is used,
756 * for example, to handle state changes via ioctls.
757 */
758static void
759ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
760{
761 struct ieee80211vap *vap;
762 int bit;
763
764 IEEE80211_LOCK_ASSERT(ic);
765
766 bit = 0;
767 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
768 if (vap->iv_flags & flag) {
769 bit = 1;
770 break;
771 }
772 if (bit)
773 ic->ic_flags |= flag;
774 else
775 ic->ic_flags &= ~flag;
776}
777
778void
779ieee80211_syncflag(struct ieee80211vap *vap, int flag)
780{
781 struct ieee80211com *ic = vap->iv_ic;
782
783 IEEE80211_LOCK(ic);
784 if (flag < 0) {
785 flag = -flag;
786 vap->iv_flags &= ~flag;
787 } else
788 vap->iv_flags |= flag;
789 ieee80211_syncflag_locked(ic, flag);
790 IEEE80211_UNLOCK(ic);
791}
792
793/*
794 * Synchronize flags_ht bit state in the com structure
795 * according to the state of all vap's. This is used,
796 * for example, to handle state changes via ioctls.
797 */
798static void
799ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
800{
801 struct ieee80211vap *vap;
802 int bit;
803
804 IEEE80211_LOCK_ASSERT(ic);
805
806 bit = 0;
807 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
808 if (vap->iv_flags_ht & flag) {
809 bit = 1;
810 break;
811 }
812 if (bit)
813 ic->ic_flags_ht |= flag;
814 else
815 ic->ic_flags_ht &= ~flag;
816}
817
818void
819ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
820{
821 struct ieee80211com *ic = vap->iv_ic;
822
823 IEEE80211_LOCK(ic);
824 if (flag < 0) {
825 flag = -flag;
826 vap->iv_flags_ht &= ~flag;
827 } else
828 vap->iv_flags_ht |= flag;
829 ieee80211_syncflag_ht_locked(ic, flag);
830 IEEE80211_UNLOCK(ic);
831}
832
833/*
834 * Synchronize flags_ext bit state in the com structure
835 * according to the state of all vap's. This is used,
836 * for example, to handle state changes via ioctls.
837 */
838static void
839ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
840{
841 struct ieee80211vap *vap;
842 int bit;
843
844 IEEE80211_LOCK_ASSERT(ic);
845
846 bit = 0;
847 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
848 if (vap->iv_flags_ext & flag) {
849 bit = 1;
850 break;
851 }
852 if (bit)
853 ic->ic_flags_ext |= flag;
854 else
855 ic->ic_flags_ext &= ~flag;
856}
857
858void
859ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
860{
861 struct ieee80211com *ic = vap->iv_ic;
862
863 IEEE80211_LOCK(ic);
864 if (flag < 0) {
865 flag = -flag;
866 vap->iv_flags_ext &= ~flag;
867 } else
868 vap->iv_flags_ext |= flag;
869 ieee80211_syncflag_ext_locked(ic, flag);
870 IEEE80211_UNLOCK(ic);
871}
872
873static __inline int
874mapgsm(u_int freq, u_int flags)
875{
876 freq *= 10;
877 if (flags & IEEE80211_CHAN_QUARTER)
878 freq += 5;
879 else if (flags & IEEE80211_CHAN_HALF)
880 freq += 10;
881 else
882 freq += 20;
883 /* NB: there is no 907/20 wide but leave room */
884 return (freq - 906*10) / 5;
885}
886
887static __inline int
888mappsb(u_int freq, u_int flags)
889{
890 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
891}
892
893/*
894 * Convert MHz frequency to IEEE channel number.
895 */
896int
897ieee80211_mhz2ieee(u_int freq, u_int flags)
898{
899#define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
900 if (flags & IEEE80211_CHAN_GSM)
901 return mapgsm(freq, flags);
902 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
903 if (freq == 2484)
904 return 14;
905 if (freq < 2484)
906 return ((int) freq - 2407) / 5;
907 else
908 return 15 + ((freq - 2512) / 20);
909 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
910 if (freq <= 5000) {
911 /* XXX check regdomain? */
912 if (IS_FREQ_IN_PSB(freq))
913 return mappsb(freq, flags);
914 return (freq - 4000) / 5;
915 } else
916 return (freq - 5000) / 5;
917 } else { /* either, guess */
918 if (freq == 2484)
919 return 14;
920 if (freq < 2484) {
921 if (907 <= freq && freq <= 922)
922 return mapgsm(freq, flags);
923 return ((int) freq - 2407) / 5;
924 }
925 if (freq < 5000) {
926 if (IS_FREQ_IN_PSB(freq))
927 return mappsb(freq, flags);
928 else if (freq > 4900)
929 return (freq - 4000) / 5;
930 else
931 return 15 + ((freq - 2512) / 20);
932 }
933 return (freq - 5000) / 5;
934 }
935#undef IS_FREQ_IN_PSB
936}
937
938/*
939 * Convert channel to IEEE channel number.
940 */
941int
942ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
943{
944 if (c == NULL) {
945 ic_printf(ic, "invalid channel (NULL)\n");
946 return 0; /* XXX */
947 }
948 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
949}
950
951/*
952 * Convert IEEE channel number to MHz frequency.
953 */
954u_int
955ieee80211_ieee2mhz(u_int chan, u_int flags)
956{
957 if (flags & IEEE80211_CHAN_GSM)
958 return 907 + 5 * (chan / 10);
959 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
960 if (chan == 14)
961 return 2484;
962 if (chan < 14)
963 return 2407 + chan*5;
964 else
965 return 2512 + ((chan-15)*20);
966 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
967 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
968 chan -= 37;
969 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
970 }
971 return 5000 + (chan*5);
972 } else { /* either, guess */
973 /* XXX can't distinguish PSB+GSM channels */
974 if (chan == 14)
975 return 2484;
976 if (chan < 14) /* 0-13 */
977 return 2407 + chan*5;
978 if (chan < 27) /* 15-26 */
979 return 2512 + ((chan-15)*20);
980 return 5000 + (chan*5);
981 }
982}
983
984static __inline void
985set_extchan(struct ieee80211_channel *c)
986{
987
988 /*
989 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
990 * "the secondary channel number shall be 'N + [1,-1] * 4'
991 */
992 if (c->ic_flags & IEEE80211_CHAN_HT40U)
993 c->ic_extieee = c->ic_ieee + 4;
994 else if (c->ic_flags & IEEE80211_CHAN_HT40D)
995 c->ic_extieee = c->ic_ieee - 4;
996 else
997 c->ic_extieee = 0;
998}
999
1000static int
1001addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1002 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1003{
1004 struct ieee80211_channel *c;
1005
1006 if (*nchans >= maxchans)
1007 return (ENOBUFS);
1008
1009 c = &chans[(*nchans)++];
1010 c->ic_ieee = ieee;
1011 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1012 c->ic_maxregpower = maxregpower;
1013 c->ic_maxpower = 2 * maxregpower;
1014 c->ic_flags = flags;
1015 set_extchan(c);
1016
1017 return (0);
1018}
1019
1020static int
1021copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1022 uint32_t flags)
1023{
1024 struct ieee80211_channel *c;
1025
1026 KASSERT(*nchans > 0, ("channel list is empty\n"));
1027
1028 if (*nchans >= maxchans)
1029 return (ENOBUFS);
1030
1031 c = &chans[(*nchans)++];
1032 c[0] = c[-1];
1033 c->ic_flags = flags;
1034 set_extchan(c);
1035
1036 return (0);
1037}
1038
1039static void
1040getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1041{
1042 int nmodes;
1043
1044 nmodes = 0;
1045 if (isset(bands, IEEE80211_MODE_11B))
1046 flags[nmodes++] = IEEE80211_CHAN_B;
1047 if (isset(bands, IEEE80211_MODE_11G))
1048 flags[nmodes++] = IEEE80211_CHAN_G;
1049 if (isset(bands, IEEE80211_MODE_11NG))
1050 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1051 if (ht40) {
1052 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1053 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1054 }
1055 flags[nmodes] = 0;
1056}
1057
1058static void
1059getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1060{
1061 int nmodes;
1062
1063 nmodes = 0;
1064 if (isset(bands, IEEE80211_MODE_11A))
1065 flags[nmodes++] = IEEE80211_CHAN_A;
1066 if (isset(bands, IEEE80211_MODE_11NA))
1067 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1068 if (ht40) {
1069 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1070 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1071 }
1072 flags[nmodes] = 0;
1073}
1074
1075static void
1076getflags(const uint8_t bands[], uint32_t flags[], int ht40)
1077{
1078
1079 flags[0] = 0;
1080 if (isset(bands, IEEE80211_MODE_11A) ||
1081 isset(bands, IEEE80211_MODE_11NA)) {
1082 if (isset(bands, IEEE80211_MODE_11B) ||
1083 isset(bands, IEEE80211_MODE_11G) ||
1084 isset(bands, IEEE80211_MODE_11NG))
1085 return;
1086
1087 getflags_5ghz(bands, flags, ht40);
1088 } else
1089 getflags_2ghz(bands, flags, ht40);
1090}
1091
1092/*
1093 * Add one 20 MHz channel into specified channel list.
1094 */
1095int
1096ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1097 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1098 uint32_t chan_flags, const uint8_t bands[])
1099{
1100 uint32_t flags[IEEE80211_MODE_MAX];
1101 int i, error;
1102
1103 getflags(bands, flags, 0);
1104 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1105
1106 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1107 flags[0] | chan_flags);
1108 for (i = 1; flags[i] != 0 && error == 0; i++) {
1109 error = copychan_prev(chans, maxchans, nchans,
1110 flags[i] | chan_flags);
1111 }
1112
1113 return (error);
1114}
1115
1116static struct ieee80211_channel *
1117findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1118 uint32_t flags)
1119{
1120 struct ieee80211_channel *c;
1121 int i;
1122
1123 flags &= IEEE80211_CHAN_ALLTURBO;
1124 /* brute force search */
1125 for (i = 0; i < nchans; i++) {
1126 c = &chans[i];
1127 if (c->ic_freq == freq &&
1128 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1129 return c;
1130 }
1131 return NULL;
1132}
1133
1134/*
1135 * Add 40 MHz channel pair into specified channel list.
1136 */
1137int
1138ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1139 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1140{
1141 struct ieee80211_channel *cent, *extc;
1142 uint16_t freq;
1143 int error;
1144
1145 freq = ieee80211_ieee2mhz(ieee, flags);
1146
1147 /*
1148 * Each entry defines an HT40 channel pair; find the
1149 * center channel, then the extension channel above.
1150 */
1151 flags |= IEEE80211_CHAN_HT20;
1152 cent = findchannel(chans, *nchans, freq, flags);
1153 if (cent == NULL)
1154 return (EINVAL);
1155
1156 extc = findchannel(chans, *nchans, freq + 20, flags);
1157 if (extc == NULL)
1158 return (ENOENT);
1159
1160 flags &= ~IEEE80211_CHAN_HT;
1161 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1162 maxregpower, flags | IEEE80211_CHAN_HT40U);
1163 if (error != 0)
1164 return (error);
1165
1166 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1167 maxregpower, flags | IEEE80211_CHAN_HT40D);
1168
1169 return (error);
1170}
1171
1172/*
1173 * Adds channels into specified channel list (ieee[] array must be sorted).
1174 * Channels are already sorted.
1175 */
1176static int
1177add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1178 const uint8_t ieee[], int nieee, uint32_t flags[])
1179{
1180 uint16_t freq;
1181 int i, j, error;
1182
1183 for (i = 0; i < nieee; i++) {
1184 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1185 for (j = 0; flags[j] != 0; j++) {
1186 if (flags[j] & IEEE80211_CHAN_HT40D)
1187 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1188 freq - 20 !=
1189 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1190 continue;
1191 if (flags[j] & IEEE80211_CHAN_HT40U)
1192 if (i == nieee - 1 ||
1193 ieee[i] + 4 > ieee[nieee - 1] ||
1194 freq + 20 !=
1195 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1196 continue;
1197
1198 if (j == 0) {
1199 error = addchan(chans, maxchans, nchans,
1200 ieee[i], freq, 0, flags[j]);
1201 } else {
1202 error = copychan_prev(chans, maxchans, nchans,
1203 flags[j]);
1204 }
1205 if (error != 0)
1206 return (error);
1207 }
1208 }
1209
1210 return (0);
1211}
1212
1213int
1214ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1215 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1216 int ht40)
1217{
1218 uint32_t flags[IEEE80211_MODE_MAX];
1219
1220 getflags_2ghz(bands, flags, ht40);
1221 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1222
1223 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1224}
1225
1226int
1227ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1228 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1229 int ht40)
1230{
1231 uint32_t flags[IEEE80211_MODE_MAX];
1232
1233 getflags_5ghz(bands, flags, ht40);
1234 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1235
1236 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1237}
1238
1239/*
1240 * Locate a channel given a frequency+flags. We cache
1241 * the previous lookup to optimize switching between two
1242 * channels--as happens with dynamic turbo.
1243 */
1244struct ieee80211_channel *
1245ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1246{
1247 struct ieee80211_channel *c;
1248
1249 flags &= IEEE80211_CHAN_ALLTURBO;
1250 c = ic->ic_prevchan;
1251 if (c != NULL && c->ic_freq == freq &&
1252 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1253 return c;
1254 /* brute force search */
1255 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1256}
1257
1258/*
1259 * Locate a channel given a channel number+flags. We cache
1260 * the previous lookup to optimize switching between two
1261 * channels--as happens with dynamic turbo.
1262 */
1263struct ieee80211_channel *
1264ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1265{
1266 struct ieee80211_channel *c;
1267 int i;
1268
1269 flags &= IEEE80211_CHAN_ALLTURBO;
1270 c = ic->ic_prevchan;
1271 if (c != NULL && c->ic_ieee == ieee &&
1272 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1273 return c;
1274 /* brute force search */
1275 for (i = 0; i < ic->ic_nchans; i++) {
1276 c = &ic->ic_channels[i];
1277 if (c->ic_ieee == ieee &&
1278 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1279 return c;
1280 }
1281 return NULL;
1282}
1283
1284/*
1285 * Lookup a channel suitable for the given rx status.
1286 *
1287 * This is used to find a channel for a frame (eg beacon, probe
1288 * response) based purely on the received PHY information.
1289 *
1290 * For now it tries to do it based on R_FREQ / R_IEEE.
1291 * This is enough for 11bg and 11a (and thus 11ng/11na)
1292 * but it will not be enough for GSM, PSB channels and the
1293 * like. It also doesn't know about legacy-turbog and
1294 * legacy-turbo modes, which some offload NICs actually
1295 * support in weird ways.
1296 *
1297 * Takes the ic and rxstatus; returns the channel or NULL
1298 * if not found.
1299 *
1300 * XXX TODO: Add support for that when the need arises.
1301 */
1302struct ieee80211_channel *
1303ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1304 const struct ieee80211_rx_stats *rxs)
1305{
1306 struct ieee80211com *ic = vap->iv_ic;
1307 uint32_t flags;
1308 struct ieee80211_channel *c;
1309
1310 if (rxs == NULL)
1311 return (NULL);
1312
1313 /*
1314 * Strictly speaking we only use freq for now,
1315 * however later on we may wish to just store
1316 * the ieee for verification.
1317 */
1318 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1319 return (NULL);
1320 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1321 return (NULL);
1322
1323 /*
1324 * If the rx status contains a valid ieee/freq, then
1325 * ensure we populate the correct channel information
1326 * in rxchan before passing it up to the scan infrastructure.
1327 * Offload NICs will pass up beacons from all channels
1328 * during background scans.
1329 */
1330
1331 /* Determine a band */
1332 /* XXX should be done by the driver? */
1333 if (rxs->c_freq < 3000) {
1334 flags = IEEE80211_CHAN_G;
1335 } else {
1336 flags = IEEE80211_CHAN_A;
1337 }
1338
1339 /* Channel lookup */
1340 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1341
1342 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1343 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1344 __func__,
1345 (int) rxs->c_freq,
1346 (int) rxs->c_ieee,
1347 flags,
1348 c);
1349
1350 return (c);
1351}
1352
1353static void
1354addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1355{
1356#define ADD(_ic, _s, _o) \
1357 ifmedia_add(media, \
1358 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
648 * Tear down vap state and reclaim the ifnet.
649 * The driver is assumed to have prepared for
650 * this; e.g. by turning off interrupts for the
651 * underlying device.
652 */
653void
654ieee80211_vap_detach(struct ieee80211vap *vap)
655{
656 struct ieee80211com *ic = vap->iv_ic;
657 struct ifnet *ifp = vap->iv_ifp;
658
659 CURVNET_SET(ifp->if_vnet);
660
661 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
662 __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
663
664 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
665 ether_ifdetach(ifp);
666
667 ieee80211_stop(vap);
668
669 /*
670 * Flush any deferred vap tasks.
671 */
672 ieee80211_draintask(ic, &vap->iv_nstate_task);
673 ieee80211_draintask(ic, &vap->iv_swbmiss_task);
674
675 /* XXX band-aid until ifnet handles this for us */
676 taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
677
678 IEEE80211_LOCK(ic);
679 KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
680 TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
681 ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
682#ifdef IEEE80211_SUPPORT_SUPERG
683 ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
684#endif
685 ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
686 ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
687 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
688 ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
689 /* NB: this handles the bpfdetach done below */
690 ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
691 if (vap->iv_ifflags & IFF_PROMISC)
692 ieee80211_promisc(vap, false);
693 if (vap->iv_ifflags & IFF_ALLMULTI)
694 ieee80211_allmulti(vap, false);
695 IEEE80211_UNLOCK(ic);
696
697 ifmedia_removeall(&vap->iv_media);
698
699 ieee80211_radiotap_vdetach(vap);
700 ieee80211_regdomain_vdetach(vap);
701 ieee80211_scan_vdetach(vap);
702#ifdef IEEE80211_SUPPORT_SUPERG
703 ieee80211_superg_vdetach(vap);
704#endif
705 ieee80211_ht_vdetach(vap);
706 /* NB: must be before ieee80211_node_vdetach */
707 ieee80211_proto_vdetach(vap);
708 ieee80211_crypto_vdetach(vap);
709 ieee80211_power_vdetach(vap);
710 ieee80211_node_vdetach(vap);
711 ieee80211_sysctl_vdetach(vap);
712
713 if_free(ifp);
714
715 CURVNET_RESTORE();
716}
717
718/*
719 * Count number of vaps in promisc, and issue promisc on
720 * parent respectively.
721 */
722void
723ieee80211_promisc(struct ieee80211vap *vap, bool on)
724{
725 struct ieee80211com *ic = vap->iv_ic;
726
727 IEEE80211_LOCK_ASSERT(ic);
728
729 if (on) {
730 if (++ic->ic_promisc == 1)
731 ieee80211_runtask(ic, &ic->ic_promisc_task);
732 } else {
733 KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
734 __func__, ic));
735 if (--ic->ic_promisc == 0)
736 ieee80211_runtask(ic, &ic->ic_promisc_task);
737 }
738}
739
740/*
741 * Count number of vaps in allmulti, and issue allmulti on
742 * parent respectively.
743 */
744void
745ieee80211_allmulti(struct ieee80211vap *vap, bool on)
746{
747 struct ieee80211com *ic = vap->iv_ic;
748
749 IEEE80211_LOCK_ASSERT(ic);
750
751 if (on) {
752 if (++ic->ic_allmulti == 1)
753 ieee80211_runtask(ic, &ic->ic_mcast_task);
754 } else {
755 KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
756 __func__, ic));
757 if (--ic->ic_allmulti == 0)
758 ieee80211_runtask(ic, &ic->ic_mcast_task);
759 }
760}
761
762/*
763 * Synchronize flag bit state in the com structure
764 * according to the state of all vap's. This is used,
765 * for example, to handle state changes via ioctls.
766 */
767static void
768ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
769{
770 struct ieee80211vap *vap;
771 int bit;
772
773 IEEE80211_LOCK_ASSERT(ic);
774
775 bit = 0;
776 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
777 if (vap->iv_flags & flag) {
778 bit = 1;
779 break;
780 }
781 if (bit)
782 ic->ic_flags |= flag;
783 else
784 ic->ic_flags &= ~flag;
785}
786
787void
788ieee80211_syncflag(struct ieee80211vap *vap, int flag)
789{
790 struct ieee80211com *ic = vap->iv_ic;
791
792 IEEE80211_LOCK(ic);
793 if (flag < 0) {
794 flag = -flag;
795 vap->iv_flags &= ~flag;
796 } else
797 vap->iv_flags |= flag;
798 ieee80211_syncflag_locked(ic, flag);
799 IEEE80211_UNLOCK(ic);
800}
801
802/*
803 * Synchronize flags_ht bit state in the com structure
804 * according to the state of all vap's. This is used,
805 * for example, to handle state changes via ioctls.
806 */
807static void
808ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
809{
810 struct ieee80211vap *vap;
811 int bit;
812
813 IEEE80211_LOCK_ASSERT(ic);
814
815 bit = 0;
816 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
817 if (vap->iv_flags_ht & flag) {
818 bit = 1;
819 break;
820 }
821 if (bit)
822 ic->ic_flags_ht |= flag;
823 else
824 ic->ic_flags_ht &= ~flag;
825}
826
827void
828ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
829{
830 struct ieee80211com *ic = vap->iv_ic;
831
832 IEEE80211_LOCK(ic);
833 if (flag < 0) {
834 flag = -flag;
835 vap->iv_flags_ht &= ~flag;
836 } else
837 vap->iv_flags_ht |= flag;
838 ieee80211_syncflag_ht_locked(ic, flag);
839 IEEE80211_UNLOCK(ic);
840}
841
842/*
843 * Synchronize flags_ext bit state in the com structure
844 * according to the state of all vap's. This is used,
845 * for example, to handle state changes via ioctls.
846 */
847static void
848ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
849{
850 struct ieee80211vap *vap;
851 int bit;
852
853 IEEE80211_LOCK_ASSERT(ic);
854
855 bit = 0;
856 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
857 if (vap->iv_flags_ext & flag) {
858 bit = 1;
859 break;
860 }
861 if (bit)
862 ic->ic_flags_ext |= flag;
863 else
864 ic->ic_flags_ext &= ~flag;
865}
866
867void
868ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
869{
870 struct ieee80211com *ic = vap->iv_ic;
871
872 IEEE80211_LOCK(ic);
873 if (flag < 0) {
874 flag = -flag;
875 vap->iv_flags_ext &= ~flag;
876 } else
877 vap->iv_flags_ext |= flag;
878 ieee80211_syncflag_ext_locked(ic, flag);
879 IEEE80211_UNLOCK(ic);
880}
881
882static __inline int
883mapgsm(u_int freq, u_int flags)
884{
885 freq *= 10;
886 if (flags & IEEE80211_CHAN_QUARTER)
887 freq += 5;
888 else if (flags & IEEE80211_CHAN_HALF)
889 freq += 10;
890 else
891 freq += 20;
892 /* NB: there is no 907/20 wide but leave room */
893 return (freq - 906*10) / 5;
894}
895
896static __inline int
897mappsb(u_int freq, u_int flags)
898{
899 return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
900}
901
902/*
903 * Convert MHz frequency to IEEE channel number.
904 */
905int
906ieee80211_mhz2ieee(u_int freq, u_int flags)
907{
908#define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
909 if (flags & IEEE80211_CHAN_GSM)
910 return mapgsm(freq, flags);
911 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
912 if (freq == 2484)
913 return 14;
914 if (freq < 2484)
915 return ((int) freq - 2407) / 5;
916 else
917 return 15 + ((freq - 2512) / 20);
918 } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5Ghz band */
919 if (freq <= 5000) {
920 /* XXX check regdomain? */
921 if (IS_FREQ_IN_PSB(freq))
922 return mappsb(freq, flags);
923 return (freq - 4000) / 5;
924 } else
925 return (freq - 5000) / 5;
926 } else { /* either, guess */
927 if (freq == 2484)
928 return 14;
929 if (freq < 2484) {
930 if (907 <= freq && freq <= 922)
931 return mapgsm(freq, flags);
932 return ((int) freq - 2407) / 5;
933 }
934 if (freq < 5000) {
935 if (IS_FREQ_IN_PSB(freq))
936 return mappsb(freq, flags);
937 else if (freq > 4900)
938 return (freq - 4000) / 5;
939 else
940 return 15 + ((freq - 2512) / 20);
941 }
942 return (freq - 5000) / 5;
943 }
944#undef IS_FREQ_IN_PSB
945}
946
947/*
948 * Convert channel to IEEE channel number.
949 */
950int
951ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
952{
953 if (c == NULL) {
954 ic_printf(ic, "invalid channel (NULL)\n");
955 return 0; /* XXX */
956 }
957 return (c == IEEE80211_CHAN_ANYC ? IEEE80211_CHAN_ANY : c->ic_ieee);
958}
959
960/*
961 * Convert IEEE channel number to MHz frequency.
962 */
963u_int
964ieee80211_ieee2mhz(u_int chan, u_int flags)
965{
966 if (flags & IEEE80211_CHAN_GSM)
967 return 907 + 5 * (chan / 10);
968 if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
969 if (chan == 14)
970 return 2484;
971 if (chan < 14)
972 return 2407 + chan*5;
973 else
974 return 2512 + ((chan-15)*20);
975 } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
976 if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
977 chan -= 37;
978 return 4940 + chan*5 + (chan % 5 ? 2 : 0);
979 }
980 return 5000 + (chan*5);
981 } else { /* either, guess */
982 /* XXX can't distinguish PSB+GSM channels */
983 if (chan == 14)
984 return 2484;
985 if (chan < 14) /* 0-13 */
986 return 2407 + chan*5;
987 if (chan < 27) /* 15-26 */
988 return 2512 + ((chan-15)*20);
989 return 5000 + (chan*5);
990 }
991}
992
993static __inline void
994set_extchan(struct ieee80211_channel *c)
995{
996
997 /*
998 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
999 * "the secondary channel number shall be 'N + [1,-1] * 4'
1000 */
1001 if (c->ic_flags & IEEE80211_CHAN_HT40U)
1002 c->ic_extieee = c->ic_ieee + 4;
1003 else if (c->ic_flags & IEEE80211_CHAN_HT40D)
1004 c->ic_extieee = c->ic_ieee - 4;
1005 else
1006 c->ic_extieee = 0;
1007}
1008
1009static int
1010addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
1011 uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
1012{
1013 struct ieee80211_channel *c;
1014
1015 if (*nchans >= maxchans)
1016 return (ENOBUFS);
1017
1018 c = &chans[(*nchans)++];
1019 c->ic_ieee = ieee;
1020 c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
1021 c->ic_maxregpower = maxregpower;
1022 c->ic_maxpower = 2 * maxregpower;
1023 c->ic_flags = flags;
1024 set_extchan(c);
1025
1026 return (0);
1027}
1028
1029static int
1030copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
1031 uint32_t flags)
1032{
1033 struct ieee80211_channel *c;
1034
1035 KASSERT(*nchans > 0, ("channel list is empty\n"));
1036
1037 if (*nchans >= maxchans)
1038 return (ENOBUFS);
1039
1040 c = &chans[(*nchans)++];
1041 c[0] = c[-1];
1042 c->ic_flags = flags;
1043 set_extchan(c);
1044
1045 return (0);
1046}
1047
1048static void
1049getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1050{
1051 int nmodes;
1052
1053 nmodes = 0;
1054 if (isset(bands, IEEE80211_MODE_11B))
1055 flags[nmodes++] = IEEE80211_CHAN_B;
1056 if (isset(bands, IEEE80211_MODE_11G))
1057 flags[nmodes++] = IEEE80211_CHAN_G;
1058 if (isset(bands, IEEE80211_MODE_11NG))
1059 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
1060 if (ht40) {
1061 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
1062 flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
1063 }
1064 flags[nmodes] = 0;
1065}
1066
1067static void
1068getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40)
1069{
1070 int nmodes;
1071
1072 nmodes = 0;
1073 if (isset(bands, IEEE80211_MODE_11A))
1074 flags[nmodes++] = IEEE80211_CHAN_A;
1075 if (isset(bands, IEEE80211_MODE_11NA))
1076 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
1077 if (ht40) {
1078 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
1079 flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
1080 }
1081 flags[nmodes] = 0;
1082}
1083
1084static void
1085getflags(const uint8_t bands[], uint32_t flags[], int ht40)
1086{
1087
1088 flags[0] = 0;
1089 if (isset(bands, IEEE80211_MODE_11A) ||
1090 isset(bands, IEEE80211_MODE_11NA)) {
1091 if (isset(bands, IEEE80211_MODE_11B) ||
1092 isset(bands, IEEE80211_MODE_11G) ||
1093 isset(bands, IEEE80211_MODE_11NG))
1094 return;
1095
1096 getflags_5ghz(bands, flags, ht40);
1097 } else
1098 getflags_2ghz(bands, flags, ht40);
1099}
1100
1101/*
1102 * Add one 20 MHz channel into specified channel list.
1103 */
1104int
1105ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
1106 int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
1107 uint32_t chan_flags, const uint8_t bands[])
1108{
1109 uint32_t flags[IEEE80211_MODE_MAX];
1110 int i, error;
1111
1112 getflags(bands, flags, 0);
1113 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1114
1115 error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
1116 flags[0] | chan_flags);
1117 for (i = 1; flags[i] != 0 && error == 0; i++) {
1118 error = copychan_prev(chans, maxchans, nchans,
1119 flags[i] | chan_flags);
1120 }
1121
1122 return (error);
1123}
1124
1125static struct ieee80211_channel *
1126findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
1127 uint32_t flags)
1128{
1129 struct ieee80211_channel *c;
1130 int i;
1131
1132 flags &= IEEE80211_CHAN_ALLTURBO;
1133 /* brute force search */
1134 for (i = 0; i < nchans; i++) {
1135 c = &chans[i];
1136 if (c->ic_freq == freq &&
1137 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1138 return c;
1139 }
1140 return NULL;
1141}
1142
1143/*
1144 * Add 40 MHz channel pair into specified channel list.
1145 */
1146int
1147ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
1148 int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
1149{
1150 struct ieee80211_channel *cent, *extc;
1151 uint16_t freq;
1152 int error;
1153
1154 freq = ieee80211_ieee2mhz(ieee, flags);
1155
1156 /*
1157 * Each entry defines an HT40 channel pair; find the
1158 * center channel, then the extension channel above.
1159 */
1160 flags |= IEEE80211_CHAN_HT20;
1161 cent = findchannel(chans, *nchans, freq, flags);
1162 if (cent == NULL)
1163 return (EINVAL);
1164
1165 extc = findchannel(chans, *nchans, freq + 20, flags);
1166 if (extc == NULL)
1167 return (ENOENT);
1168
1169 flags &= ~IEEE80211_CHAN_HT;
1170 error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
1171 maxregpower, flags | IEEE80211_CHAN_HT40U);
1172 if (error != 0)
1173 return (error);
1174
1175 error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
1176 maxregpower, flags | IEEE80211_CHAN_HT40D);
1177
1178 return (error);
1179}
1180
1181/*
1182 * Adds channels into specified channel list (ieee[] array must be sorted).
1183 * Channels are already sorted.
1184 */
1185static int
1186add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
1187 const uint8_t ieee[], int nieee, uint32_t flags[])
1188{
1189 uint16_t freq;
1190 int i, j, error;
1191
1192 for (i = 0; i < nieee; i++) {
1193 freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
1194 for (j = 0; flags[j] != 0; j++) {
1195 if (flags[j] & IEEE80211_CHAN_HT40D)
1196 if (i == 0 || ieee[i] < ieee[0] + 4 ||
1197 freq - 20 !=
1198 ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
1199 continue;
1200 if (flags[j] & IEEE80211_CHAN_HT40U)
1201 if (i == nieee - 1 ||
1202 ieee[i] + 4 > ieee[nieee - 1] ||
1203 freq + 20 !=
1204 ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
1205 continue;
1206
1207 if (j == 0) {
1208 error = addchan(chans, maxchans, nchans,
1209 ieee[i], freq, 0, flags[j]);
1210 } else {
1211 error = copychan_prev(chans, maxchans, nchans,
1212 flags[j]);
1213 }
1214 if (error != 0)
1215 return (error);
1216 }
1217 }
1218
1219 return (0);
1220}
1221
1222int
1223ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
1224 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1225 int ht40)
1226{
1227 uint32_t flags[IEEE80211_MODE_MAX];
1228
1229 getflags_2ghz(bands, flags, ht40);
1230 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1231
1232 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1233}
1234
1235int
1236ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
1237 int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
1238 int ht40)
1239{
1240 uint32_t flags[IEEE80211_MODE_MAX];
1241
1242 getflags_5ghz(bands, flags, ht40);
1243 KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
1244
1245 return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
1246}
1247
1248/*
1249 * Locate a channel given a frequency+flags. We cache
1250 * the previous lookup to optimize switching between two
1251 * channels--as happens with dynamic turbo.
1252 */
1253struct ieee80211_channel *
1254ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
1255{
1256 struct ieee80211_channel *c;
1257
1258 flags &= IEEE80211_CHAN_ALLTURBO;
1259 c = ic->ic_prevchan;
1260 if (c != NULL && c->ic_freq == freq &&
1261 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1262 return c;
1263 /* brute force search */
1264 return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
1265}
1266
1267/*
1268 * Locate a channel given a channel number+flags. We cache
1269 * the previous lookup to optimize switching between two
1270 * channels--as happens with dynamic turbo.
1271 */
1272struct ieee80211_channel *
1273ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
1274{
1275 struct ieee80211_channel *c;
1276 int i;
1277
1278 flags &= IEEE80211_CHAN_ALLTURBO;
1279 c = ic->ic_prevchan;
1280 if (c != NULL && c->ic_ieee == ieee &&
1281 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1282 return c;
1283 /* brute force search */
1284 for (i = 0; i < ic->ic_nchans; i++) {
1285 c = &ic->ic_channels[i];
1286 if (c->ic_ieee == ieee &&
1287 (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
1288 return c;
1289 }
1290 return NULL;
1291}
1292
1293/*
1294 * Lookup a channel suitable for the given rx status.
1295 *
1296 * This is used to find a channel for a frame (eg beacon, probe
1297 * response) based purely on the received PHY information.
1298 *
1299 * For now it tries to do it based on R_FREQ / R_IEEE.
1300 * This is enough for 11bg and 11a (and thus 11ng/11na)
1301 * but it will not be enough for GSM, PSB channels and the
1302 * like. It also doesn't know about legacy-turbog and
1303 * legacy-turbo modes, which some offload NICs actually
1304 * support in weird ways.
1305 *
1306 * Takes the ic and rxstatus; returns the channel or NULL
1307 * if not found.
1308 *
1309 * XXX TODO: Add support for that when the need arises.
1310 */
1311struct ieee80211_channel *
1312ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
1313 const struct ieee80211_rx_stats *rxs)
1314{
1315 struct ieee80211com *ic = vap->iv_ic;
1316 uint32_t flags;
1317 struct ieee80211_channel *c;
1318
1319 if (rxs == NULL)
1320 return (NULL);
1321
1322 /*
1323 * Strictly speaking we only use freq for now,
1324 * however later on we may wish to just store
1325 * the ieee for verification.
1326 */
1327 if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
1328 return (NULL);
1329 if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
1330 return (NULL);
1331
1332 /*
1333 * If the rx status contains a valid ieee/freq, then
1334 * ensure we populate the correct channel information
1335 * in rxchan before passing it up to the scan infrastructure.
1336 * Offload NICs will pass up beacons from all channels
1337 * during background scans.
1338 */
1339
1340 /* Determine a band */
1341 /* XXX should be done by the driver? */
1342 if (rxs->c_freq < 3000) {
1343 flags = IEEE80211_CHAN_G;
1344 } else {
1345 flags = IEEE80211_CHAN_A;
1346 }
1347
1348 /* Channel lookup */
1349 c = ieee80211_find_channel(ic, rxs->c_freq, flags);
1350
1351 IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
1352 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1353 __func__,
1354 (int) rxs->c_freq,
1355 (int) rxs->c_ieee,
1356 flags,
1357 c);
1358
1359 return (c);
1360}
1361
1362static void
1363addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
1364{
1365#define ADD(_ic, _s, _o) \
1366 ifmedia_add(media, \
1367 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1359 static const u_int mopts[IEEE80211_MODE_MAX] = {
1368 static const u_int mopts[IEEE80211_MODE_MAX] = {
1360 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1361 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1362 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1363 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1364 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1365 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1366 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1367 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1368 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1369 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1370 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1371 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1372 };
1373 u_int mopt;
1374
1375 mopt = mopts[mode];
1376 if (addsta)
1377 ADD(ic, mword, mopt); /* STA mode has no cap */
1378 if (caps & IEEE80211_C_IBSS)
1379 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1380 if (caps & IEEE80211_C_HOSTAP)
1381 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1382 if (caps & IEEE80211_C_AHDEMO)
1383 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1384 if (caps & IEEE80211_C_MONITOR)
1385 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1386 if (caps & IEEE80211_C_WDS)
1387 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1388 if (caps & IEEE80211_C_MBSS)
1389 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1390#undef ADD
1391}
1392
1393/*
1394 * Setup the media data structures according to the channel and
1395 * rate tables.
1396 */
1397static int
1398ieee80211_media_setup(struct ieee80211com *ic,
1399 struct ifmedia *media, int caps, int addsta,
1400 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1401{
1402 int i, j, rate, maxrate, mword, r;
1403 enum ieee80211_phymode mode;
1404 const struct ieee80211_rateset *rs;
1405 struct ieee80211_rateset allrates;
1406
1407 /*
1408 * Fill in media characteristics.
1409 */
1410 ifmedia_init(media, 0, media_change, media_stat);
1411 maxrate = 0;
1412 /*
1413 * Add media for legacy operating modes.
1414 */
1415 memset(&allrates, 0, sizeof(allrates));
1416 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1417 if (isclr(ic->ic_modecaps, mode))
1418 continue;
1419 addmedia(media, caps, addsta, mode, IFM_AUTO);
1420 if (mode == IEEE80211_MODE_AUTO)
1421 continue;
1422 rs = &ic->ic_sup_rates[mode];
1423 for (i = 0; i < rs->rs_nrates; i++) {
1424 rate = rs->rs_rates[i];
1425 mword = ieee80211_rate2media(ic, rate, mode);
1426 if (mword == 0)
1427 continue;
1428 addmedia(media, caps, addsta, mode, mword);
1429 /*
1430 * Add legacy rate to the collection of all rates.
1431 */
1432 r = rate & IEEE80211_RATE_VAL;
1433 for (j = 0; j < allrates.rs_nrates; j++)
1434 if (allrates.rs_rates[j] == r)
1435 break;
1436 if (j == allrates.rs_nrates) {
1437 /* unique, add to the set */
1438 allrates.rs_rates[j] = r;
1439 allrates.rs_nrates++;
1440 }
1441 rate = (rate & IEEE80211_RATE_VAL) / 2;
1442 if (rate > maxrate)
1443 maxrate = rate;
1444 }
1445 }
1446 for (i = 0; i < allrates.rs_nrates; i++) {
1447 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1448 IEEE80211_MODE_AUTO);
1449 if (mword == 0)
1450 continue;
1451 /* NB: remove media options from mword */
1452 addmedia(media, caps, addsta,
1453 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1454 }
1455 /*
1456 * Add HT/11n media. Note that we do not have enough
1457 * bits in the media subtype to express the MCS so we
1458 * use a "placeholder" media subtype and any fixed MCS
1459 * must be specified with a different mechanism.
1460 */
1461 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1462 if (isclr(ic->ic_modecaps, mode))
1463 continue;
1464 addmedia(media, caps, addsta, mode, IFM_AUTO);
1465 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1466 }
1467 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1468 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1469 addmedia(media, caps, addsta,
1470 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1471 i = ic->ic_txstream * 8 - 1;
1472 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1473 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1474 rate = ieee80211_htrates[i].ht40_rate_400ns;
1475 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1476 rate = ieee80211_htrates[i].ht40_rate_800ns;
1477 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1478 rate = ieee80211_htrates[i].ht20_rate_400ns;
1479 else
1480 rate = ieee80211_htrates[i].ht20_rate_800ns;
1481 if (rate > maxrate)
1482 maxrate = rate;
1483 }
1484 return maxrate;
1485}
1486
1487/* XXX inline or eliminate? */
1488const struct ieee80211_rateset *
1489ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1490{
1491 /* XXX does this work for 11ng basic rates? */
1492 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1493}
1494
1495void
1496ieee80211_announce(struct ieee80211com *ic)
1497{
1498 int i, rate, mword;
1499 enum ieee80211_phymode mode;
1500 const struct ieee80211_rateset *rs;
1501
1502 /* NB: skip AUTO since it has no rates */
1503 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1504 if (isclr(ic->ic_modecaps, mode))
1505 continue;
1506 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1507 rs = &ic->ic_sup_rates[mode];
1508 for (i = 0; i < rs->rs_nrates; i++) {
1509 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1510 if (mword == 0)
1511 continue;
1512 rate = ieee80211_media2rate(mword);
1513 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1514 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1515 }
1516 printf("\n");
1517 }
1518 ieee80211_ht_announce(ic);
1519}
1520
1521void
1522ieee80211_announce_channels(struct ieee80211com *ic)
1523{
1524 const struct ieee80211_channel *c;
1525 char type;
1526 int i, cw;
1527
1528 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1529 for (i = 0; i < ic->ic_nchans; i++) {
1530 c = &ic->ic_channels[i];
1531 if (IEEE80211_IS_CHAN_ST(c))
1532 type = 'S';
1533 else if (IEEE80211_IS_CHAN_108A(c))
1534 type = 'T';
1535 else if (IEEE80211_IS_CHAN_108G(c))
1536 type = 'G';
1537 else if (IEEE80211_IS_CHAN_HT(c))
1538 type = 'n';
1539 else if (IEEE80211_IS_CHAN_A(c))
1540 type = 'a';
1541 else if (IEEE80211_IS_CHAN_ANYG(c))
1542 type = 'g';
1543 else if (IEEE80211_IS_CHAN_B(c))
1544 type = 'b';
1545 else
1546 type = 'f';
1547 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1548 cw = 40;
1549 else if (IEEE80211_IS_CHAN_HALF(c))
1550 cw = 10;
1551 else if (IEEE80211_IS_CHAN_QUARTER(c))
1552 cw = 5;
1553 else
1554 cw = 20;
1555 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1556 , c->ic_ieee, c->ic_freq, type
1557 , cw
1558 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1559 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1560 , c->ic_maxregpower
1561 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1562 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1563 );
1564 }
1565}
1566
1567static int
1568media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1569{
1570 switch (IFM_MODE(ime->ifm_media)) {
1571 case IFM_IEEE80211_11A:
1572 *mode = IEEE80211_MODE_11A;
1573 break;
1574 case IFM_IEEE80211_11B:
1575 *mode = IEEE80211_MODE_11B;
1576 break;
1577 case IFM_IEEE80211_11G:
1578 *mode = IEEE80211_MODE_11G;
1579 break;
1580 case IFM_IEEE80211_FH:
1581 *mode = IEEE80211_MODE_FH;
1582 break;
1583 case IFM_IEEE80211_11NA:
1584 *mode = IEEE80211_MODE_11NA;
1585 break;
1586 case IFM_IEEE80211_11NG:
1587 *mode = IEEE80211_MODE_11NG;
1588 break;
1589 case IFM_AUTO:
1590 *mode = IEEE80211_MODE_AUTO;
1591 break;
1592 default:
1593 return 0;
1594 }
1595 /*
1596 * Turbo mode is an ``option''.
1597 * XXX does not apply to AUTO
1598 */
1599 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1600 if (*mode == IEEE80211_MODE_11A) {
1601 if (flags & IEEE80211_F_TURBOP)
1602 *mode = IEEE80211_MODE_TURBO_A;
1603 else
1604 *mode = IEEE80211_MODE_STURBO_A;
1605 } else if (*mode == IEEE80211_MODE_11G)
1606 *mode = IEEE80211_MODE_TURBO_G;
1607 else
1608 return 0;
1609 }
1610 /* XXX HT40 +/- */
1611 return 1;
1612}
1613
1614/*
1615 * Handle a media change request on the vap interface.
1616 */
1617int
1618ieee80211_media_change(struct ifnet *ifp)
1619{
1620 struct ieee80211vap *vap = ifp->if_softc;
1621 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1622 uint16_t newmode;
1623
1624 if (!media2mode(ime, vap->iv_flags, &newmode))
1625 return EINVAL;
1626 if (vap->iv_des_mode != newmode) {
1627 vap->iv_des_mode = newmode;
1628 /* XXX kick state machine if up+running */
1629 }
1630 return 0;
1631}
1632
1633/*
1634 * Common code to calculate the media status word
1635 * from the operating mode and channel state.
1636 */
1637static int
1638media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1639{
1640 int status;
1641
1642 status = IFM_IEEE80211;
1643 switch (opmode) {
1644 case IEEE80211_M_STA:
1645 break;
1646 case IEEE80211_M_IBSS:
1647 status |= IFM_IEEE80211_ADHOC;
1648 break;
1649 case IEEE80211_M_HOSTAP:
1650 status |= IFM_IEEE80211_HOSTAP;
1651 break;
1652 case IEEE80211_M_MONITOR:
1653 status |= IFM_IEEE80211_MONITOR;
1654 break;
1655 case IEEE80211_M_AHDEMO:
1656 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1657 break;
1658 case IEEE80211_M_WDS:
1659 status |= IFM_IEEE80211_WDS;
1660 break;
1661 case IEEE80211_M_MBSS:
1662 status |= IFM_IEEE80211_MBSS;
1663 break;
1664 }
1665 if (IEEE80211_IS_CHAN_HTA(chan)) {
1666 status |= IFM_IEEE80211_11NA;
1667 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1668 status |= IFM_IEEE80211_11NG;
1669 } else if (IEEE80211_IS_CHAN_A(chan)) {
1670 status |= IFM_IEEE80211_11A;
1671 } else if (IEEE80211_IS_CHAN_B(chan)) {
1672 status |= IFM_IEEE80211_11B;
1673 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1674 status |= IFM_IEEE80211_11G;
1675 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1676 status |= IFM_IEEE80211_FH;
1677 }
1678 /* XXX else complain? */
1679
1680 if (IEEE80211_IS_CHAN_TURBO(chan))
1681 status |= IFM_IEEE80211_TURBO;
1682#if 0
1683 if (IEEE80211_IS_CHAN_HT20(chan))
1684 status |= IFM_IEEE80211_HT20;
1685 if (IEEE80211_IS_CHAN_HT40(chan))
1686 status |= IFM_IEEE80211_HT40;
1687#endif
1688 return status;
1689}
1690
1691void
1692ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1693{
1694 struct ieee80211vap *vap = ifp->if_softc;
1695 struct ieee80211com *ic = vap->iv_ic;
1696 enum ieee80211_phymode mode;
1697
1698 imr->ifm_status = IFM_AVALID;
1699 /*
1700 * NB: use the current channel's mode to lock down a xmit
1701 * rate only when running; otherwise we may have a mismatch
1702 * in which case the rate will not be convertible.
1703 */
1704 if (vap->iv_state == IEEE80211_S_RUN ||
1705 vap->iv_state == IEEE80211_S_SLEEP) {
1706 imr->ifm_status |= IFM_ACTIVE;
1707 mode = ieee80211_chan2mode(ic->ic_curchan);
1708 } else
1709 mode = IEEE80211_MODE_AUTO;
1710 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1711 /*
1712 * Calculate a current rate if possible.
1713 */
1714 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1715 /*
1716 * A fixed rate is set, report that.
1717 */
1718 imr->ifm_active |= ieee80211_rate2media(ic,
1719 vap->iv_txparms[mode].ucastrate, mode);
1720 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1721 /*
1722 * In station mode report the current transmit rate.
1723 */
1724 imr->ifm_active |= ieee80211_rate2media(ic,
1725 vap->iv_bss->ni_txrate, mode);
1726 } else
1727 imr->ifm_active |= IFM_AUTO;
1728 if (imr->ifm_status & IFM_ACTIVE)
1729 imr->ifm_current = imr->ifm_active;
1730}
1731
1732/*
1733 * Set the current phy mode and recalculate the active channel
1734 * set based on the available channels for this mode. Also
1735 * select a new default/current channel if the current one is
1736 * inappropriate for this mode.
1737 */
1738int
1739ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1740{
1741 /*
1742 * Adjust basic rates in 11b/11g supported rate set.
1743 * Note that if operating on a hal/quarter rate channel
1744 * this is a noop as those rates sets are different
1745 * and used instead.
1746 */
1747 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1748 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1749
1750 ic->ic_curmode = mode;
1751 ieee80211_reset_erp(ic); /* reset ERP state */
1752
1753 return 0;
1754}
1755
1756/*
1757 * Return the phy mode for with the specified channel.
1758 */
1759enum ieee80211_phymode
1760ieee80211_chan2mode(const struct ieee80211_channel *chan)
1761{
1762
1763 if (IEEE80211_IS_CHAN_HTA(chan))
1764 return IEEE80211_MODE_11NA;
1765 else if (IEEE80211_IS_CHAN_HTG(chan))
1766 return IEEE80211_MODE_11NG;
1767 else if (IEEE80211_IS_CHAN_108G(chan))
1768 return IEEE80211_MODE_TURBO_G;
1769 else if (IEEE80211_IS_CHAN_ST(chan))
1770 return IEEE80211_MODE_STURBO_A;
1771 else if (IEEE80211_IS_CHAN_TURBO(chan))
1772 return IEEE80211_MODE_TURBO_A;
1773 else if (IEEE80211_IS_CHAN_HALF(chan))
1774 return IEEE80211_MODE_HALF;
1775 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1776 return IEEE80211_MODE_QUARTER;
1777 else if (IEEE80211_IS_CHAN_A(chan))
1778 return IEEE80211_MODE_11A;
1779 else if (IEEE80211_IS_CHAN_ANYG(chan))
1780 return IEEE80211_MODE_11G;
1781 else if (IEEE80211_IS_CHAN_B(chan))
1782 return IEEE80211_MODE_11B;
1783 else if (IEEE80211_IS_CHAN_FHSS(chan))
1784 return IEEE80211_MODE_FH;
1785
1786 /* NB: should not get here */
1787 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1788 __func__, chan->ic_freq, chan->ic_flags);
1789 return IEEE80211_MODE_11B;
1790}
1791
1792struct ratemedia {
1793 u_int match; /* rate + mode */
1794 u_int media; /* if_media rate */
1795};
1796
1797static int
1798findmedia(const struct ratemedia rates[], int n, u_int match)
1799{
1800 int i;
1801
1802 for (i = 0; i < n; i++)
1803 if (rates[i].match == match)
1804 return rates[i].media;
1805 return IFM_AUTO;
1806}
1807
1808/*
1809 * Convert IEEE80211 rate value to ifmedia subtype.
1810 * Rate is either a legacy rate in units of 0.5Mbps
1811 * or an MCS index.
1812 */
1813int
1814ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1815{
1816 static const struct ratemedia rates[] = {
1817 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1818 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1819 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1820 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1821 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1822 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1823 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1824 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1825 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1826 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1827 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1828 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1829 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1830 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1831 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1832 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1833 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1834 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1835 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1836 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1837 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1838 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1839 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1840 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1841 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1842 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1843 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1844 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1845 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1846 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1847 /* NB: OFDM72 doesn't really exist so we don't handle it */
1848 };
1849 static const struct ratemedia htrates[] = {
1850 { 0, IFM_IEEE80211_MCS },
1851 { 1, IFM_IEEE80211_MCS },
1852 { 2, IFM_IEEE80211_MCS },
1853 { 3, IFM_IEEE80211_MCS },
1854 { 4, IFM_IEEE80211_MCS },
1855 { 5, IFM_IEEE80211_MCS },
1856 { 6, IFM_IEEE80211_MCS },
1857 { 7, IFM_IEEE80211_MCS },
1858 { 8, IFM_IEEE80211_MCS },
1859 { 9, IFM_IEEE80211_MCS },
1860 { 10, IFM_IEEE80211_MCS },
1861 { 11, IFM_IEEE80211_MCS },
1862 { 12, IFM_IEEE80211_MCS },
1863 { 13, IFM_IEEE80211_MCS },
1864 { 14, IFM_IEEE80211_MCS },
1865 { 15, IFM_IEEE80211_MCS },
1866 { 16, IFM_IEEE80211_MCS },
1867 { 17, IFM_IEEE80211_MCS },
1868 { 18, IFM_IEEE80211_MCS },
1869 { 19, IFM_IEEE80211_MCS },
1870 { 20, IFM_IEEE80211_MCS },
1871 { 21, IFM_IEEE80211_MCS },
1872 { 22, IFM_IEEE80211_MCS },
1873 { 23, IFM_IEEE80211_MCS },
1874 { 24, IFM_IEEE80211_MCS },
1875 { 25, IFM_IEEE80211_MCS },
1876 { 26, IFM_IEEE80211_MCS },
1877 { 27, IFM_IEEE80211_MCS },
1878 { 28, IFM_IEEE80211_MCS },
1879 { 29, IFM_IEEE80211_MCS },
1880 { 30, IFM_IEEE80211_MCS },
1881 { 31, IFM_IEEE80211_MCS },
1882 { 32, IFM_IEEE80211_MCS },
1883 { 33, IFM_IEEE80211_MCS },
1884 { 34, IFM_IEEE80211_MCS },
1885 { 35, IFM_IEEE80211_MCS },
1886 { 36, IFM_IEEE80211_MCS },
1887 { 37, IFM_IEEE80211_MCS },
1888 { 38, IFM_IEEE80211_MCS },
1889 { 39, IFM_IEEE80211_MCS },
1890 { 40, IFM_IEEE80211_MCS },
1891 { 41, IFM_IEEE80211_MCS },
1892 { 42, IFM_IEEE80211_MCS },
1893 { 43, IFM_IEEE80211_MCS },
1894 { 44, IFM_IEEE80211_MCS },
1895 { 45, IFM_IEEE80211_MCS },
1896 { 46, IFM_IEEE80211_MCS },
1897 { 47, IFM_IEEE80211_MCS },
1898 { 48, IFM_IEEE80211_MCS },
1899 { 49, IFM_IEEE80211_MCS },
1900 { 50, IFM_IEEE80211_MCS },
1901 { 51, IFM_IEEE80211_MCS },
1902 { 52, IFM_IEEE80211_MCS },
1903 { 53, IFM_IEEE80211_MCS },
1904 { 54, IFM_IEEE80211_MCS },
1905 { 55, IFM_IEEE80211_MCS },
1906 { 56, IFM_IEEE80211_MCS },
1907 { 57, IFM_IEEE80211_MCS },
1908 { 58, IFM_IEEE80211_MCS },
1909 { 59, IFM_IEEE80211_MCS },
1910 { 60, IFM_IEEE80211_MCS },
1911 { 61, IFM_IEEE80211_MCS },
1912 { 62, IFM_IEEE80211_MCS },
1913 { 63, IFM_IEEE80211_MCS },
1914 { 64, IFM_IEEE80211_MCS },
1915 { 65, IFM_IEEE80211_MCS },
1916 { 66, IFM_IEEE80211_MCS },
1917 { 67, IFM_IEEE80211_MCS },
1918 { 68, IFM_IEEE80211_MCS },
1919 { 69, IFM_IEEE80211_MCS },
1920 { 70, IFM_IEEE80211_MCS },
1921 { 71, IFM_IEEE80211_MCS },
1922 { 72, IFM_IEEE80211_MCS },
1923 { 73, IFM_IEEE80211_MCS },
1924 { 74, IFM_IEEE80211_MCS },
1925 { 75, IFM_IEEE80211_MCS },
1926 { 76, IFM_IEEE80211_MCS },
1927 };
1928 int m;
1929
1930 /*
1931 * Check 11n rates first for match as an MCS.
1932 */
1933 if (mode == IEEE80211_MODE_11NA) {
1934 if (rate & IEEE80211_RATE_MCS) {
1935 rate &= ~IEEE80211_RATE_MCS;
1936 m = findmedia(htrates, nitems(htrates), rate);
1937 if (m != IFM_AUTO)
1938 return m | IFM_IEEE80211_11NA;
1939 }
1940 } else if (mode == IEEE80211_MODE_11NG) {
1941 /* NB: 12 is ambiguous, it will be treated as an MCS */
1942 if (rate & IEEE80211_RATE_MCS) {
1943 rate &= ~IEEE80211_RATE_MCS;
1944 m = findmedia(htrates, nitems(htrates), rate);
1945 if (m != IFM_AUTO)
1946 return m | IFM_IEEE80211_11NG;
1947 }
1948 }
1949 rate &= IEEE80211_RATE_VAL;
1950 switch (mode) {
1951 case IEEE80211_MODE_11A:
1952 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1953 case IEEE80211_MODE_QUARTER:
1954 case IEEE80211_MODE_11NA:
1955 case IEEE80211_MODE_TURBO_A:
1956 case IEEE80211_MODE_STURBO_A:
1369 [IEEE80211_MODE_AUTO] = IFM_AUTO,
1370 [IEEE80211_MODE_11A] = IFM_IEEE80211_11A,
1371 [IEEE80211_MODE_11B] = IFM_IEEE80211_11B,
1372 [IEEE80211_MODE_11G] = IFM_IEEE80211_11G,
1373 [IEEE80211_MODE_FH] = IFM_IEEE80211_FH,
1374 [IEEE80211_MODE_TURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1375 [IEEE80211_MODE_TURBO_G] = IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
1376 [IEEE80211_MODE_STURBO_A] = IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
1377 [IEEE80211_MODE_HALF] = IFM_IEEE80211_11A, /* XXX */
1378 [IEEE80211_MODE_QUARTER] = IFM_IEEE80211_11A, /* XXX */
1379 [IEEE80211_MODE_11NA] = IFM_IEEE80211_11NA,
1380 [IEEE80211_MODE_11NG] = IFM_IEEE80211_11NG,
1381 };
1382 u_int mopt;
1383
1384 mopt = mopts[mode];
1385 if (addsta)
1386 ADD(ic, mword, mopt); /* STA mode has no cap */
1387 if (caps & IEEE80211_C_IBSS)
1388 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
1389 if (caps & IEEE80211_C_HOSTAP)
1390 ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
1391 if (caps & IEEE80211_C_AHDEMO)
1392 ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
1393 if (caps & IEEE80211_C_MONITOR)
1394 ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
1395 if (caps & IEEE80211_C_WDS)
1396 ADD(media, mword, mopt | IFM_IEEE80211_WDS);
1397 if (caps & IEEE80211_C_MBSS)
1398 ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
1399#undef ADD
1400}
1401
1402/*
1403 * Setup the media data structures according to the channel and
1404 * rate tables.
1405 */
1406static int
1407ieee80211_media_setup(struct ieee80211com *ic,
1408 struct ifmedia *media, int caps, int addsta,
1409 ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
1410{
1411 int i, j, rate, maxrate, mword, r;
1412 enum ieee80211_phymode mode;
1413 const struct ieee80211_rateset *rs;
1414 struct ieee80211_rateset allrates;
1415
1416 /*
1417 * Fill in media characteristics.
1418 */
1419 ifmedia_init(media, 0, media_change, media_stat);
1420 maxrate = 0;
1421 /*
1422 * Add media for legacy operating modes.
1423 */
1424 memset(&allrates, 0, sizeof(allrates));
1425 for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
1426 if (isclr(ic->ic_modecaps, mode))
1427 continue;
1428 addmedia(media, caps, addsta, mode, IFM_AUTO);
1429 if (mode == IEEE80211_MODE_AUTO)
1430 continue;
1431 rs = &ic->ic_sup_rates[mode];
1432 for (i = 0; i < rs->rs_nrates; i++) {
1433 rate = rs->rs_rates[i];
1434 mword = ieee80211_rate2media(ic, rate, mode);
1435 if (mword == 0)
1436 continue;
1437 addmedia(media, caps, addsta, mode, mword);
1438 /*
1439 * Add legacy rate to the collection of all rates.
1440 */
1441 r = rate & IEEE80211_RATE_VAL;
1442 for (j = 0; j < allrates.rs_nrates; j++)
1443 if (allrates.rs_rates[j] == r)
1444 break;
1445 if (j == allrates.rs_nrates) {
1446 /* unique, add to the set */
1447 allrates.rs_rates[j] = r;
1448 allrates.rs_nrates++;
1449 }
1450 rate = (rate & IEEE80211_RATE_VAL) / 2;
1451 if (rate > maxrate)
1452 maxrate = rate;
1453 }
1454 }
1455 for (i = 0; i < allrates.rs_nrates; i++) {
1456 mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
1457 IEEE80211_MODE_AUTO);
1458 if (mword == 0)
1459 continue;
1460 /* NB: remove media options from mword */
1461 addmedia(media, caps, addsta,
1462 IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
1463 }
1464 /*
1465 * Add HT/11n media. Note that we do not have enough
1466 * bits in the media subtype to express the MCS so we
1467 * use a "placeholder" media subtype and any fixed MCS
1468 * must be specified with a different mechanism.
1469 */
1470 for (; mode <= IEEE80211_MODE_11NG; mode++) {
1471 if (isclr(ic->ic_modecaps, mode))
1472 continue;
1473 addmedia(media, caps, addsta, mode, IFM_AUTO);
1474 addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
1475 }
1476 if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
1477 isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
1478 addmedia(media, caps, addsta,
1479 IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
1480 i = ic->ic_txstream * 8 - 1;
1481 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
1482 (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
1483 rate = ieee80211_htrates[i].ht40_rate_400ns;
1484 else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
1485 rate = ieee80211_htrates[i].ht40_rate_800ns;
1486 else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
1487 rate = ieee80211_htrates[i].ht20_rate_400ns;
1488 else
1489 rate = ieee80211_htrates[i].ht20_rate_800ns;
1490 if (rate > maxrate)
1491 maxrate = rate;
1492 }
1493 return maxrate;
1494}
1495
1496/* XXX inline or eliminate? */
1497const struct ieee80211_rateset *
1498ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
1499{
1500 /* XXX does this work for 11ng basic rates? */
1501 return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
1502}
1503
1504void
1505ieee80211_announce(struct ieee80211com *ic)
1506{
1507 int i, rate, mword;
1508 enum ieee80211_phymode mode;
1509 const struct ieee80211_rateset *rs;
1510
1511 /* NB: skip AUTO since it has no rates */
1512 for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
1513 if (isclr(ic->ic_modecaps, mode))
1514 continue;
1515 ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
1516 rs = &ic->ic_sup_rates[mode];
1517 for (i = 0; i < rs->rs_nrates; i++) {
1518 mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
1519 if (mword == 0)
1520 continue;
1521 rate = ieee80211_media2rate(mword);
1522 printf("%s%d%sMbps", (i != 0 ? " " : ""),
1523 rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
1524 }
1525 printf("\n");
1526 }
1527 ieee80211_ht_announce(ic);
1528}
1529
1530void
1531ieee80211_announce_channels(struct ieee80211com *ic)
1532{
1533 const struct ieee80211_channel *c;
1534 char type;
1535 int i, cw;
1536
1537 printf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1538 for (i = 0; i < ic->ic_nchans; i++) {
1539 c = &ic->ic_channels[i];
1540 if (IEEE80211_IS_CHAN_ST(c))
1541 type = 'S';
1542 else if (IEEE80211_IS_CHAN_108A(c))
1543 type = 'T';
1544 else if (IEEE80211_IS_CHAN_108G(c))
1545 type = 'G';
1546 else if (IEEE80211_IS_CHAN_HT(c))
1547 type = 'n';
1548 else if (IEEE80211_IS_CHAN_A(c))
1549 type = 'a';
1550 else if (IEEE80211_IS_CHAN_ANYG(c))
1551 type = 'g';
1552 else if (IEEE80211_IS_CHAN_B(c))
1553 type = 'b';
1554 else
1555 type = 'f';
1556 if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
1557 cw = 40;
1558 else if (IEEE80211_IS_CHAN_HALF(c))
1559 cw = 10;
1560 else if (IEEE80211_IS_CHAN_QUARTER(c))
1561 cw = 5;
1562 else
1563 cw = 20;
1564 printf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1565 , c->ic_ieee, c->ic_freq, type
1566 , cw
1567 , IEEE80211_IS_CHAN_HT40U(c) ? '+' :
1568 IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
1569 , c->ic_maxregpower
1570 , c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
1571 , c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
1572 );
1573 }
1574}
1575
1576static int
1577media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
1578{
1579 switch (IFM_MODE(ime->ifm_media)) {
1580 case IFM_IEEE80211_11A:
1581 *mode = IEEE80211_MODE_11A;
1582 break;
1583 case IFM_IEEE80211_11B:
1584 *mode = IEEE80211_MODE_11B;
1585 break;
1586 case IFM_IEEE80211_11G:
1587 *mode = IEEE80211_MODE_11G;
1588 break;
1589 case IFM_IEEE80211_FH:
1590 *mode = IEEE80211_MODE_FH;
1591 break;
1592 case IFM_IEEE80211_11NA:
1593 *mode = IEEE80211_MODE_11NA;
1594 break;
1595 case IFM_IEEE80211_11NG:
1596 *mode = IEEE80211_MODE_11NG;
1597 break;
1598 case IFM_AUTO:
1599 *mode = IEEE80211_MODE_AUTO;
1600 break;
1601 default:
1602 return 0;
1603 }
1604 /*
1605 * Turbo mode is an ``option''.
1606 * XXX does not apply to AUTO
1607 */
1608 if (ime->ifm_media & IFM_IEEE80211_TURBO) {
1609 if (*mode == IEEE80211_MODE_11A) {
1610 if (flags & IEEE80211_F_TURBOP)
1611 *mode = IEEE80211_MODE_TURBO_A;
1612 else
1613 *mode = IEEE80211_MODE_STURBO_A;
1614 } else if (*mode == IEEE80211_MODE_11G)
1615 *mode = IEEE80211_MODE_TURBO_G;
1616 else
1617 return 0;
1618 }
1619 /* XXX HT40 +/- */
1620 return 1;
1621}
1622
1623/*
1624 * Handle a media change request on the vap interface.
1625 */
1626int
1627ieee80211_media_change(struct ifnet *ifp)
1628{
1629 struct ieee80211vap *vap = ifp->if_softc;
1630 struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
1631 uint16_t newmode;
1632
1633 if (!media2mode(ime, vap->iv_flags, &newmode))
1634 return EINVAL;
1635 if (vap->iv_des_mode != newmode) {
1636 vap->iv_des_mode = newmode;
1637 /* XXX kick state machine if up+running */
1638 }
1639 return 0;
1640}
1641
1642/*
1643 * Common code to calculate the media status word
1644 * from the operating mode and channel state.
1645 */
1646static int
1647media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
1648{
1649 int status;
1650
1651 status = IFM_IEEE80211;
1652 switch (opmode) {
1653 case IEEE80211_M_STA:
1654 break;
1655 case IEEE80211_M_IBSS:
1656 status |= IFM_IEEE80211_ADHOC;
1657 break;
1658 case IEEE80211_M_HOSTAP:
1659 status |= IFM_IEEE80211_HOSTAP;
1660 break;
1661 case IEEE80211_M_MONITOR:
1662 status |= IFM_IEEE80211_MONITOR;
1663 break;
1664 case IEEE80211_M_AHDEMO:
1665 status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
1666 break;
1667 case IEEE80211_M_WDS:
1668 status |= IFM_IEEE80211_WDS;
1669 break;
1670 case IEEE80211_M_MBSS:
1671 status |= IFM_IEEE80211_MBSS;
1672 break;
1673 }
1674 if (IEEE80211_IS_CHAN_HTA(chan)) {
1675 status |= IFM_IEEE80211_11NA;
1676 } else if (IEEE80211_IS_CHAN_HTG(chan)) {
1677 status |= IFM_IEEE80211_11NG;
1678 } else if (IEEE80211_IS_CHAN_A(chan)) {
1679 status |= IFM_IEEE80211_11A;
1680 } else if (IEEE80211_IS_CHAN_B(chan)) {
1681 status |= IFM_IEEE80211_11B;
1682 } else if (IEEE80211_IS_CHAN_ANYG(chan)) {
1683 status |= IFM_IEEE80211_11G;
1684 } else if (IEEE80211_IS_CHAN_FHSS(chan)) {
1685 status |= IFM_IEEE80211_FH;
1686 }
1687 /* XXX else complain? */
1688
1689 if (IEEE80211_IS_CHAN_TURBO(chan))
1690 status |= IFM_IEEE80211_TURBO;
1691#if 0
1692 if (IEEE80211_IS_CHAN_HT20(chan))
1693 status |= IFM_IEEE80211_HT20;
1694 if (IEEE80211_IS_CHAN_HT40(chan))
1695 status |= IFM_IEEE80211_HT40;
1696#endif
1697 return status;
1698}
1699
1700void
1701ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
1702{
1703 struct ieee80211vap *vap = ifp->if_softc;
1704 struct ieee80211com *ic = vap->iv_ic;
1705 enum ieee80211_phymode mode;
1706
1707 imr->ifm_status = IFM_AVALID;
1708 /*
1709 * NB: use the current channel's mode to lock down a xmit
1710 * rate only when running; otherwise we may have a mismatch
1711 * in which case the rate will not be convertible.
1712 */
1713 if (vap->iv_state == IEEE80211_S_RUN ||
1714 vap->iv_state == IEEE80211_S_SLEEP) {
1715 imr->ifm_status |= IFM_ACTIVE;
1716 mode = ieee80211_chan2mode(ic->ic_curchan);
1717 } else
1718 mode = IEEE80211_MODE_AUTO;
1719 imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
1720 /*
1721 * Calculate a current rate if possible.
1722 */
1723 if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
1724 /*
1725 * A fixed rate is set, report that.
1726 */
1727 imr->ifm_active |= ieee80211_rate2media(ic,
1728 vap->iv_txparms[mode].ucastrate, mode);
1729 } else if (vap->iv_opmode == IEEE80211_M_STA) {
1730 /*
1731 * In station mode report the current transmit rate.
1732 */
1733 imr->ifm_active |= ieee80211_rate2media(ic,
1734 vap->iv_bss->ni_txrate, mode);
1735 } else
1736 imr->ifm_active |= IFM_AUTO;
1737 if (imr->ifm_status & IFM_ACTIVE)
1738 imr->ifm_current = imr->ifm_active;
1739}
1740
1741/*
1742 * Set the current phy mode and recalculate the active channel
1743 * set based on the available channels for this mode. Also
1744 * select a new default/current channel if the current one is
1745 * inappropriate for this mode.
1746 */
1747int
1748ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
1749{
1750 /*
1751 * Adjust basic rates in 11b/11g supported rate set.
1752 * Note that if operating on a hal/quarter rate channel
1753 * this is a noop as those rates sets are different
1754 * and used instead.
1755 */
1756 if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
1757 ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
1758
1759 ic->ic_curmode = mode;
1760 ieee80211_reset_erp(ic); /* reset ERP state */
1761
1762 return 0;
1763}
1764
1765/*
1766 * Return the phy mode for with the specified channel.
1767 */
1768enum ieee80211_phymode
1769ieee80211_chan2mode(const struct ieee80211_channel *chan)
1770{
1771
1772 if (IEEE80211_IS_CHAN_HTA(chan))
1773 return IEEE80211_MODE_11NA;
1774 else if (IEEE80211_IS_CHAN_HTG(chan))
1775 return IEEE80211_MODE_11NG;
1776 else if (IEEE80211_IS_CHAN_108G(chan))
1777 return IEEE80211_MODE_TURBO_G;
1778 else if (IEEE80211_IS_CHAN_ST(chan))
1779 return IEEE80211_MODE_STURBO_A;
1780 else if (IEEE80211_IS_CHAN_TURBO(chan))
1781 return IEEE80211_MODE_TURBO_A;
1782 else if (IEEE80211_IS_CHAN_HALF(chan))
1783 return IEEE80211_MODE_HALF;
1784 else if (IEEE80211_IS_CHAN_QUARTER(chan))
1785 return IEEE80211_MODE_QUARTER;
1786 else if (IEEE80211_IS_CHAN_A(chan))
1787 return IEEE80211_MODE_11A;
1788 else if (IEEE80211_IS_CHAN_ANYG(chan))
1789 return IEEE80211_MODE_11G;
1790 else if (IEEE80211_IS_CHAN_B(chan))
1791 return IEEE80211_MODE_11B;
1792 else if (IEEE80211_IS_CHAN_FHSS(chan))
1793 return IEEE80211_MODE_FH;
1794
1795 /* NB: should not get here */
1796 printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1797 __func__, chan->ic_freq, chan->ic_flags);
1798 return IEEE80211_MODE_11B;
1799}
1800
1801struct ratemedia {
1802 u_int match; /* rate + mode */
1803 u_int media; /* if_media rate */
1804};
1805
1806static int
1807findmedia(const struct ratemedia rates[], int n, u_int match)
1808{
1809 int i;
1810
1811 for (i = 0; i < n; i++)
1812 if (rates[i].match == match)
1813 return rates[i].media;
1814 return IFM_AUTO;
1815}
1816
1817/*
1818 * Convert IEEE80211 rate value to ifmedia subtype.
1819 * Rate is either a legacy rate in units of 0.5Mbps
1820 * or an MCS index.
1821 */
1822int
1823ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
1824{
1825 static const struct ratemedia rates[] = {
1826 { 2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
1827 { 4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
1828 { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
1829 { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
1830 { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
1831 { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
1832 { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
1833 { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
1834 { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
1835 { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
1836 { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
1837 { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
1838 { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
1839 { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
1840 { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
1841 { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
1842 { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
1843 { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
1844 { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
1845 { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
1846 { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
1847 { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
1848 { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
1849 { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
1850 { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
1851 { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
1852 { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
1853 { 6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
1854 { 9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
1855 { 54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
1856 /* NB: OFDM72 doesn't really exist so we don't handle it */
1857 };
1858 static const struct ratemedia htrates[] = {
1859 { 0, IFM_IEEE80211_MCS },
1860 { 1, IFM_IEEE80211_MCS },
1861 { 2, IFM_IEEE80211_MCS },
1862 { 3, IFM_IEEE80211_MCS },
1863 { 4, IFM_IEEE80211_MCS },
1864 { 5, IFM_IEEE80211_MCS },
1865 { 6, IFM_IEEE80211_MCS },
1866 { 7, IFM_IEEE80211_MCS },
1867 { 8, IFM_IEEE80211_MCS },
1868 { 9, IFM_IEEE80211_MCS },
1869 { 10, IFM_IEEE80211_MCS },
1870 { 11, IFM_IEEE80211_MCS },
1871 { 12, IFM_IEEE80211_MCS },
1872 { 13, IFM_IEEE80211_MCS },
1873 { 14, IFM_IEEE80211_MCS },
1874 { 15, IFM_IEEE80211_MCS },
1875 { 16, IFM_IEEE80211_MCS },
1876 { 17, IFM_IEEE80211_MCS },
1877 { 18, IFM_IEEE80211_MCS },
1878 { 19, IFM_IEEE80211_MCS },
1879 { 20, IFM_IEEE80211_MCS },
1880 { 21, IFM_IEEE80211_MCS },
1881 { 22, IFM_IEEE80211_MCS },
1882 { 23, IFM_IEEE80211_MCS },
1883 { 24, IFM_IEEE80211_MCS },
1884 { 25, IFM_IEEE80211_MCS },
1885 { 26, IFM_IEEE80211_MCS },
1886 { 27, IFM_IEEE80211_MCS },
1887 { 28, IFM_IEEE80211_MCS },
1888 { 29, IFM_IEEE80211_MCS },
1889 { 30, IFM_IEEE80211_MCS },
1890 { 31, IFM_IEEE80211_MCS },
1891 { 32, IFM_IEEE80211_MCS },
1892 { 33, IFM_IEEE80211_MCS },
1893 { 34, IFM_IEEE80211_MCS },
1894 { 35, IFM_IEEE80211_MCS },
1895 { 36, IFM_IEEE80211_MCS },
1896 { 37, IFM_IEEE80211_MCS },
1897 { 38, IFM_IEEE80211_MCS },
1898 { 39, IFM_IEEE80211_MCS },
1899 { 40, IFM_IEEE80211_MCS },
1900 { 41, IFM_IEEE80211_MCS },
1901 { 42, IFM_IEEE80211_MCS },
1902 { 43, IFM_IEEE80211_MCS },
1903 { 44, IFM_IEEE80211_MCS },
1904 { 45, IFM_IEEE80211_MCS },
1905 { 46, IFM_IEEE80211_MCS },
1906 { 47, IFM_IEEE80211_MCS },
1907 { 48, IFM_IEEE80211_MCS },
1908 { 49, IFM_IEEE80211_MCS },
1909 { 50, IFM_IEEE80211_MCS },
1910 { 51, IFM_IEEE80211_MCS },
1911 { 52, IFM_IEEE80211_MCS },
1912 { 53, IFM_IEEE80211_MCS },
1913 { 54, IFM_IEEE80211_MCS },
1914 { 55, IFM_IEEE80211_MCS },
1915 { 56, IFM_IEEE80211_MCS },
1916 { 57, IFM_IEEE80211_MCS },
1917 { 58, IFM_IEEE80211_MCS },
1918 { 59, IFM_IEEE80211_MCS },
1919 { 60, IFM_IEEE80211_MCS },
1920 { 61, IFM_IEEE80211_MCS },
1921 { 62, IFM_IEEE80211_MCS },
1922 { 63, IFM_IEEE80211_MCS },
1923 { 64, IFM_IEEE80211_MCS },
1924 { 65, IFM_IEEE80211_MCS },
1925 { 66, IFM_IEEE80211_MCS },
1926 { 67, IFM_IEEE80211_MCS },
1927 { 68, IFM_IEEE80211_MCS },
1928 { 69, IFM_IEEE80211_MCS },
1929 { 70, IFM_IEEE80211_MCS },
1930 { 71, IFM_IEEE80211_MCS },
1931 { 72, IFM_IEEE80211_MCS },
1932 { 73, IFM_IEEE80211_MCS },
1933 { 74, IFM_IEEE80211_MCS },
1934 { 75, IFM_IEEE80211_MCS },
1935 { 76, IFM_IEEE80211_MCS },
1936 };
1937 int m;
1938
1939 /*
1940 * Check 11n rates first for match as an MCS.
1941 */
1942 if (mode == IEEE80211_MODE_11NA) {
1943 if (rate & IEEE80211_RATE_MCS) {
1944 rate &= ~IEEE80211_RATE_MCS;
1945 m = findmedia(htrates, nitems(htrates), rate);
1946 if (m != IFM_AUTO)
1947 return m | IFM_IEEE80211_11NA;
1948 }
1949 } else if (mode == IEEE80211_MODE_11NG) {
1950 /* NB: 12 is ambiguous, it will be treated as an MCS */
1951 if (rate & IEEE80211_RATE_MCS) {
1952 rate &= ~IEEE80211_RATE_MCS;
1953 m = findmedia(htrates, nitems(htrates), rate);
1954 if (m != IFM_AUTO)
1955 return m | IFM_IEEE80211_11NG;
1956 }
1957 }
1958 rate &= IEEE80211_RATE_VAL;
1959 switch (mode) {
1960 case IEEE80211_MODE_11A:
1961 case IEEE80211_MODE_HALF: /* XXX good 'nuf */
1962 case IEEE80211_MODE_QUARTER:
1963 case IEEE80211_MODE_11NA:
1964 case IEEE80211_MODE_TURBO_A:
1965 case IEEE80211_MODE_STURBO_A:
1957 return findmedia(rates, nitems(rates),
1966 return findmedia(rates, nitems(rates),
1958 rate | IFM_IEEE80211_11A);
1959 case IEEE80211_MODE_11B:
1967 rate | IFM_IEEE80211_11A);
1968 case IEEE80211_MODE_11B:
1960 return findmedia(rates, nitems(rates),
1969 return findmedia(rates, nitems(rates),
1961 rate | IFM_IEEE80211_11B);
1962 case IEEE80211_MODE_FH:
1970 rate | IFM_IEEE80211_11B);
1971 case IEEE80211_MODE_FH:
1963 return findmedia(rates, nitems(rates),
1972 return findmedia(rates, nitems(rates),
1964 rate | IFM_IEEE80211_FH);
1965 case IEEE80211_MODE_AUTO:
1966 /* NB: ic may be NULL for some drivers */
1967 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1968 return findmedia(rates, nitems(rates),
1969 rate | IFM_IEEE80211_FH);
1970 /* NB: hack, 11g matches both 11b+11a rates */
1971 /* fall thru... */
1972 case IEEE80211_MODE_11G:
1973 case IEEE80211_MODE_11NG:
1974 case IEEE80211_MODE_TURBO_G:
1975 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1976 }
1977 return IFM_AUTO;
1978}
1979
1980int
1981ieee80211_media2rate(int mword)
1982{
1983 static const int ieeerates[] = {
1984 -1, /* IFM_AUTO */
1985 0, /* IFM_MANUAL */
1986 0, /* IFM_NONE */
1987 2, /* IFM_IEEE80211_FH1 */
1988 4, /* IFM_IEEE80211_FH2 */
1989 2, /* IFM_IEEE80211_DS1 */
1990 4, /* IFM_IEEE80211_DS2 */
1991 11, /* IFM_IEEE80211_DS5 */
1992 22, /* IFM_IEEE80211_DS11 */
1993 44, /* IFM_IEEE80211_DS22 */
1994 12, /* IFM_IEEE80211_OFDM6 */
1995 18, /* IFM_IEEE80211_OFDM9 */
1996 24, /* IFM_IEEE80211_OFDM12 */
1997 36, /* IFM_IEEE80211_OFDM18 */
1998 48, /* IFM_IEEE80211_OFDM24 */
1999 72, /* IFM_IEEE80211_OFDM36 */
2000 96, /* IFM_IEEE80211_OFDM48 */
2001 108, /* IFM_IEEE80211_OFDM54 */
2002 144, /* IFM_IEEE80211_OFDM72 */
2003 0, /* IFM_IEEE80211_DS354k */
2004 0, /* IFM_IEEE80211_DS512k */
2005 6, /* IFM_IEEE80211_OFDM3 */
2006 9, /* IFM_IEEE80211_OFDM4 */
2007 54, /* IFM_IEEE80211_OFDM27 */
2008 -1, /* IFM_IEEE80211_MCS */
2009 };
2010 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2011 ieeerates[IFM_SUBTYPE(mword)] : 0;
2012}
2013
2014/*
2015 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2016 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2017 */
2018#define mix(a, b, c) \
2019do { \
2020 a -= b; a -= c; a ^= (c >> 13); \
2021 b -= c; b -= a; b ^= (a << 8); \
2022 c -= a; c -= b; c ^= (b >> 13); \
2023 a -= b; a -= c; a ^= (c >> 12); \
2024 b -= c; b -= a; b ^= (a << 16); \
2025 c -= a; c -= b; c ^= (b >> 5); \
2026 a -= b; a -= c; a ^= (c >> 3); \
2027 b -= c; b -= a; b ^= (a << 10); \
2028 c -= a; c -= b; c ^= (b >> 15); \
2029} while (/*CONSTCOND*/0)
2030
2031uint32_t
2032ieee80211_mac_hash(const struct ieee80211com *ic,
2033 const uint8_t addr[IEEE80211_ADDR_LEN])
2034{
2035 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2036
2037 b += addr[5] << 8;
2038 b += addr[4];
2039 a += addr[3] << 24;
2040 a += addr[2] << 16;
2041 a += addr[1] << 8;
2042 a += addr[0];
2043
2044 mix(a, b, c);
2045
2046 return c;
2047}
2048#undef mix
2049
2050char
2051ieee80211_channel_type_char(const struct ieee80211_channel *c)
2052{
2053 if (IEEE80211_IS_CHAN_ST(c))
2054 return 'S';
2055 if (IEEE80211_IS_CHAN_108A(c))
2056 return 'T';
2057 if (IEEE80211_IS_CHAN_108G(c))
2058 return 'G';
2059 if (IEEE80211_IS_CHAN_HT(c))
2060 return 'n';
2061 if (IEEE80211_IS_CHAN_A(c))
2062 return 'a';
2063 if (IEEE80211_IS_CHAN_ANYG(c))
2064 return 'g';
2065 if (IEEE80211_IS_CHAN_B(c))
2066 return 'b';
2067 return 'f';
2068}
1973 rate | IFM_IEEE80211_FH);
1974 case IEEE80211_MODE_AUTO:
1975 /* NB: ic may be NULL for some drivers */
1976 if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
1977 return findmedia(rates, nitems(rates),
1978 rate | IFM_IEEE80211_FH);
1979 /* NB: hack, 11g matches both 11b+11a rates */
1980 /* fall thru... */
1981 case IEEE80211_MODE_11G:
1982 case IEEE80211_MODE_11NG:
1983 case IEEE80211_MODE_TURBO_G:
1984 return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
1985 }
1986 return IFM_AUTO;
1987}
1988
1989int
1990ieee80211_media2rate(int mword)
1991{
1992 static const int ieeerates[] = {
1993 -1, /* IFM_AUTO */
1994 0, /* IFM_MANUAL */
1995 0, /* IFM_NONE */
1996 2, /* IFM_IEEE80211_FH1 */
1997 4, /* IFM_IEEE80211_FH2 */
1998 2, /* IFM_IEEE80211_DS1 */
1999 4, /* IFM_IEEE80211_DS2 */
2000 11, /* IFM_IEEE80211_DS5 */
2001 22, /* IFM_IEEE80211_DS11 */
2002 44, /* IFM_IEEE80211_DS22 */
2003 12, /* IFM_IEEE80211_OFDM6 */
2004 18, /* IFM_IEEE80211_OFDM9 */
2005 24, /* IFM_IEEE80211_OFDM12 */
2006 36, /* IFM_IEEE80211_OFDM18 */
2007 48, /* IFM_IEEE80211_OFDM24 */
2008 72, /* IFM_IEEE80211_OFDM36 */
2009 96, /* IFM_IEEE80211_OFDM48 */
2010 108, /* IFM_IEEE80211_OFDM54 */
2011 144, /* IFM_IEEE80211_OFDM72 */
2012 0, /* IFM_IEEE80211_DS354k */
2013 0, /* IFM_IEEE80211_DS512k */
2014 6, /* IFM_IEEE80211_OFDM3 */
2015 9, /* IFM_IEEE80211_OFDM4 */
2016 54, /* IFM_IEEE80211_OFDM27 */
2017 -1, /* IFM_IEEE80211_MCS */
2018 };
2019 return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
2020 ieeerates[IFM_SUBTYPE(mword)] : 0;
2021}
2022
2023/*
2024 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2025 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2026 */
2027#define mix(a, b, c) \
2028do { \
2029 a -= b; a -= c; a ^= (c >> 13); \
2030 b -= c; b -= a; b ^= (a << 8); \
2031 c -= a; c -= b; c ^= (b >> 13); \
2032 a -= b; a -= c; a ^= (c >> 12); \
2033 b -= c; b -= a; b ^= (a << 16); \
2034 c -= a; c -= b; c ^= (b >> 5); \
2035 a -= b; a -= c; a ^= (c >> 3); \
2036 b -= c; b -= a; b ^= (a << 10); \
2037 c -= a; c -= b; c ^= (b >> 15); \
2038} while (/*CONSTCOND*/0)
2039
2040uint32_t
2041ieee80211_mac_hash(const struct ieee80211com *ic,
2042 const uint8_t addr[IEEE80211_ADDR_LEN])
2043{
2044 uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
2045
2046 b += addr[5] << 8;
2047 b += addr[4];
2048 a += addr[3] << 24;
2049 a += addr[2] << 16;
2050 a += addr[1] << 8;
2051 a += addr[0];
2052
2053 mix(a, b, c);
2054
2055 return c;
2056}
2057#undef mix
2058
2059char
2060ieee80211_channel_type_char(const struct ieee80211_channel *c)
2061{
2062 if (IEEE80211_IS_CHAN_ST(c))
2063 return 'S';
2064 if (IEEE80211_IS_CHAN_108A(c))
2065 return 'T';
2066 if (IEEE80211_IS_CHAN_108G(c))
2067 return 'G';
2068 if (IEEE80211_IS_CHAN_HT(c))
2069 return 'n';
2070 if (IEEE80211_IS_CHAN_A(c))
2071 return 'a';
2072 if (IEEE80211_IS_CHAN_ANYG(c))
2073 return 'g';
2074 if (IEEE80211_IS_CHAN_B(c))
2075 return 'b';
2076 return 'f';
2077}