2 * Copyright (c) 2001 Atsushi Onoe
3 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * IEEE 802.11 generic handler
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/socket.h>
42 #include <machine/stdarg.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>
51 #include <netproto/802_11/ieee80211_var.h>
52 #include <netproto/802_11/ieee80211_regdomain.h>
53 #ifdef IEEE80211_SUPPORT_SUPERG
54 #include <netproto/802_11/ieee80211_superg.h>
56 #include <netproto/802_11/ieee80211_ratectl.h>
60 const 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 [IEEE80211_MODE_VHT_2GHZ
] = "11acg",
74 [IEEE80211_MODE_VHT_5GHZ
] = "11ac",
76 /* map ieee80211_opmode to the corresponding capability bit */
77 const int ieee80211_opcap
[IEEE80211_OPMODE_MAX
] = {
78 [IEEE80211_M_IBSS
] = IEEE80211_C_IBSS
,
79 [IEEE80211_M_WDS
] = IEEE80211_C_WDS
,
80 [IEEE80211_M_STA
] = IEEE80211_C_STA
,
81 [IEEE80211_M_AHDEMO
] = IEEE80211_C_AHDEMO
,
82 [IEEE80211_M_HOSTAP
] = IEEE80211_C_HOSTAP
,
83 [IEEE80211_M_MONITOR
] = IEEE80211_C_MONITOR
,
84 #ifdef IEEE80211_SUPPORT_MESH
85 [IEEE80211_M_MBSS
] = IEEE80211_C_MBSS
,
89 const uint8_t ieee80211broadcastaddr
[IEEE80211_ADDR_LEN
] =
90 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
92 static void ieee80211_syncflag_locked(struct ieee80211com
*ic
, int flag
);
93 static void ieee80211_syncflag_ht_locked(struct ieee80211com
*ic
, int flag
);
94 static void ieee80211_syncflag_ext_locked(struct ieee80211com
*ic
, int flag
);
95 static int ieee80211_media_setup(struct ieee80211com
*ic
,
96 struct ifmedia
*media
, int caps
, int addsta
,
97 ifm_change_cb_t media_change
, ifm_stat_cb_t media_stat
);
98 static int media_status(enum ieee80211_opmode
,
99 const struct ieee80211_channel
*);
100 #if defined(__DragonFly__)
102 static uint64_t ieee80211_get_counter(struct ifnet
*, ift_counter
);
105 MALLOC_DEFINE(M_80211_VAP
, "80211vap", "802.11 vap state");
108 * Default supported rates for 802.11 operation (in IEEE .5Mb units).
110 #define B(r) ((r) | IEEE80211_RATE_BASIC)
111 static const struct ieee80211_rateset ieee80211_rateset_11a
=
112 { 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
113 static const struct ieee80211_rateset ieee80211_rateset_half
=
114 { 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
115 static const struct ieee80211_rateset ieee80211_rateset_quarter
=
116 { 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
117 static const struct ieee80211_rateset ieee80211_rateset_11b
=
118 { 4, { B(2), B(4), B(11), B(22) } };
119 /* NB: OFDM rates are handled specially based on mode */
120 static const struct ieee80211_rateset ieee80211_rateset_11g
=
121 { 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
125 * Fill in 802.11 available channel set, mark
126 * all available channels as active, and pick
127 * a default channel if not already specified.
130 ieee80211_chan_init(struct ieee80211com
*ic
)
132 #define DEFAULTRATES(m, def) do { \
133 if (ic->ic_sup_rates[m].rs_nrates == 0) \
134 ic->ic_sup_rates[m] = def; \
136 struct ieee80211_channel
*c
;
139 KASSERT(0 < ic
->ic_nchans
&& ic
->ic_nchans
<= IEEE80211_CHAN_MAX
,
140 ("invalid number of channels specified: %u", ic
->ic_nchans
));
141 memset(ic
->ic_chan_avail
, 0, sizeof(ic
->ic_chan_avail
));
142 memset(ic
->ic_modecaps
, 0, sizeof(ic
->ic_modecaps
));
143 setbit(ic
->ic_modecaps
, IEEE80211_MODE_AUTO
);
144 for (i
= 0; i
< ic
->ic_nchans
; i
++) {
145 c
= &ic
->ic_channels
[i
];
146 KASSERT(c
->ic_flags
!= 0, ("channel with no flags"));
148 * Help drivers that work only with frequencies by filling
149 * in IEEE channel #'s if not already calculated. Note this
150 * mimics similar work done in ieee80211_setregdomain when
151 * changing regulatory state.
154 c
->ic_ieee
= ieee80211_mhz2ieee(c
->ic_freq
,c
->ic_flags
);
155 if (IEEE80211_IS_CHAN_HT40(c
) && c
->ic_extieee
== 0)
156 c
->ic_extieee
= ieee80211_mhz2ieee(c
->ic_freq
+
157 (IEEE80211_IS_CHAN_HT40U(c
) ? 20 : -20),
159 /* default max tx power to max regulatory */
160 if (c
->ic_maxpower
== 0)
161 c
->ic_maxpower
= 2*c
->ic_maxregpower
;
162 setbit(ic
->ic_chan_avail
, c
->ic_ieee
);
164 * Identify mode capabilities.
166 if (IEEE80211_IS_CHAN_A(c
))
167 setbit(ic
->ic_modecaps
, IEEE80211_MODE_11A
);
168 if (IEEE80211_IS_CHAN_B(c
))
169 setbit(ic
->ic_modecaps
, IEEE80211_MODE_11B
);
170 if (IEEE80211_IS_CHAN_ANYG(c
))
171 setbit(ic
->ic_modecaps
, IEEE80211_MODE_11G
);
172 if (IEEE80211_IS_CHAN_FHSS(c
))
173 setbit(ic
->ic_modecaps
, IEEE80211_MODE_FH
);
174 if (IEEE80211_IS_CHAN_108A(c
))
175 setbit(ic
->ic_modecaps
, IEEE80211_MODE_TURBO_A
);
176 if (IEEE80211_IS_CHAN_108G(c
))
177 setbit(ic
->ic_modecaps
, IEEE80211_MODE_TURBO_G
);
178 if (IEEE80211_IS_CHAN_ST(c
))
179 setbit(ic
->ic_modecaps
, IEEE80211_MODE_STURBO_A
);
180 if (IEEE80211_IS_CHAN_HALF(c
))
181 setbit(ic
->ic_modecaps
, IEEE80211_MODE_HALF
);
182 if (IEEE80211_IS_CHAN_QUARTER(c
))
183 setbit(ic
->ic_modecaps
, IEEE80211_MODE_QUARTER
);
184 if (IEEE80211_IS_CHAN_HTA(c
))
185 setbit(ic
->ic_modecaps
, IEEE80211_MODE_11NA
);
186 if (IEEE80211_IS_CHAN_HTG(c
))
187 setbit(ic
->ic_modecaps
, IEEE80211_MODE_11NG
);
189 /* initialize candidate channels to all available */
190 memcpy(ic
->ic_chan_active
, ic
->ic_chan_avail
,
191 sizeof(ic
->ic_chan_avail
));
193 /* sort channel table to allow lookup optimizations */
194 ieee80211_sort_channels(ic
->ic_channels
, ic
->ic_nchans
);
196 /* invalidate any previous state */
197 ic
->ic_bsschan
= IEEE80211_CHAN_ANYC
;
198 ic
->ic_prevchan
= NULL
;
199 ic
->ic_csa_newchan
= NULL
;
200 /* arbitrarily pick the first channel */
201 ic
->ic_curchan
= &ic
->ic_channels
[0];
202 ic
->ic_rt
= ieee80211_get_ratetable(ic
->ic_curchan
);
204 /* fillin well-known rate sets if driver has not specified */
205 DEFAULTRATES(IEEE80211_MODE_11B
, ieee80211_rateset_11b
);
206 DEFAULTRATES(IEEE80211_MODE_11G
, ieee80211_rateset_11g
);
207 DEFAULTRATES(IEEE80211_MODE_11A
, ieee80211_rateset_11a
);
208 DEFAULTRATES(IEEE80211_MODE_TURBO_A
, ieee80211_rateset_11a
);
209 DEFAULTRATES(IEEE80211_MODE_TURBO_G
, ieee80211_rateset_11g
);
210 DEFAULTRATES(IEEE80211_MODE_STURBO_A
, ieee80211_rateset_11a
);
211 DEFAULTRATES(IEEE80211_MODE_HALF
, ieee80211_rateset_half
);
212 DEFAULTRATES(IEEE80211_MODE_QUARTER
, ieee80211_rateset_quarter
);
213 DEFAULTRATES(IEEE80211_MODE_11NA
, ieee80211_rateset_11a
);
214 DEFAULTRATES(IEEE80211_MODE_11NG
, ieee80211_rateset_11g
);
217 * Setup required information to fill the mcsset field, if driver did
218 * not. Assume a 2T2R setup for historic reasons.
220 if (ic
->ic_rxstream
== 0)
222 if (ic
->ic_txstream
== 0)
226 * Set auto mode to reset active channel state and any desired channel.
228 (void) ieee80211_setmode(ic
, IEEE80211_MODE_AUTO
);
233 null_update_mcast(struct ieee80211com
*ic
)
236 ic_printf(ic
, "need multicast update callback\n");
240 null_update_promisc(struct ieee80211com
*ic
)
243 ic_printf(ic
, "need promiscuous mode update callback\n");
247 null_update_chw(struct ieee80211com
*ic
)
250 ic_printf(ic
, "%s: need callback\n", __func__
);
254 ic_printf(struct ieee80211com
*ic
, const char * fmt
, ...)
256 #if defined(__DragonFly__)
260 retval
= kprintf("%s: ", ic
->ic_name
);
261 osdep_va_start(ap
, fmt
);
262 retval
+= kvprintf(fmt
, ap
);
268 retval
= printf("%s: ", ic
->ic_name
);
270 retval
+= vprintf(fmt
, ap
);
276 static LIST_HEAD(, ieee80211com
) ic_head
= LIST_HEAD_INITIALIZER(ic_head
);
277 #if defined(__DragonFly__)
278 static struct lock ic_list_lock
=
279 LOCK_INITIALIZER("80211list", 0, LK_CANRECURSE
);
281 static struct mtx ic_list_mtx
;
282 MTX_SYSINIT(ic_list
, &ic_list_mtx
, "ieee80211com list", MTX_DEF
);
286 sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS
)
288 struct ieee80211com
*ic
;
293 #if defined(__DragonFly__)
295 error
= sysctl_wire_old_buffer(req
, 0);
299 sbuf_new_for_sysctl(&sb
, NULL
, 8, req
);
300 #if defined(__DragonFly__)
302 sbuf_clear_flags(&sb
, SBUF_INCLUDENUL
);
305 #if defined(__DragonFly__)
306 lockmgr(&ic_list_lock
, LK_EXCLUSIVE
);
308 mtx_lock(&ic_list_mtx
);
310 LIST_FOREACH(ic
, &ic_head
, ic_next
) {
311 sbuf_printf(&sb
, "%s%s", sp
, ic
->ic_name
);
314 #if defined(__DragonFly__)
315 lockmgr(&ic_list_lock
, LK_RELEASE
);
317 mtx_unlock(&ic_list_mtx
);
319 error
= sbuf_finish(&sb
);
324 #if defined(__DragonFly__)
325 SYSCTL_PROC(_net_wlan
, OID_AUTO
, devices
,
326 CTLTYPE_STRING
| CTLFLAG_RD
, NULL
, 0,
327 sysctl_ieee80211coms
, "A", "names of available 802.11 devices");
329 SYSCTL_PROC(_net_wlan
, OID_AUTO
, devices
,
330 CTLTYPE_STRING
| CTLFLAG_RD
| CTLFLAG_MPSAFE
, NULL
, 0,
331 sysctl_ieee80211coms
, "A", "names of available 802.11 devices");
336 * Attach/setup the common net80211 state. Called by
337 * the driver on attach to prior to creating any vap's.
340 ieee80211_ifattach(struct ieee80211com
*ic
)
343 IEEE80211_LOCK_INIT(ic
, ic
->ic_name
);
344 IEEE80211_TX_LOCK_INIT(ic
, ic
->ic_name
);
345 TAILQ_INIT(&ic
->ic_vaps
);
347 /* Create a taskqueue for all state changes */
348 ic
->ic_tq
= taskqueue_create("ic_taskq", M_WAITOK
| M_ZERO
,
349 taskqueue_thread_enqueue
, &ic
->ic_tq
);
350 #if defined(__DragonFly__)
351 taskqueue_start_threads(&ic
->ic_tq
, 1, TDPRI_KERN_DAEMON
, -1,
352 "%s net80211 taskq", ic
->ic_name
);
354 taskqueue_start_threads(&ic
->ic_tq
, 1, PI_NET
, "%s net80211 taskq",
356 ic
->ic_ierrors
= counter_u64_alloc(M_WAITOK
);
357 ic
->ic_oerrors
= counter_u64_alloc(M_WAITOK
);
360 * Fill in 802.11 available channel set, mark all
361 * available channels as active, and pick a default
362 * channel if not already specified.
364 ieee80211_chan_init(ic
);
366 ic
->ic_update_mcast
= null_update_mcast
;
367 ic
->ic_update_promisc
= null_update_promisc
;
368 ic
->ic_update_chw
= null_update_chw
;
370 ic
->ic_hash_key
= arc4random();
371 ic
->ic_bintval
= IEEE80211_BINTVAL_DEFAULT
;
372 ic
->ic_lintval
= ic
->ic_bintval
;
373 ic
->ic_txpowlimit
= IEEE80211_TXPOWER_MAX
;
375 ieee80211_crypto_attach(ic
);
376 ieee80211_node_attach(ic
);
377 ieee80211_power_attach(ic
);
378 ieee80211_proto_attach(ic
);
379 #ifdef IEEE80211_SUPPORT_SUPERG
380 ieee80211_superg_attach(ic
);
382 ieee80211_ht_attach(ic
);
383 ieee80211_scan_attach(ic
);
384 ieee80211_regdomain_attach(ic
);
385 ieee80211_dfs_attach(ic
);
387 ieee80211_sysctl_attach(ic
);
389 #if defined(__DragonFly__)
390 lockmgr(&ic_list_lock
, LK_EXCLUSIVE
);
392 mtx_lock(&ic_list_mtx
);
394 LIST_INSERT_HEAD(&ic_head
, ic
, ic_next
);
395 #if defined(__DragonFly__)
396 lockmgr(&ic_list_lock
, LK_RELEASE
);
398 mtx_unlock(&ic_list_mtx
);
403 * Detach net80211 state on device detach. Tear down
404 * all vap's and reclaim all common state prior to the
405 * device state going away. Note we may call back into
406 * driver; it must be prepared for this.
409 ieee80211_ifdetach(struct ieee80211com
*ic
)
411 struct ieee80211vap
*vap
;
413 #if defined(__DragonFly__)
414 wlan_serialize_enter();
417 #if defined(__DragonFly__)
418 lockmgr(&ic_list_lock
, LK_EXCLUSIVE
);
420 mtx_lock(&ic_list_mtx
);
422 LIST_REMOVE(ic
, ic_next
);
423 #if defined(__DragonFly__)
424 lockmgr(&ic_list_lock
, LK_RELEASE
);
426 mtx_unlock(&ic_list_mtx
);
429 #if defined(__DragonFly__)
430 taskqueue_drain(taskqueue_thread
[0], &ic
->ic_restart_task
);
432 taskqueue_drain(taskqueue_thread
, &ic
->ic_restart_task
);
436 * The VAP is responsible for setting and clearing
437 * the VIMAGE context.
439 while ((vap
= TAILQ_FIRST(&ic
->ic_vaps
)) != NULL
)
440 ieee80211_vap_destroy(vap
);
441 ieee80211_waitfor_parent(ic
);
443 ieee80211_sysctl_detach(ic
);
444 ieee80211_dfs_detach(ic
);
445 ieee80211_regdomain_detach(ic
);
446 ieee80211_scan_detach(ic
);
447 #ifdef IEEE80211_SUPPORT_SUPERG
448 ieee80211_superg_detach(ic
);
450 ieee80211_ht_detach(ic
);
451 /* NB: must be called before ieee80211_node_detach */
452 ieee80211_proto_detach(ic
);
453 ieee80211_crypto_detach(ic
);
454 ieee80211_power_detach(ic
);
455 ieee80211_node_detach(ic
);
457 #if defined(__DragonFly__)
459 counter_u64_free(ic
->ic_ierrors
);
460 counter_u64_free(ic
->ic_oerrors
);
463 taskqueue_free(ic
->ic_tq
);
464 IEEE80211_TX_LOCK_DESTROY(ic
);
465 IEEE80211_LOCK_DESTROY(ic
);
467 #if defined(__DragonFly__)
468 wlan_serialize_exit();
472 struct ieee80211com
*
473 ieee80211_find_com(const char *name
)
475 struct ieee80211com
*ic
;
477 #if defined(__DragonFly__)
478 lockmgr(&ic_list_lock
, LK_EXCLUSIVE
);
480 mtx_lock(&ic_list_mtx
);
482 LIST_FOREACH(ic
, &ic_head
, ic_next
) {
483 if (strcmp(ic
->ic_name
, name
) == 0)
486 #if defined(__DragonFly__)
487 lockmgr(&ic_list_lock
, LK_RELEASE
);
489 mtx_unlock(&ic_list_mtx
);
496 * Default reset method for use with the ioctl support. This
497 * method is invoked after any state change in the 802.11
498 * layer that should be propagated to the hardware but not
499 * require re-initialization of the 802.11 state machine (e.g
500 * rescanning for an ap). We always return ENETRESET which
501 * should cause the driver to re-initialize the device. Drivers
502 * can override this method to implement more optimized support.
505 default_reset(struct ieee80211vap
*vap
, u_long cmd
)
510 #if defined(__DragonFly__)
513 * Add underlying device errors to vap errors.
516 ieee80211_get_counter(struct ifnet
*ifp
, ift_counter cnt
)
518 struct ieee80211vap
*vap
= ifp
->if_softc
;
519 struct ieee80211com
*ic
= vap
->iv_ic
;
522 rv
= if_get_counter_default(ifp
, cnt
);
524 case IFCOUNTER_OERRORS
:
525 rv
+= counter_u64_fetch(ic
->ic_oerrors
);
527 case IFCOUNTER_IERRORS
:
528 rv
+= counter_u64_fetch(ic
->ic_ierrors
);
540 * Prepare a vap for use. Drivers use this call to
541 * setup net80211 state in new vap's prior attaching
542 * them with ieee80211_vap_attach (below).
545 ieee80211_vap_setup(struct ieee80211com
*ic
, struct ieee80211vap
*vap
,
546 const char name
[IFNAMSIZ
], int unit
, enum ieee80211_opmode opmode
,
547 int flags
, const uint8_t bssid
[IEEE80211_ADDR_LEN
])
551 ifp
= if_alloc(IFT_ETHER
);
553 ic_printf(ic
, "%s: unable to allocate ifnet\n",
557 if_initname(ifp
, name
, unit
);
558 ifp
->if_softc
= vap
; /* back pointer */
559 ifp
->if_flags
= IFF_SIMPLEX
| IFF_BROADCAST
| IFF_MULTICAST
;
560 #if defined(__DragonFly__)
561 ifp
->if_start
= ieee80211_vap_start
;
563 ifp
->if_transmit
= ieee80211_vap_transmit
;
564 ifp
->if_qflush
= ieee80211_vap_qflush
;
566 ifp
->if_ioctl
= ieee80211_ioctl
;
567 ifp
->if_init
= ieee80211_init
;
568 #if defined(__DragonFly__)
570 ifp
->if_get_counter
= ieee80211_get_counter
;
575 vap
->iv_flags
= ic
->ic_flags
; /* propagate common flags */
576 vap
->iv_flags_ext
= ic
->ic_flags_ext
;
577 vap
->iv_flags_ven
= ic
->ic_flags_ven
;
578 vap
->iv_caps
= ic
->ic_caps
&~ IEEE80211_C_OPMODE
;
579 vap
->iv_htcaps
= ic
->ic_htcaps
;
580 vap
->iv_htextcaps
= ic
->ic_htextcaps
;
581 vap
->iv_opmode
= opmode
;
582 vap
->iv_caps
|= ieee80211_opcap
[opmode
];
583 IEEE80211_ADDR_COPY(vap
->iv_myaddr
, ic
->ic_macaddr
);
585 case IEEE80211_M_WDS
:
587 * WDS links must specify the bssid of the far end.
588 * For legacy operation this is a static relationship.
589 * For non-legacy operation the station must associate
590 * and be authorized to pass traffic. Plumbing the
591 * vap to the proper node happens when the vap
592 * transitions to RUN state.
594 IEEE80211_ADDR_COPY(vap
->iv_des_bssid
, bssid
);
595 vap
->iv_flags
|= IEEE80211_F_DESBSSID
;
596 if (flags
& IEEE80211_CLONE_WDSLEGACY
)
597 vap
->iv_flags_ext
|= IEEE80211_FEXT_WDSLEGACY
;
599 #ifdef IEEE80211_SUPPORT_TDMA
600 case IEEE80211_M_AHDEMO
:
601 if (flags
& IEEE80211_CLONE_TDMA
) {
602 /* NB: checked before clone operation allowed */
603 KASSERT(ic
->ic_caps
& IEEE80211_C_TDMA
,
604 ("not TDMA capable, ic_caps 0x%x", ic
->ic_caps
));
606 * Propagate TDMA capability to mark vap; this
607 * cannot be removed and is used to distinguish
608 * regular ahdemo operation from ahdemo+tdma.
610 vap
->iv_caps
|= IEEE80211_C_TDMA
;
617 /* auto-enable s/w beacon miss support */
618 if (flags
& IEEE80211_CLONE_NOBEACONS
)
619 vap
->iv_flags_ext
|= IEEE80211_FEXT_SWBMISS
;
620 /* auto-generated or user supplied MAC address */
621 if (flags
& (IEEE80211_CLONE_BSSID
|IEEE80211_CLONE_MACADDR
))
622 vap
->iv_flags_ext
|= IEEE80211_FEXT_UNIQMAC
;
624 * Enable various functionality by default if we're
625 * capable; the driver can override us if it knows better.
627 if (vap
->iv_caps
& IEEE80211_C_WME
)
628 vap
->iv_flags
|= IEEE80211_F_WME
;
629 if (vap
->iv_caps
& IEEE80211_C_BURST
)
630 vap
->iv_flags
|= IEEE80211_F_BURST
;
631 /* NB: bg scanning only makes sense for station mode right now */
632 #if defined(__DragonFly__)
634 * DISABLE BGSCAN BY DEFAULT, many issues can crop up including
635 * the link going dead.
639 if (vap
->iv_opmode
== IEEE80211_M_STA
&&
640 (vap
->iv_caps
& IEEE80211_C_BGSCAN
))
641 vap
->iv_flags
|= IEEE80211_F_BGSCAN
;
643 vap
->iv_flags
|= IEEE80211_F_DOTH
; /* XXX no cap, just ena */
644 /* NB: DFS support only makes sense for ap mode right now */
645 if (vap
->iv_opmode
== IEEE80211_M_HOSTAP
&&
646 (vap
->iv_caps
& IEEE80211_C_DFS
))
647 vap
->iv_flags_ext
|= IEEE80211_FEXT_DFS
;
649 vap
->iv_des_chan
= IEEE80211_CHAN_ANYC
; /* any channel is ok */
650 vap
->iv_bmissthreshold
= IEEE80211_HWBMISS_DEFAULT
;
651 vap
->iv_dtim_period
= IEEE80211_DTIM_DEFAULT
;
653 * Install a default reset method for the ioctl support;
654 * the driver can override this.
656 vap
->iv_reset
= default_reset
;
658 ieee80211_sysctl_vattach(vap
);
659 ieee80211_crypto_vattach(vap
);
660 ieee80211_node_vattach(vap
);
661 ieee80211_power_vattach(vap
);
662 ieee80211_proto_vattach(vap
);
663 #ifdef IEEE80211_SUPPORT_SUPERG
664 ieee80211_superg_vattach(vap
);
666 ieee80211_ht_vattach(vap
);
667 ieee80211_scan_vattach(vap
);
668 ieee80211_regdomain_vattach(vap
);
669 ieee80211_radiotap_vattach(vap
);
670 ieee80211_ratectl_set(vap
, IEEE80211_RATECTL_NONE
);
676 * Activate a vap. State should have been prepared with a
677 * call to ieee80211_vap_setup and by the driver. On return
678 * from this call the vap is ready for use.
681 ieee80211_vap_attach(struct ieee80211vap
*vap
, ifm_change_cb_t media_change
,
682 ifm_stat_cb_t media_stat
, const uint8_t macaddr
[IEEE80211_ADDR_LEN
])
684 struct ifnet
*ifp
= vap
->iv_ifp
;
685 struct ieee80211com
*ic
= vap
->iv_ic
;
686 struct ifmediareq imr
;
689 #if defined(__DragonFly__)
691 * This function must _not_ be serialized by the WLAN serializer,
692 * since it could dead-lock the domsg to netisrs in ether_ifattach().
694 wlan_assert_notserialized();
696 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_STATE
,
697 "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
698 __func__
, ieee80211_opmode_name
[vap
->iv_opmode
],
699 ic
->ic_name
, vap
->iv_flags
, vap
->iv_flags_ext
);
702 * Do late attach work that cannot happen until after
703 * the driver has had a chance to override defaults.
705 ieee80211_node_latevattach(vap
);
706 ieee80211_power_latevattach(vap
);
708 maxrate
= ieee80211_media_setup(ic
, &vap
->iv_media
, vap
->iv_caps
,
709 vap
->iv_opmode
== IEEE80211_M_STA
, media_change
, media_stat
);
710 ieee80211_media_status(ifp
, &imr
);
711 /* NB: strip explicit mode; we're actually in autoselect */
712 ifmedia_set(&vap
->iv_media
,
713 imr
.ifm_active
&~ (IFM_MMASK
| IFM_IEEE80211_TURBO
));
715 ifp
->if_baudrate
= IF_Mbps(maxrate
);
717 #if defined(__DragonFly__)
718 ether_ifattach(ifp
, macaddr
, &wlan_global_serializer
);
720 ether_ifattach(ifp
, macaddr
);
722 IEEE80211_ADDR_COPY(vap
->iv_myaddr
, IF_LLADDR(ifp
));
723 /* hook output method setup by ether_ifattach */
724 vap
->iv_output
= ifp
->if_output
;
725 ifp
->if_output
= ieee80211_output
;
726 /* NB: if_mtu set by ether_ifattach to ETHERMTU */
729 TAILQ_INSERT_TAIL(&ic
->ic_vaps
, vap
, iv_next
);
730 ieee80211_syncflag_locked(ic
, IEEE80211_F_WME
);
731 #ifdef IEEE80211_SUPPORT_SUPERG
732 ieee80211_syncflag_locked(ic
, IEEE80211_F_TURBOP
);
734 ieee80211_syncflag_locked(ic
, IEEE80211_F_PCF
);
735 ieee80211_syncflag_locked(ic
, IEEE80211_F_BURST
);
736 ieee80211_syncflag_ht_locked(ic
, IEEE80211_FHT_HT
);
737 ieee80211_syncflag_ht_locked(ic
, IEEE80211_FHT_USEHT40
);
738 IEEE80211_UNLOCK(ic
);
744 * Tear down vap state and reclaim the ifnet.
745 * The driver is assumed to have prepared for
746 * this; e.g. by turning off interrupts for the
750 ieee80211_vap_detach(struct ieee80211vap
*vap
)
752 struct ieee80211com
*ic
= vap
->iv_ic
;
753 struct ifnet
*ifp
= vap
->iv_ifp
;
755 #if defined(__DragonFly__)
757 * This function must _not_ be serialized by the WLAN serializer,
758 * since it could dead-lock the domsg to netisrs in ether_ifdettach().
760 wlan_assert_notserialized();
762 CURVNET_SET(ifp
->if_vnet
);
764 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_STATE
, "%s: %s parent %s\n",
765 __func__
, ieee80211_opmode_name
[vap
->iv_opmode
], ic
->ic_name
);
767 /* NB: bpfdetach is called by ether_ifdetach and claims all taps */
773 * Flush any deferred vap tasks.
775 ieee80211_draintask(ic
, &vap
->iv_nstate_task
);
776 ieee80211_draintask(ic
, &vap
->iv_swbmiss_task
);
778 #if defined(__DragonFly__)
779 /* XXX hmm, not sure what we should do here */
781 /* XXX band-aid until ifnet handles this for us */
782 taskqueue_drain(taskqueue_swi
, &ifp
->if_linktask
);
786 KASSERT(vap
->iv_state
== IEEE80211_S_INIT
, ("vap still running"));
787 TAILQ_REMOVE(&ic
->ic_vaps
, vap
, iv_next
);
788 ieee80211_syncflag_locked(ic
, IEEE80211_F_WME
);
789 #ifdef IEEE80211_SUPPORT_SUPERG
790 ieee80211_syncflag_locked(ic
, IEEE80211_F_TURBOP
);
792 ieee80211_syncflag_locked(ic
, IEEE80211_F_PCF
);
793 ieee80211_syncflag_locked(ic
, IEEE80211_F_BURST
);
794 ieee80211_syncflag_ht_locked(ic
, IEEE80211_FHT_HT
);
795 ieee80211_syncflag_ht_locked(ic
, IEEE80211_FHT_USEHT40
);
796 /* NB: this handles the bpfdetach done below */
797 ieee80211_syncflag_ext_locked(ic
, IEEE80211_FEXT_BPF
);
798 #if defined(__DragonFly__)
799 if (vap
->iv_ifflags
& IFF_PROMISC
)
800 ieee80211_promisc(vap
, 0);
801 if (vap
->iv_ifflags
& IFF_ALLMULTI
)
802 ieee80211_allmulti(vap
, 0);
804 if (vap
->iv_ifflags
& IFF_PROMISC
)
805 ieee80211_promisc(vap
, false);
806 if (vap
->iv_ifflags
& IFF_ALLMULTI
)
807 ieee80211_allmulti(vap
, false);
809 IEEE80211_UNLOCK(ic
);
811 ifmedia_removeall(&vap
->iv_media
);
813 ieee80211_radiotap_vdetach(vap
);
814 ieee80211_regdomain_vdetach(vap
);
815 ieee80211_scan_vdetach(vap
);
816 #ifdef IEEE80211_SUPPORT_SUPERG
817 ieee80211_superg_vdetach(vap
);
819 ieee80211_ht_vdetach(vap
);
820 /* NB: must be before ieee80211_node_vdetach */
821 ieee80211_proto_vdetach(vap
);
822 ieee80211_crypto_vdetach(vap
);
823 ieee80211_power_vdetach(vap
);
824 ieee80211_node_vdetach(vap
);
825 ieee80211_sysctl_vdetach(vap
);
833 * Count number of vaps in promisc, and issue promisc on
834 * parent respectively.
836 #if defined(__DragonFly__)
838 ieee80211_promisc(struct ieee80211vap
*vap
, int on
)
841 ieee80211_promisc(struct ieee80211vap
*vap
, bool on
)
844 struct ieee80211com
*ic
= vap
->iv_ic
;
846 IEEE80211_LOCK_ASSERT(ic
);
849 if (++ic
->ic_promisc
== 1)
850 ieee80211_runtask(ic
, &ic
->ic_promisc_task
);
852 KASSERT(ic
->ic_promisc
> 0, ("%s: ic %p not promisc",
854 if (--ic
->ic_promisc
== 0)
855 ieee80211_runtask(ic
, &ic
->ic_promisc_task
);
860 * Count number of vaps in allmulti, and issue allmulti on
861 * parent respectively.
863 #if defined(__DragonFly__)
865 ieee80211_allmulti(struct ieee80211vap
*vap
, int on
)
868 ieee80211_allmulti(struct ieee80211vap
*vap
, bool on
)
871 struct ieee80211com
*ic
= vap
->iv_ic
;
873 IEEE80211_LOCK_ASSERT(ic
);
876 if (++ic
->ic_allmulti
== 1)
877 ieee80211_runtask(ic
, &ic
->ic_mcast_task
);
879 KASSERT(ic
->ic_allmulti
> 0, ("%s: ic %p not allmulti",
881 if (--ic
->ic_allmulti
== 0)
882 ieee80211_runtask(ic
, &ic
->ic_mcast_task
);
887 * Synchronize flag bit state in the com structure
888 * according to the state of all vap's. This is used,
889 * for example, to handle state changes via ioctls.
892 ieee80211_syncflag_locked(struct ieee80211com
*ic
, int flag
)
894 struct ieee80211vap
*vap
;
897 IEEE80211_LOCK_ASSERT(ic
);
900 TAILQ_FOREACH(vap
, &ic
->ic_vaps
, iv_next
)
901 if (vap
->iv_flags
& flag
) {
906 ic
->ic_flags
|= flag
;
908 ic
->ic_flags
&= ~flag
;
912 ieee80211_syncflag(struct ieee80211vap
*vap
, int flag
)
914 struct ieee80211com
*ic
= vap
->iv_ic
;
919 vap
->iv_flags
&= ~flag
;
921 vap
->iv_flags
|= flag
;
922 ieee80211_syncflag_locked(ic
, flag
);
923 IEEE80211_UNLOCK(ic
);
927 * Synchronize flags_ht bit state in the com structure
928 * according to the state of all vap's. This is used,
929 * for example, to handle state changes via ioctls.
932 ieee80211_syncflag_ht_locked(struct ieee80211com
*ic
, int flag
)
934 struct ieee80211vap
*vap
;
937 IEEE80211_LOCK_ASSERT(ic
);
940 TAILQ_FOREACH(vap
, &ic
->ic_vaps
, iv_next
)
941 if (vap
->iv_flags_ht
& flag
) {
946 ic
->ic_flags_ht
|= flag
;
948 ic
->ic_flags_ht
&= ~flag
;
952 ieee80211_syncflag_ht(struct ieee80211vap
*vap
, int flag
)
954 struct ieee80211com
*ic
= vap
->iv_ic
;
959 vap
->iv_flags_ht
&= ~flag
;
961 vap
->iv_flags_ht
|= flag
;
962 ieee80211_syncflag_ht_locked(ic
, flag
);
963 IEEE80211_UNLOCK(ic
);
967 * Synchronize flags_ext bit state in the com structure
968 * according to the state of all vap's. This is used,
969 * for example, to handle state changes via ioctls.
972 ieee80211_syncflag_ext_locked(struct ieee80211com
*ic
, int flag
)
974 struct ieee80211vap
*vap
;
977 IEEE80211_LOCK_ASSERT(ic
);
980 TAILQ_FOREACH(vap
, &ic
->ic_vaps
, iv_next
)
981 if (vap
->iv_flags_ext
& flag
) {
986 ic
->ic_flags_ext
|= flag
;
988 ic
->ic_flags_ext
&= ~flag
;
992 ieee80211_syncflag_ext(struct ieee80211vap
*vap
, int flag
)
994 struct ieee80211com
*ic
= vap
->iv_ic
;
999 vap
->iv_flags_ext
&= ~flag
;
1001 vap
->iv_flags_ext
|= flag
;
1002 ieee80211_syncflag_ext_locked(ic
, flag
);
1003 IEEE80211_UNLOCK(ic
);
1007 mapgsm(u_int freq
, u_int flags
)
1010 if (flags
& IEEE80211_CHAN_QUARTER
)
1012 else if (flags
& IEEE80211_CHAN_HALF
)
1016 /* NB: there is no 907/20 wide but leave room */
1017 return (freq
- 906*10) / 5;
1021 mappsb(u_int freq
, u_int flags
)
1023 return 37 + ((freq
* 10) + ((freq
% 5) == 2 ? 5 : 0) - 49400) / 5;
1027 * Convert MHz frequency to IEEE channel number.
1030 ieee80211_mhz2ieee(u_int freq
, u_int flags
)
1032 #define IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
1033 if (flags
& IEEE80211_CHAN_GSM
)
1034 return mapgsm(freq
, flags
);
1035 if (flags
& IEEE80211_CHAN_2GHZ
) { /* 2GHz band */
1039 return ((int) freq
- 2407) / 5;
1041 return 15 + ((freq
- 2512) / 20);
1042 } else if (flags
& IEEE80211_CHAN_5GHZ
) { /* 5Ghz band */
1044 /* XXX check regdomain? */
1045 if (IS_FREQ_IN_PSB(freq
))
1046 return mappsb(freq
, flags
);
1047 return (freq
- 4000) / 5;
1049 return (freq
- 5000) / 5;
1050 } else { /* either, guess */
1054 if (907 <= freq
&& freq
<= 922)
1055 return mapgsm(freq
, flags
);
1056 return ((int) freq
- 2407) / 5;
1059 if (IS_FREQ_IN_PSB(freq
))
1060 return mappsb(freq
, flags
);
1061 else if (freq
> 4900)
1062 return (freq
- 4000) / 5;
1064 return 15 + ((freq
- 2512) / 20);
1066 return (freq
- 5000) / 5;
1068 #undef IS_FREQ_IN_PSB
1072 * Convert channel to IEEE channel number.
1075 ieee80211_chan2ieee(struct ieee80211com
*ic
, const struct ieee80211_channel
*c
)
1078 ic_printf(ic
, "invalid channel (NULL)\n");
1081 return (c
== IEEE80211_CHAN_ANYC
? IEEE80211_CHAN_ANY
: c
->ic_ieee
);
1085 * Convert IEEE channel number to MHz frequency.
1088 ieee80211_ieee2mhz(u_int chan
, u_int flags
)
1090 if (flags
& IEEE80211_CHAN_GSM
)
1091 return 907 + 5 * (chan
/ 10);
1092 if (flags
& IEEE80211_CHAN_2GHZ
) { /* 2GHz band */
1096 return 2407 + chan
*5;
1098 return 2512 + ((chan
-15)*20);
1099 } else if (flags
& IEEE80211_CHAN_5GHZ
) {/* 5Ghz band */
1100 if (flags
& (IEEE80211_CHAN_HALF
|IEEE80211_CHAN_QUARTER
)) {
1102 return 4940 + chan
*5 + (chan
% 5 ? 2 : 0);
1104 return 5000 + (chan
*5);
1105 } else { /* either, guess */
1106 /* XXX can't distinguish PSB+GSM channels */
1109 if (chan
< 14) /* 0-13 */
1110 return 2407 + chan
*5;
1111 if (chan
< 27) /* 15-26 */
1112 return 2512 + ((chan
-15)*20);
1113 return 5000 + (chan
*5);
1117 static __inline
void
1118 set_extchan(struct ieee80211_channel
*c
)
1122 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
1123 * "the secondary channel number shall be 'N + [1,-1] * 4'
1125 if (c
->ic_flags
& IEEE80211_CHAN_HT40U
)
1126 c
->ic_extieee
= c
->ic_ieee
+ 4;
1127 else if (c
->ic_flags
& IEEE80211_CHAN_HT40D
)
1128 c
->ic_extieee
= c
->ic_ieee
- 4;
1134 addchan(struct ieee80211_channel chans
[], int maxchans
, int *nchans
,
1135 uint8_t ieee
, uint16_t freq
, int8_t maxregpower
, uint32_t flags
)
1137 struct ieee80211_channel
*c
;
1139 if (*nchans
>= maxchans
)
1142 c
= &chans
[(*nchans
)++];
1144 c
->ic_freq
= freq
!= 0 ? freq
: ieee80211_ieee2mhz(ieee
, flags
);
1145 c
->ic_maxregpower
= maxregpower
;
1146 c
->ic_maxpower
= 2 * maxregpower
;
1147 c
->ic_flags
= flags
;
1154 copychan_prev(struct ieee80211_channel chans
[], int maxchans
, int *nchans
,
1157 struct ieee80211_channel
*c
;
1159 KASSERT(*nchans
> 0, ("channel list is empty\n"));
1161 if (*nchans
>= maxchans
)
1164 c
= &chans
[(*nchans
)++];
1166 c
->ic_flags
= flags
;
1173 getflags_2ghz(const uint8_t bands
[], uint32_t flags
[], int ht40
)
1178 if (isset(bands
, IEEE80211_MODE_11B
))
1179 flags
[nmodes
++] = IEEE80211_CHAN_B
;
1180 if (isset(bands
, IEEE80211_MODE_11G
))
1181 flags
[nmodes
++] = IEEE80211_CHAN_G
;
1182 if (isset(bands
, IEEE80211_MODE_11NG
))
1183 flags
[nmodes
++] = IEEE80211_CHAN_G
| IEEE80211_CHAN_HT20
;
1185 flags
[nmodes
++] = IEEE80211_CHAN_G
| IEEE80211_CHAN_HT40U
;
1186 flags
[nmodes
++] = IEEE80211_CHAN_G
| IEEE80211_CHAN_HT40D
;
1192 getflags_5ghz(const uint8_t bands
[], uint32_t flags
[], int ht40
)
1197 if (isset(bands
, IEEE80211_MODE_11A
))
1198 flags
[nmodes
++] = IEEE80211_CHAN_A
;
1199 if (isset(bands
, IEEE80211_MODE_11NA
))
1200 flags
[nmodes
++] = IEEE80211_CHAN_A
| IEEE80211_CHAN_HT20
;
1202 flags
[nmodes
++] = IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40U
;
1203 flags
[nmodes
++] = IEEE80211_CHAN_A
| IEEE80211_CHAN_HT40D
;
1209 getflags(const uint8_t bands
[], uint32_t flags
[], int ht40
)
1213 if (isset(bands
, IEEE80211_MODE_11A
) ||
1214 isset(bands
, IEEE80211_MODE_11NA
)) {
1215 if (isset(bands
, IEEE80211_MODE_11B
) ||
1216 isset(bands
, IEEE80211_MODE_11G
) ||
1217 isset(bands
, IEEE80211_MODE_11NG
))
1220 getflags_5ghz(bands
, flags
, ht40
);
1222 getflags_2ghz(bands
, flags
, ht40
);
1226 * Add one 20 MHz channel into specified channel list.
1229 ieee80211_add_channel(struct ieee80211_channel chans
[], int maxchans
,
1230 int *nchans
, uint8_t ieee
, uint16_t freq
, int8_t maxregpower
,
1231 uint32_t chan_flags
, const uint8_t bands
[])
1233 uint32_t flags
[IEEE80211_MODE_MAX
];
1236 getflags(bands
, flags
, 0);
1237 KASSERT(flags
[0] != 0, ("%s: no correct mode provided\n", __func__
));
1239 error
= addchan(chans
, maxchans
, nchans
, ieee
, freq
, maxregpower
,
1240 flags
[0] | chan_flags
);
1241 for (i
= 1; flags
[i
] != 0 && error
== 0; i
++) {
1242 error
= copychan_prev(chans
, maxchans
, nchans
,
1243 flags
[i
] | chan_flags
);
1249 static struct ieee80211_channel
*
1250 findchannel(struct ieee80211_channel chans
[], int nchans
, uint16_t freq
,
1253 struct ieee80211_channel
*c
;
1256 flags
&= IEEE80211_CHAN_ALLTURBO
;
1257 /* brute force search */
1258 for (i
= 0; i
< nchans
; i
++) {
1260 if (c
->ic_freq
== freq
&&
1261 (c
->ic_flags
& IEEE80211_CHAN_ALLTURBO
) == flags
)
1268 * Add 40 MHz channel pair into specified channel list.
1271 ieee80211_add_channel_ht40(struct ieee80211_channel chans
[], int maxchans
,
1272 int *nchans
, uint8_t ieee
, int8_t maxregpower
, uint32_t flags
)
1274 struct ieee80211_channel
*cent
, *extc
;
1278 freq
= ieee80211_ieee2mhz(ieee
, flags
);
1281 * Each entry defines an HT40 channel pair; find the
1282 * center channel, then the extension channel above.
1284 flags
|= IEEE80211_CHAN_HT20
;
1285 cent
= findchannel(chans
, *nchans
, freq
, flags
);
1289 extc
= findchannel(chans
, *nchans
, freq
+ 20, flags
);
1293 flags
&= ~IEEE80211_CHAN_HT
;
1294 error
= addchan(chans
, maxchans
, nchans
, cent
->ic_ieee
, cent
->ic_freq
,
1295 maxregpower
, flags
| IEEE80211_CHAN_HT40U
);
1299 error
= addchan(chans
, maxchans
, nchans
, extc
->ic_ieee
, extc
->ic_freq
,
1300 maxregpower
, flags
| IEEE80211_CHAN_HT40D
);
1306 * Adds channels into specified channel list (ieee[] array must be sorted).
1307 * Channels are already sorted.
1310 add_chanlist(struct ieee80211_channel chans
[], int maxchans
, int *nchans
,
1311 const uint8_t ieee
[], int nieee
, uint32_t flags
[])
1316 #if defined(__DragonFly__)
1317 error
= 0; /* work-around GCC uninitialized variable warning */
1319 for (i
= 0; i
< nieee
; i
++) {
1320 freq
= ieee80211_ieee2mhz(ieee
[i
], flags
[0]);
1321 for (j
= 0; flags
[j
] != 0; j
++) {
1322 if (flags
[j
] & IEEE80211_CHAN_HT40D
)
1323 if (i
== 0 || ieee
[i
] < ieee
[0] + 4 ||
1325 ieee80211_ieee2mhz(ieee
[i
] - 4, flags
[j
]))
1327 if (flags
[j
] & IEEE80211_CHAN_HT40U
)
1328 if (i
== nieee
- 1 ||
1329 ieee
[i
] + 4 > ieee
[nieee
- 1] ||
1331 ieee80211_ieee2mhz(ieee
[i
] + 4, flags
[j
]))
1335 error
= addchan(chans
, maxchans
, nchans
,
1336 ieee
[i
], freq
, 0, flags
[j
]);
1338 error
= copychan_prev(chans
, maxchans
, nchans
,
1350 ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans
[], int maxchans
,
1351 int *nchans
, const uint8_t ieee
[], int nieee
, const uint8_t bands
[],
1354 uint32_t flags
[IEEE80211_MODE_MAX
];
1356 getflags_2ghz(bands
, flags
, ht40
);
1357 KASSERT(flags
[0] != 0, ("%s: no correct mode provided\n", __func__
));
1359 return (add_chanlist(chans
, maxchans
, nchans
, ieee
, nieee
, flags
));
1363 ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans
[], int maxchans
,
1364 int *nchans
, const uint8_t ieee
[], int nieee
, const uint8_t bands
[],
1367 uint32_t flags
[IEEE80211_MODE_MAX
];
1369 getflags_5ghz(bands
, flags
, ht40
);
1370 KASSERT(flags
[0] != 0, ("%s: no correct mode provided\n", __func__
));
1372 return (add_chanlist(chans
, maxchans
, nchans
, ieee
, nieee
, flags
));
1376 * Locate a channel given a frequency+flags. We cache
1377 * the previous lookup to optimize switching between two
1378 * channels--as happens with dynamic turbo.
1380 struct ieee80211_channel
*
1381 ieee80211_find_channel(struct ieee80211com
*ic
, int freq
, int flags
)
1383 struct ieee80211_channel
*c
;
1385 flags
&= IEEE80211_CHAN_ALLTURBO
;
1386 c
= ic
->ic_prevchan
;
1387 if (c
!= NULL
&& c
->ic_freq
== freq
&&
1388 (c
->ic_flags
& IEEE80211_CHAN_ALLTURBO
) == flags
)
1390 /* brute force search */
1391 return (findchannel(ic
->ic_channels
, ic
->ic_nchans
, freq
, flags
));
1395 * Locate a channel given a channel number+flags. We cache
1396 * the previous lookup to optimize switching between two
1397 * channels--as happens with dynamic turbo.
1399 struct ieee80211_channel
*
1400 ieee80211_find_channel_byieee(struct ieee80211com
*ic
, int ieee
, int flags
)
1402 struct ieee80211_channel
*c
;
1405 flags
&= IEEE80211_CHAN_ALLTURBO
;
1406 c
= ic
->ic_prevchan
;
1407 if (c
!= NULL
&& c
->ic_ieee
== ieee
&&
1408 (c
->ic_flags
& IEEE80211_CHAN_ALLTURBO
) == flags
)
1410 /* brute force search */
1411 for (i
= 0; i
< ic
->ic_nchans
; i
++) {
1412 c
= &ic
->ic_channels
[i
];
1413 if (c
->ic_ieee
== ieee
&&
1414 (c
->ic_flags
& IEEE80211_CHAN_ALLTURBO
) == flags
)
1421 * Lookup a channel suitable for the given rx status.
1423 * This is used to find a channel for a frame (eg beacon, probe
1424 * response) based purely on the received PHY information.
1426 * For now it tries to do it based on R_FREQ / R_IEEE.
1427 * This is enough for 11bg and 11a (and thus 11ng/11na)
1428 * but it will not be enough for GSM, PSB channels and the
1429 * like. It also doesn't know about legacy-turbog and
1430 * legacy-turbo modes, which some offload NICs actually
1431 * support in weird ways.
1433 * Takes the ic and rxstatus; returns the channel or NULL
1436 * XXX TODO: Add support for that when the need arises.
1438 struct ieee80211_channel
*
1439 ieee80211_lookup_channel_rxstatus(struct ieee80211vap
*vap
,
1440 const struct ieee80211_rx_stats
*rxs
)
1442 struct ieee80211com
*ic
= vap
->iv_ic
;
1444 struct ieee80211_channel
*c
;
1450 * Strictly speaking we only use freq for now,
1451 * however later on we may wish to just store
1452 * the ieee for verification.
1454 if ((rxs
->r_flags
& IEEE80211_R_FREQ
) == 0)
1456 if ((rxs
->r_flags
& IEEE80211_R_IEEE
) == 0)
1460 * If the rx status contains a valid ieee/freq, then
1461 * ensure we populate the correct channel information
1462 * in rxchan before passing it up to the scan infrastructure.
1463 * Offload NICs will pass up beacons from all channels
1464 * during background scans.
1467 /* Determine a band */
1468 /* XXX should be done by the driver? */
1469 if (rxs
->c_freq
< 3000) {
1470 flags
= IEEE80211_CHAN_G
;
1472 flags
= IEEE80211_CHAN_A
;
1475 /* Channel lookup */
1476 c
= ieee80211_find_channel(ic
, rxs
->c_freq
, flags
);
1478 IEEE80211_DPRINTF(vap
, IEEE80211_MSG_INPUT
,
1479 "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
1490 addmedia(struct ifmedia
*media
, int caps
, int addsta
, int mode
, int mword
)
1492 #define ADD(_ic, _s, _o) \
1493 ifmedia_add(media, \
1494 IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
1495 static const u_int mopts
[IEEE80211_MODE_MAX
] = {
1496 [IEEE80211_MODE_AUTO
] = IFM_AUTO
,
1497 [IEEE80211_MODE_11A
] = IFM_IEEE80211_11A
,
1498 [IEEE80211_MODE_11B
] = IFM_IEEE80211_11B
,
1499 [IEEE80211_MODE_11G
] = IFM_IEEE80211_11G
,
1500 [IEEE80211_MODE_FH
] = IFM_IEEE80211_FH
,
1501 [IEEE80211_MODE_TURBO_A
] = IFM_IEEE80211_11A
|IFM_IEEE80211_TURBO
,
1502 [IEEE80211_MODE_TURBO_G
] = IFM_IEEE80211_11G
|IFM_IEEE80211_TURBO
,
1503 [IEEE80211_MODE_STURBO_A
] = IFM_IEEE80211_11A
|IFM_IEEE80211_TURBO
,
1504 [IEEE80211_MODE_HALF
] = IFM_IEEE80211_11A
, /* XXX */
1505 [IEEE80211_MODE_QUARTER
] = IFM_IEEE80211_11A
, /* XXX */
1506 [IEEE80211_MODE_11NA
] = IFM_IEEE80211_11NA
,
1507 [IEEE80211_MODE_11NG
] = IFM_IEEE80211_11NG
,
1513 ADD(ic
, mword
, mopt
); /* STA mode has no cap */
1514 if (caps
& IEEE80211_C_IBSS
)
1515 ADD(media
, mword
, mopt
| IFM_IEEE80211_ADHOC
);
1516 if (caps
& IEEE80211_C_HOSTAP
)
1517 ADD(media
, mword
, mopt
| IFM_IEEE80211_HOSTAP
);
1518 if (caps
& IEEE80211_C_AHDEMO
)
1519 ADD(media
, mword
, mopt
| IFM_IEEE80211_ADHOC
| IFM_FLAG0
);
1520 if (caps
& IEEE80211_C_MONITOR
)
1521 ADD(media
, mword
, mopt
| IFM_IEEE80211_MONITOR
);
1522 if (caps
& IEEE80211_C_WDS
)
1523 ADD(media
, mword
, mopt
| IFM_IEEE80211_WDS
);
1524 if (caps
& IEEE80211_C_MBSS
)
1525 ADD(media
, mword
, mopt
| IFM_IEEE80211_MBSS
);
1530 * Setup the media data structures according to the channel and
1534 ieee80211_media_setup(struct ieee80211com
*ic
,
1535 struct ifmedia
*media
, int caps
, int addsta
,
1536 ifm_change_cb_t media_change
, ifm_stat_cb_t media_stat
)
1538 int i
, j
, rate
, maxrate
, mword
, r
;
1539 enum ieee80211_phymode mode
;
1540 const struct ieee80211_rateset
*rs
;
1541 struct ieee80211_rateset allrates
;
1544 * Fill in media characteristics.
1546 ifmedia_init(media
, 0, media_change
, media_stat
);
1549 * Add media for legacy operating modes.
1551 memset(&allrates
, 0, sizeof(allrates
));
1552 for (mode
= IEEE80211_MODE_AUTO
; mode
< IEEE80211_MODE_11NA
; mode
++) {
1553 if (isclr(ic
->ic_modecaps
, mode
))
1555 addmedia(media
, caps
, addsta
, mode
, IFM_AUTO
);
1556 if (mode
== IEEE80211_MODE_AUTO
)
1558 rs
= &ic
->ic_sup_rates
[mode
];
1559 for (i
= 0; i
< rs
->rs_nrates
; i
++) {
1560 rate
= rs
->rs_rates
[i
];
1561 mword
= ieee80211_rate2media(ic
, rate
, mode
);
1564 addmedia(media
, caps
, addsta
, mode
, mword
);
1566 * Add legacy rate to the collection of all rates.
1568 r
= rate
& IEEE80211_RATE_VAL
;
1569 for (j
= 0; j
< allrates
.rs_nrates
; j
++)
1570 if (allrates
.rs_rates
[j
] == r
)
1572 if (j
== allrates
.rs_nrates
) {
1573 /* unique, add to the set */
1574 allrates
.rs_rates
[j
] = r
;
1575 allrates
.rs_nrates
++;
1577 rate
= (rate
& IEEE80211_RATE_VAL
) / 2;
1582 for (i
= 0; i
< allrates
.rs_nrates
; i
++) {
1583 mword
= ieee80211_rate2media(ic
, allrates
.rs_rates
[i
],
1584 IEEE80211_MODE_AUTO
);
1587 /* NB: remove media options from mword */
1588 addmedia(media
, caps
, addsta
,
1589 IEEE80211_MODE_AUTO
, IFM_SUBTYPE(mword
));
1592 * Add HT/11n media. Note that we do not have enough
1593 * bits in the media subtype to express the MCS so we
1594 * use a "placeholder" media subtype and any fixed MCS
1595 * must be specified with a different mechanism.
1597 for (; mode
<= IEEE80211_MODE_11NG
; mode
++) {
1598 if (isclr(ic
->ic_modecaps
, mode
))
1600 addmedia(media
, caps
, addsta
, mode
, IFM_AUTO
);
1601 addmedia(media
, caps
, addsta
, mode
, IFM_IEEE80211_MCS
);
1603 if (isset(ic
->ic_modecaps
, IEEE80211_MODE_11NA
) ||
1604 isset(ic
->ic_modecaps
, IEEE80211_MODE_11NG
)) {
1605 addmedia(media
, caps
, addsta
,
1606 IEEE80211_MODE_AUTO
, IFM_IEEE80211_MCS
);
1607 i
= ic
->ic_txstream
* 8 - 1;
1608 if ((ic
->ic_htcaps
& IEEE80211_HTCAP_CHWIDTH40
) &&
1609 (ic
->ic_htcaps
& IEEE80211_HTCAP_SHORTGI40
))
1610 rate
= ieee80211_htrates
[i
].ht40_rate_400ns
;
1611 else if ((ic
->ic_htcaps
& IEEE80211_HTCAP_CHWIDTH40
))
1612 rate
= ieee80211_htrates
[i
].ht40_rate_800ns
;
1613 else if ((ic
->ic_htcaps
& IEEE80211_HTCAP_SHORTGI20
))
1614 rate
= ieee80211_htrates
[i
].ht20_rate_400ns
;
1616 rate
= ieee80211_htrates
[i
].ht20_rate_800ns
;
1623 /* XXX inline or eliminate? */
1624 const struct ieee80211_rateset
*
1625 ieee80211_get_suprates(struct ieee80211com
*ic
, const struct ieee80211_channel
*c
)
1627 /* XXX does this work for 11ng basic rates? */
1628 return &ic
->ic_sup_rates
[ieee80211_chan2mode(c
)];
1632 ieee80211_announce(struct ieee80211com
*ic
)
1635 enum ieee80211_phymode mode
;
1636 const struct ieee80211_rateset
*rs
;
1638 /* NB: skip AUTO since it has no rates */
1639 for (mode
= IEEE80211_MODE_AUTO
+1; mode
< IEEE80211_MODE_11NA
; mode
++) {
1640 if (isclr(ic
->ic_modecaps
, mode
))
1642 ic_printf(ic
, "%s rates: ", ieee80211_phymode_name
[mode
]);
1643 rs
= &ic
->ic_sup_rates
[mode
];
1644 for (i
= 0; i
< rs
->rs_nrates
; i
++) {
1645 mword
= ieee80211_rate2media(ic
, rs
->rs_rates
[i
], mode
);
1648 rate
= ieee80211_media2rate(mword
);
1649 kprintf("%s%d%sMbps", (i
!= 0 ? " " : ""),
1650 rate
/ 2, ((rate
& 0x1) != 0 ? ".5" : ""));
1654 ieee80211_ht_announce(ic
);
1658 ieee80211_announce_channels(struct ieee80211com
*ic
)
1660 const struct ieee80211_channel
*c
;
1664 kprintf("Chan Freq CW RegPwr MinPwr MaxPwr\n");
1665 for (i
= 0; i
< ic
->ic_nchans
; i
++) {
1666 c
= &ic
->ic_channels
[i
];
1667 if (IEEE80211_IS_CHAN_ST(c
))
1669 else if (IEEE80211_IS_CHAN_108A(c
))
1671 else if (IEEE80211_IS_CHAN_108G(c
))
1673 else if (IEEE80211_IS_CHAN_HT(c
))
1675 else if (IEEE80211_IS_CHAN_A(c
))
1677 else if (IEEE80211_IS_CHAN_ANYG(c
))
1679 else if (IEEE80211_IS_CHAN_B(c
))
1683 if (IEEE80211_IS_CHAN_HT40(c
) || IEEE80211_IS_CHAN_TURBO(c
))
1685 else if (IEEE80211_IS_CHAN_HALF(c
))
1687 else if (IEEE80211_IS_CHAN_QUARTER(c
))
1691 kprintf("%4d %4d%c %2d%c %6d %4d.%d %4d.%d\n"
1692 , c
->ic_ieee
, c
->ic_freq
, type
1694 , IEEE80211_IS_CHAN_HT40U(c
) ? '+' :
1695 IEEE80211_IS_CHAN_HT40D(c
) ? '-' : ' '
1697 , c
->ic_minpower
/ 2, c
->ic_minpower
& 1 ? 5 : 0
1698 , c
->ic_maxpower
/ 2, c
->ic_maxpower
& 1 ? 5 : 0
1704 media2mode(const struct ifmedia_entry
*ime
, uint32_t flags
, uint16_t *mode
)
1706 switch (IFM_MODE(ime
->ifm_media
)) {
1707 case IFM_IEEE80211_11A
:
1708 *mode
= IEEE80211_MODE_11A
;
1710 case IFM_IEEE80211_11B
:
1711 *mode
= IEEE80211_MODE_11B
;
1713 case IFM_IEEE80211_11G
:
1714 *mode
= IEEE80211_MODE_11G
;
1716 case IFM_IEEE80211_FH
:
1717 *mode
= IEEE80211_MODE_FH
;
1719 case IFM_IEEE80211_11NA
:
1720 *mode
= IEEE80211_MODE_11NA
;
1722 case IFM_IEEE80211_11NG
:
1723 *mode
= IEEE80211_MODE_11NG
;
1726 *mode
= IEEE80211_MODE_AUTO
;
1732 * Turbo mode is an ``option''.
1733 * XXX does not apply to AUTO
1735 if (ime
->ifm_media
& IFM_IEEE80211_TURBO
) {
1736 if (*mode
== IEEE80211_MODE_11A
) {
1737 if (flags
& IEEE80211_F_TURBOP
)
1738 *mode
= IEEE80211_MODE_TURBO_A
;
1740 *mode
= IEEE80211_MODE_STURBO_A
;
1741 } else if (*mode
== IEEE80211_MODE_11G
)
1742 *mode
= IEEE80211_MODE_TURBO_G
;
1751 * Handle a media change request on the vap interface.
1754 ieee80211_media_change(struct ifnet
*ifp
)
1756 struct ieee80211vap
*vap
= ifp
->if_softc
;
1757 struct ifmedia_entry
*ime
= vap
->iv_media
.ifm_cur
;
1760 if (!media2mode(ime
, vap
->iv_flags
, &newmode
))
1762 if (vap
->iv_des_mode
!= newmode
) {
1763 vap
->iv_des_mode
= newmode
;
1764 /* XXX kick state machine if up+running */
1770 * Common code to calculate the media status word
1771 * from the operating mode and channel state.
1774 media_status(enum ieee80211_opmode opmode
, const struct ieee80211_channel
*chan
)
1778 status
= IFM_IEEE80211
;
1780 case IEEE80211_M_STA
:
1782 case IEEE80211_M_IBSS
:
1783 status
|= IFM_IEEE80211_ADHOC
;
1785 case IEEE80211_M_HOSTAP
:
1786 status
|= IFM_IEEE80211_HOSTAP
;
1788 case IEEE80211_M_MONITOR
:
1789 status
|= IFM_IEEE80211_MONITOR
;
1791 case IEEE80211_M_AHDEMO
:
1792 status
|= IFM_IEEE80211_ADHOC
| IFM_FLAG0
;
1794 case IEEE80211_M_WDS
:
1795 status
|= IFM_IEEE80211_WDS
;
1797 case IEEE80211_M_MBSS
:
1798 status
|= IFM_IEEE80211_MBSS
;
1801 if (IEEE80211_IS_CHAN_HTA(chan
)) {
1802 status
|= IFM_IEEE80211_11NA
;
1803 } else if (IEEE80211_IS_CHAN_HTG(chan
)) {
1804 status
|= IFM_IEEE80211_11NG
;
1805 } else if (IEEE80211_IS_CHAN_A(chan
)) {
1806 status
|= IFM_IEEE80211_11A
;
1807 } else if (IEEE80211_IS_CHAN_B(chan
)) {
1808 status
|= IFM_IEEE80211_11B
;
1809 } else if (IEEE80211_IS_CHAN_ANYG(chan
)) {
1810 status
|= IFM_IEEE80211_11G
;
1811 } else if (IEEE80211_IS_CHAN_FHSS(chan
)) {
1812 status
|= IFM_IEEE80211_FH
;
1814 /* XXX else complain? */
1816 if (IEEE80211_IS_CHAN_TURBO(chan
))
1817 status
|= IFM_IEEE80211_TURBO
;
1819 if (IEEE80211_IS_CHAN_HT20(chan
))
1820 status
|= IFM_IEEE80211_HT20
;
1821 if (IEEE80211_IS_CHAN_HT40(chan
))
1822 status
|= IFM_IEEE80211_HT40
;
1828 ieee80211_media_status(struct ifnet
*ifp
, struct ifmediareq
*imr
)
1830 struct ieee80211vap
*vap
= ifp
->if_softc
;
1831 struct ieee80211com
*ic
= vap
->iv_ic
;
1832 enum ieee80211_phymode mode
;
1834 imr
->ifm_status
= IFM_AVALID
;
1836 * NB: use the current channel's mode to lock down a xmit
1837 * rate only when running; otherwise we may have a mismatch
1838 * in which case the rate will not be convertible.
1840 if (vap
->iv_state
== IEEE80211_S_RUN
||
1841 vap
->iv_state
== IEEE80211_S_SLEEP
) {
1842 imr
->ifm_status
|= IFM_ACTIVE
;
1843 mode
= ieee80211_chan2mode(ic
->ic_curchan
);
1845 mode
= IEEE80211_MODE_AUTO
;
1846 imr
->ifm_active
= media_status(vap
->iv_opmode
, ic
->ic_curchan
);
1848 * Calculate a current rate if possible.
1850 if (vap
->iv_txparms
[mode
].ucastrate
!= IEEE80211_FIXED_RATE_NONE
) {
1852 * A fixed rate is set, report that.
1854 imr
->ifm_active
|= ieee80211_rate2media(ic
,
1855 vap
->iv_txparms
[mode
].ucastrate
, mode
);
1856 } else if (vap
->iv_opmode
== IEEE80211_M_STA
) {
1858 * In station mode report the current transmit rate.
1860 imr
->ifm_active
|= ieee80211_rate2media(ic
,
1861 vap
->iv_bss
->ni_txrate
, mode
);
1863 imr
->ifm_active
|= IFM_AUTO
;
1864 if (imr
->ifm_status
& IFM_ACTIVE
)
1865 imr
->ifm_current
= imr
->ifm_active
;
1869 * Set the current phy mode and recalculate the active channel
1870 * set based on the available channels for this mode. Also
1871 * select a new default/current channel if the current one is
1872 * inappropriate for this mode.
1875 ieee80211_setmode(struct ieee80211com
*ic
, enum ieee80211_phymode mode
)
1878 * Adjust basic rates in 11b/11g supported rate set.
1879 * Note that if operating on a hal/quarter rate channel
1880 * this is a noop as those rates sets are different
1883 if (mode
== IEEE80211_MODE_11G
|| mode
== IEEE80211_MODE_11B
)
1884 ieee80211_setbasicrates(&ic
->ic_sup_rates
[mode
], mode
);
1886 ic
->ic_curmode
= mode
;
1887 ieee80211_reset_erp(ic
); /* reset ERP state */
1893 * Return the phy mode for with the specified channel.
1895 enum ieee80211_phymode
1896 ieee80211_chan2mode(const struct ieee80211_channel
*chan
)
1899 if (IEEE80211_IS_CHAN_HTA(chan
))
1900 return IEEE80211_MODE_11NA
;
1901 else if (IEEE80211_IS_CHAN_HTG(chan
))
1902 return IEEE80211_MODE_11NG
;
1903 else if (IEEE80211_IS_CHAN_108G(chan
))
1904 return IEEE80211_MODE_TURBO_G
;
1905 else if (IEEE80211_IS_CHAN_ST(chan
))
1906 return IEEE80211_MODE_STURBO_A
;
1907 else if (IEEE80211_IS_CHAN_TURBO(chan
))
1908 return IEEE80211_MODE_TURBO_A
;
1909 else if (IEEE80211_IS_CHAN_HALF(chan
))
1910 return IEEE80211_MODE_HALF
;
1911 else if (IEEE80211_IS_CHAN_QUARTER(chan
))
1912 return IEEE80211_MODE_QUARTER
;
1913 else if (IEEE80211_IS_CHAN_A(chan
))
1914 return IEEE80211_MODE_11A
;
1915 else if (IEEE80211_IS_CHAN_ANYG(chan
))
1916 return IEEE80211_MODE_11G
;
1917 else if (IEEE80211_IS_CHAN_B(chan
))
1918 return IEEE80211_MODE_11B
;
1919 else if (IEEE80211_IS_CHAN_FHSS(chan
))
1920 return IEEE80211_MODE_FH
;
1922 /* NB: should not get here */
1923 kprintf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
1924 __func__
, chan
->ic_freq
, chan
->ic_flags
);
1925 return IEEE80211_MODE_11B
;
1929 u_int match
; /* rate + mode */
1930 u_int media
; /* if_media rate */
1934 findmedia(const struct ratemedia rates
[], int n
, u_int match
)
1938 for (i
= 0; i
< n
; i
++)
1939 if (rates
[i
].match
== match
)
1940 return rates
[i
].media
;
1945 * Convert IEEE80211 rate value to ifmedia subtype.
1946 * Rate is either a legacy rate in units of 0.5Mbps
1950 ieee80211_rate2media(struct ieee80211com
*ic
, int rate
, enum ieee80211_phymode mode
)
1952 static const struct ratemedia rates
[] = {
1953 { 2 | IFM_IEEE80211_FH
, IFM_IEEE80211_FH1
},
1954 { 4 | IFM_IEEE80211_FH
, IFM_IEEE80211_FH2
},
1955 { 2 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS1
},
1956 { 4 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS2
},
1957 { 11 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS5
},
1958 { 22 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS11
},
1959 { 44 | IFM_IEEE80211_11B
, IFM_IEEE80211_DS22
},
1960 { 12 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM6
},
1961 { 18 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM9
},
1962 { 24 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM12
},
1963 { 36 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM18
},
1964 { 48 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM24
},
1965 { 72 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM36
},
1966 { 96 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM48
},
1967 { 108 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM54
},
1968 { 2 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS1
},
1969 { 4 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS2
},
1970 { 11 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS5
},
1971 { 22 | IFM_IEEE80211_11G
, IFM_IEEE80211_DS11
},
1972 { 12 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM6
},
1973 { 18 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM9
},
1974 { 24 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM12
},
1975 { 36 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM18
},
1976 { 48 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM24
},
1977 { 72 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM36
},
1978 { 96 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM48
},
1979 { 108 | IFM_IEEE80211_11G
, IFM_IEEE80211_OFDM54
},
1980 { 6 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM3
},
1981 { 9 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM4
},
1982 { 54 | IFM_IEEE80211_11A
, IFM_IEEE80211_OFDM27
},
1983 /* NB: OFDM72 doesn't really exist so we don't handle it */
1985 static const struct ratemedia htrates
[] = {
1986 { 0, IFM_IEEE80211_MCS
},
1987 { 1, IFM_IEEE80211_MCS
},
1988 { 2, IFM_IEEE80211_MCS
},
1989 { 3, IFM_IEEE80211_MCS
},
1990 { 4, IFM_IEEE80211_MCS
},
1991 { 5, IFM_IEEE80211_MCS
},
1992 { 6, IFM_IEEE80211_MCS
},
1993 { 7, IFM_IEEE80211_MCS
},
1994 { 8, IFM_IEEE80211_MCS
},
1995 { 9, IFM_IEEE80211_MCS
},
1996 { 10, IFM_IEEE80211_MCS
},
1997 { 11, IFM_IEEE80211_MCS
},
1998 { 12, IFM_IEEE80211_MCS
},
1999 { 13, IFM_IEEE80211_MCS
},
2000 { 14, IFM_IEEE80211_MCS
},
2001 { 15, IFM_IEEE80211_MCS
},
2002 { 16, IFM_IEEE80211_MCS
},
2003 { 17, IFM_IEEE80211_MCS
},
2004 { 18, IFM_IEEE80211_MCS
},
2005 { 19, IFM_IEEE80211_MCS
},
2006 { 20, IFM_IEEE80211_MCS
},
2007 { 21, IFM_IEEE80211_MCS
},
2008 { 22, IFM_IEEE80211_MCS
},
2009 { 23, IFM_IEEE80211_MCS
},
2010 { 24, IFM_IEEE80211_MCS
},
2011 { 25, IFM_IEEE80211_MCS
},
2012 { 26, IFM_IEEE80211_MCS
},
2013 { 27, IFM_IEEE80211_MCS
},
2014 { 28, IFM_IEEE80211_MCS
},
2015 { 29, IFM_IEEE80211_MCS
},
2016 { 30, IFM_IEEE80211_MCS
},
2017 { 31, IFM_IEEE80211_MCS
},
2018 { 32, IFM_IEEE80211_MCS
},
2019 { 33, IFM_IEEE80211_MCS
},
2020 { 34, IFM_IEEE80211_MCS
},
2021 { 35, IFM_IEEE80211_MCS
},
2022 { 36, IFM_IEEE80211_MCS
},
2023 { 37, IFM_IEEE80211_MCS
},
2024 { 38, IFM_IEEE80211_MCS
},
2025 { 39, IFM_IEEE80211_MCS
},
2026 { 40, IFM_IEEE80211_MCS
},
2027 { 41, IFM_IEEE80211_MCS
},
2028 { 42, IFM_IEEE80211_MCS
},
2029 { 43, IFM_IEEE80211_MCS
},
2030 { 44, IFM_IEEE80211_MCS
},
2031 { 45, IFM_IEEE80211_MCS
},
2032 { 46, IFM_IEEE80211_MCS
},
2033 { 47, IFM_IEEE80211_MCS
},
2034 { 48, IFM_IEEE80211_MCS
},
2035 { 49, IFM_IEEE80211_MCS
},
2036 { 50, IFM_IEEE80211_MCS
},
2037 { 51, IFM_IEEE80211_MCS
},
2038 { 52, IFM_IEEE80211_MCS
},
2039 { 53, IFM_IEEE80211_MCS
},
2040 { 54, IFM_IEEE80211_MCS
},
2041 { 55, IFM_IEEE80211_MCS
},
2042 { 56, IFM_IEEE80211_MCS
},
2043 { 57, IFM_IEEE80211_MCS
},
2044 { 58, IFM_IEEE80211_MCS
},
2045 { 59, IFM_IEEE80211_MCS
},
2046 { 60, IFM_IEEE80211_MCS
},
2047 { 61, IFM_IEEE80211_MCS
},
2048 { 62, IFM_IEEE80211_MCS
},
2049 { 63, IFM_IEEE80211_MCS
},
2050 { 64, IFM_IEEE80211_MCS
},
2051 { 65, IFM_IEEE80211_MCS
},
2052 { 66, IFM_IEEE80211_MCS
},
2053 { 67, IFM_IEEE80211_MCS
},
2054 { 68, IFM_IEEE80211_MCS
},
2055 { 69, IFM_IEEE80211_MCS
},
2056 { 70, IFM_IEEE80211_MCS
},
2057 { 71, IFM_IEEE80211_MCS
},
2058 { 72, IFM_IEEE80211_MCS
},
2059 { 73, IFM_IEEE80211_MCS
},
2060 { 74, IFM_IEEE80211_MCS
},
2061 { 75, IFM_IEEE80211_MCS
},
2062 { 76, IFM_IEEE80211_MCS
},
2067 * Check 11n rates first for match as an MCS.
2069 if (mode
== IEEE80211_MODE_11NA
) {
2070 if (rate
& IEEE80211_RATE_MCS
) {
2071 rate
&= ~IEEE80211_RATE_MCS
;
2072 m
= findmedia(htrates
, nitems(htrates
), rate
);
2074 return m
| IFM_IEEE80211_11NA
;
2076 } else if (mode
== IEEE80211_MODE_11NG
) {
2077 /* NB: 12 is ambiguous, it will be treated as an MCS */
2078 if (rate
& IEEE80211_RATE_MCS
) {
2079 rate
&= ~IEEE80211_RATE_MCS
;
2080 m
= findmedia(htrates
, nitems(htrates
), rate
);
2082 return m
| IFM_IEEE80211_11NG
;
2085 rate
&= IEEE80211_RATE_VAL
;
2087 case IEEE80211_MODE_11A
:
2088 case IEEE80211_MODE_HALF
: /* XXX good 'nuf */
2089 case IEEE80211_MODE_QUARTER
:
2090 case IEEE80211_MODE_11NA
:
2091 case IEEE80211_MODE_TURBO_A
:
2092 case IEEE80211_MODE_STURBO_A
:
2093 return findmedia(rates
, nitems(rates
),
2094 rate
| IFM_IEEE80211_11A
);
2095 case IEEE80211_MODE_11B
:
2096 return findmedia(rates
, nitems(rates
),
2097 rate
| IFM_IEEE80211_11B
);
2098 case IEEE80211_MODE_FH
:
2099 return findmedia(rates
, nitems(rates
),
2100 rate
| IFM_IEEE80211_FH
);
2101 case IEEE80211_MODE_AUTO
:
2102 /* NB: ic may be NULL for some drivers */
2103 if (ic
!= NULL
&& ic
->ic_phytype
== IEEE80211_T_FH
)
2104 return findmedia(rates
, nitems(rates
),
2105 rate
| IFM_IEEE80211_FH
);
2106 /* NB: hack, 11g matches both 11b+11a rates */
2108 case IEEE80211_MODE_11G
:
2109 case IEEE80211_MODE_11NG
:
2110 case IEEE80211_MODE_TURBO_G
:
2111 return findmedia(rates
, nitems(rates
), rate
| IFM_IEEE80211_11G
);
2112 case IEEE80211_MODE_VHT_2GHZ
:
2113 case IEEE80211_MODE_VHT_5GHZ
:
2114 /* XXX TODO: need to figure out mapping for VHT rates */
2121 ieee80211_media2rate(int mword
)
2123 static const int ieeerates
[] = {
2127 2, /* IFM_IEEE80211_FH1 */
2128 4, /* IFM_IEEE80211_FH2 */
2129 2, /* IFM_IEEE80211_DS1 */
2130 4, /* IFM_IEEE80211_DS2 */
2131 11, /* IFM_IEEE80211_DS5 */
2132 22, /* IFM_IEEE80211_DS11 */
2133 44, /* IFM_IEEE80211_DS22 */
2134 12, /* IFM_IEEE80211_OFDM6 */
2135 18, /* IFM_IEEE80211_OFDM9 */
2136 24, /* IFM_IEEE80211_OFDM12 */
2137 36, /* IFM_IEEE80211_OFDM18 */
2138 48, /* IFM_IEEE80211_OFDM24 */
2139 72, /* IFM_IEEE80211_OFDM36 */
2140 96, /* IFM_IEEE80211_OFDM48 */
2141 108, /* IFM_IEEE80211_OFDM54 */
2142 144, /* IFM_IEEE80211_OFDM72 */
2143 0, /* IFM_IEEE80211_DS354k */
2144 0, /* IFM_IEEE80211_DS512k */
2145 6, /* IFM_IEEE80211_OFDM3 */
2146 9, /* IFM_IEEE80211_OFDM4 */
2147 54, /* IFM_IEEE80211_OFDM27 */
2148 -1, /* IFM_IEEE80211_MCS */
2150 return IFM_SUBTYPE(mword
) < nitems(ieeerates
) ?
2151 ieeerates
[IFM_SUBTYPE(mword
)] : 0;
2155 * The following hash function is adapted from "Hash Functions" by Bob Jenkins
2156 * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
2158 #define mix(a, b, c) \
2160 a -= b; a -= c; a ^= (c >> 13); \
2161 b -= c; b -= a; b ^= (a << 8); \
2162 c -= a; c -= b; c ^= (b >> 13); \
2163 a -= b; a -= c; a ^= (c >> 12); \
2164 b -= c; b -= a; b ^= (a << 16); \
2165 c -= a; c -= b; c ^= (b >> 5); \
2166 a -= b; a -= c; a ^= (c >> 3); \
2167 b -= c; b -= a; b ^= (a << 10); \
2168 c -= a; c -= b; c ^= (b >> 15); \
2169 } while (/*CONSTCOND*/0)
2172 ieee80211_mac_hash(const struct ieee80211com
*ic
,
2173 const uint8_t addr
[IEEE80211_ADDR_LEN
])
2175 uint32_t a
= 0x9e3779b9, b
= 0x9e3779b9, c
= ic
->ic_hash_key
;
2191 ieee80211_channel_type_char(const struct ieee80211_channel
*c
)
2193 if (IEEE80211_IS_CHAN_ST(c
))
2195 if (IEEE80211_IS_CHAN_108A(c
))
2197 if (IEEE80211_IS_CHAN_108G(c
))
2199 if (IEEE80211_IS_CHAN_HT(c
))
2201 if (IEEE80211_IS_CHAN_A(c
))
2203 if (IEEE80211_IS_CHAN_ANYG(c
))
2205 if (IEEE80211_IS_CHAN_B(c
))