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