Home | History | Annotate | Line # | Download | only in net80211
ieee80211.c revision 1.56.18.9
      1  1.56.18.9       nat /*	$NetBSD: ieee80211.c,v 1.56.18.9 2020/04/16 15:30:00 nat Exp $ */
      2  1.56.18.2      phil 
      3        1.1    dyoung /*-
      4  1.56.18.1      phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  1.56.18.1      phil  *
      6        1.1    dyoung  * Copyright (c) 2001 Atsushi Onoe
      7  1.56.18.1      phil  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
      8        1.1    dyoung  * All rights reserved.
      9        1.1    dyoung  *
     10        1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     11        1.1    dyoung  * modification, are permitted provided that the following conditions
     12        1.1    dyoung  * are met:
     13        1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     14        1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     15        1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     17        1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     18        1.1    dyoung  *
     19        1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20        1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21        1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22        1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23        1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24        1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25        1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26        1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27        1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28        1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29        1.1    dyoung  */
     30        1.1    dyoung 
     31        1.1    dyoung #include <sys/cdefs.h>
     32  1.56.18.7  christos #ifdef __NetBSD__
     33  1.56.18.9       nat __KERNEL_RCSID(0, "$NetBSD: ieee80211.c,v 1.56.18.9 2020/04/16 15:30:00 nat Exp $");
     34  1.56.18.2      phil #endif
     35        1.1    dyoung 
     36        1.1    dyoung /*
     37        1.1    dyoung  * IEEE 802.11 generic handler
     38        1.1    dyoung  */
     39  1.56.18.7  christos #ifdef _KERNEL_OPT
     40  1.56.18.1      phil #include "opt_wlan.h"
     41  1.56.18.7  christos #endif
     42        1.1    dyoung 
     43        1.1    dyoung #include <sys/param.h>
     44  1.56.18.1      phil #include <sys/systm.h>
     45        1.1    dyoung #include <sys/kernel.h>
     46  1.56.18.1      phil #include <sys/malloc.h>
     47        1.1    dyoung #include <sys/socket.h>
     48  1.56.18.1      phil #include <sys/sbuf.h>
     49  1.56.18.1      phil 
     50  1.56.18.2      phil #ifdef __FreeBSD__
     51  1.56.18.1      phil #include <machine/stdarg.h>
     52  1.56.18.2      phil #elif __NetBSD__
     53  1.56.18.4      phil #include <sys/once.h>
     54  1.56.18.2      phil #include <sys/stdarg.h>
     55  1.56.18.2      phil #else
     56  1.56.18.2      phil #error
     57  1.56.18.2      phil #endif
     58        1.1    dyoung 
     59        1.1    dyoung #include <net/if.h>
     60  1.56.18.2      phil #ifdef __FreeBSD__
     61  1.56.18.1      phil #include <net/if_var.h>
     62  1.56.18.2      phil #endif
     63  1.56.18.1      phil #include <net/if_dl.h>
     64        1.1    dyoung #include <net/if_media.h>
     65  1.56.18.1      phil #include <net/if_types.h>
     66  1.56.18.2      phil #ifdef __FreeBSD__
     67  1.56.18.1      phil #include <net/ethernet.h>
     68  1.56.18.2      phil #endif
     69  1.56.18.2      phil #ifdef __NetBSD__
     70  1.56.18.2      phil #include <net/route.h>
     71  1.56.18.2      phil #include <net/if_ether.h>
     72  1.56.18.2      phil #endif
     73        1.1    dyoung 
     74        1.1    dyoung #include <net80211/ieee80211_var.h>
     75  1.56.18.1      phil #include <net80211/ieee80211_regdomain.h>
     76  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
     77  1.56.18.1      phil #include <net80211/ieee80211_superg.h>
     78  1.56.18.1      phil #endif
     79  1.56.18.1      phil #include <net80211/ieee80211_ratectl.h>
     80  1.56.18.1      phil #include <net80211/ieee80211_vht.h>
     81        1.1    dyoung 
     82        1.1    dyoung #include <net/bpf.h>
     83        1.1    dyoung 
     84  1.56.18.2      phil #ifdef __NetBSD__
     85  1.56.18.2      phil #undef  KASSERT
     86  1.56.18.2      phil #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     87  1.56.18.2      phil #endif
     88  1.56.18.2      phil 
     89  1.56.18.1      phil const char *ieee80211_phymode_name[IEEE80211_MODE_MAX] = {
     90  1.56.18.1      phil 	[IEEE80211_MODE_AUTO]	  = "auto",
     91  1.56.18.1      phil 	[IEEE80211_MODE_11A]	  = "11a",
     92  1.56.18.1      phil 	[IEEE80211_MODE_11B]	  = "11b",
     93  1.56.18.1      phil 	[IEEE80211_MODE_11G]	  = "11g",
     94  1.56.18.1      phil 	[IEEE80211_MODE_FH]	  = "FH",
     95  1.56.18.1      phil 	[IEEE80211_MODE_TURBO_A]  = "turboA",
     96  1.56.18.1      phil 	[IEEE80211_MODE_TURBO_G]  = "turboG",
     97  1.56.18.1      phil 	[IEEE80211_MODE_STURBO_A] = "sturboA",
     98  1.56.18.1      phil 	[IEEE80211_MODE_HALF]	  = "half",
     99  1.56.18.1      phil 	[IEEE80211_MODE_QUARTER]  = "quarter",
    100  1.56.18.1      phil 	[IEEE80211_MODE_11NA]	  = "11na",
    101  1.56.18.1      phil 	[IEEE80211_MODE_11NG]	  = "11ng",
    102  1.56.18.1      phil 	[IEEE80211_MODE_VHT_2GHZ]	  = "11acg",
    103  1.56.18.1      phil 	[IEEE80211_MODE_VHT_5GHZ]	  = "11ac",
    104  1.56.18.1      phil };
    105  1.56.18.1      phil /* map ieee80211_opmode to the corresponding capability bit */
    106  1.56.18.1      phil const int ieee80211_opcap[IEEE80211_OPMODE_MAX] = {
    107  1.56.18.1      phil 	[IEEE80211_M_IBSS]	= IEEE80211_C_IBSS,
    108  1.56.18.1      phil 	[IEEE80211_M_WDS]	= IEEE80211_C_WDS,
    109  1.56.18.1      phil 	[IEEE80211_M_STA]	= IEEE80211_C_STA,
    110  1.56.18.1      phil 	[IEEE80211_M_AHDEMO]	= IEEE80211_C_AHDEMO,
    111  1.56.18.1      phil 	[IEEE80211_M_HOSTAP]	= IEEE80211_C_HOSTAP,
    112  1.56.18.1      phil 	[IEEE80211_M_MONITOR]	= IEEE80211_C_MONITOR,
    113  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_MESH
    114  1.56.18.1      phil 	[IEEE80211_M_MBSS]	= IEEE80211_C_MBSS,
    115        1.4    dyoung #endif
    116       1.54  christos };
    117       1.54  christos 
    118  1.56.18.1      phil const uint8_t ieee80211broadcastaddr[IEEE80211_ADDR_LEN] =
    119  1.56.18.1      phil 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
    120       1.20    dyoung 
    121  1.56.18.1      phil static	void ieee80211_syncflag_locked(struct ieee80211com *ic, int flag);
    122  1.56.18.1      phil static	void ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag);
    123  1.56.18.1      phil static	void ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag);
    124  1.56.18.1      phil static	void ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag);
    125  1.56.18.1      phil static	int ieee80211_media_setup(struct ieee80211com *ic,
    126  1.56.18.1      phil 		struct ifmedia *media, int caps, int addsta,
    127  1.56.18.1      phil 		ifm_change_cb_t media_change, ifm_stat_cb_t media_stat);
    128  1.56.18.1      phil static	int media_status(enum ieee80211_opmode,
    129  1.56.18.1      phil 		const struct ieee80211_channel *);
    130  1.56.18.1      phil static uint64_t ieee80211_get_counter(struct ifnet *, ift_counter);
    131        1.1    dyoung 
    132  1.56.18.2      phil 
    133  1.56.18.1      phil MALLOC_DEFINE(M_80211_VAP, "80211vap", "802.11 vap state");
    134       1.37    dyoung 
    135  1.56.18.1      phil /*
    136  1.56.18.1      phil  * Default supported rates for 802.11 operation (in IEEE .5Mb units).
    137  1.56.18.1      phil  */
    138  1.56.18.1      phil #define	B(r)	((r) | IEEE80211_RATE_BASIC)
    139  1.56.18.1      phil static const struct ieee80211_rateset ieee80211_rateset_11a =
    140  1.56.18.1      phil 	{ 8, { B(12), 18, B(24), 36, B(48), 72, 96, 108 } };
    141  1.56.18.1      phil static const struct ieee80211_rateset ieee80211_rateset_half =
    142  1.56.18.1      phil 	{ 8, { B(6), 9, B(12), 18, B(24), 36, 48, 54 } };
    143  1.56.18.1      phil static const struct ieee80211_rateset ieee80211_rateset_quarter =
    144  1.56.18.1      phil 	{ 8, { B(3), 4, B(6), 9, B(12), 18, 24, 27 } };
    145  1.56.18.1      phil static const struct ieee80211_rateset ieee80211_rateset_11b =
    146  1.56.18.1      phil 	{ 4, { B(2), B(4), B(11), B(22) } };
    147  1.56.18.1      phil /* NB: OFDM rates are handled specially based on mode */
    148  1.56.18.1      phil static const struct ieee80211_rateset ieee80211_rateset_11g =
    149  1.56.18.1      phil 	{ 12, { B(2), B(4), B(11), B(22), 12, 18, 24, 36, 48, 72, 96, 108 } };
    150  1.56.18.1      phil #undef B
    151  1.56.18.1      phil 
    152  1.56.18.1      phil static int set_vht_extchan(struct ieee80211_channel *c);
    153  1.56.18.1      phil 
    154  1.56.18.1      phil /*
    155  1.56.18.1      phil  * Fill in 802.11 available channel set, mark
    156  1.56.18.1      phil  * all available channels as active, and pick
    157  1.56.18.1      phil  * a default channel if not already specified.
    158  1.56.18.1      phil  */
    159  1.56.18.1      phil void
    160  1.56.18.1      phil ieee80211_chan_init(struct ieee80211com *ic)
    161       1.37    dyoung {
    162  1.56.18.1      phil #define	DEFAULTRATES(m, def) do { \
    163  1.56.18.1      phil 	if (ic->ic_sup_rates[m].rs_nrates == 0) \
    164  1.56.18.1      phil 		ic->ic_sup_rates[m] = def; \
    165  1.56.18.1      phil } while (0)
    166  1.56.18.1      phil 	struct ieee80211_channel *c;
    167       1.37    dyoung 	int i;
    168       1.37    dyoung 
    169  1.56.18.1      phil 	KASSERT(0 < ic->ic_nchans && ic->ic_nchans <= IEEE80211_CHAN_MAX,
    170  1.56.18.1      phil 		("invalid number of channels specified: %u", ic->ic_nchans));
    171  1.56.18.1      phil 	memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
    172  1.56.18.1      phil 	memset(ic->ic_modecaps, 0, sizeof(ic->ic_modecaps));
    173  1.56.18.1      phil 	setbit(ic->ic_modecaps, IEEE80211_MODE_AUTO);
    174  1.56.18.1      phil 	for (i = 0; i < ic->ic_nchans; i++) {
    175  1.56.18.1      phil 		c = &ic->ic_channels[i];
    176  1.56.18.1      phil 		KASSERT(c->ic_flags != 0, ("channel with no flags"));
    177  1.56.18.1      phil 		/*
    178  1.56.18.1      phil 		 * Help drivers that work only with frequencies by filling
    179  1.56.18.1      phil 		 * in IEEE channel #'s if not already calculated.  Note this
    180  1.56.18.1      phil 		 * mimics similar work done in ieee80211_setregdomain when
    181  1.56.18.1      phil 		 * changing regulatory state.
    182  1.56.18.1      phil 		 */
    183  1.56.18.1      phil 		if (c->ic_ieee == 0)
    184  1.56.18.1      phil 			c->ic_ieee = ieee80211_mhz2ieee(c->ic_freq,c->ic_flags);
    185  1.56.18.1      phil 
    186  1.56.18.1      phil 		/*
    187  1.56.18.1      phil 		 * Setup the HT40/VHT40 upper/lower bits.
    188  1.56.18.1      phil 		 * The VHT80 math is done elsewhere.
    189  1.56.18.1      phil 		 */
    190  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_HT40(c) && c->ic_extieee == 0)
    191  1.56.18.1      phil 			c->ic_extieee = ieee80211_mhz2ieee(c->ic_freq +
    192  1.56.18.1      phil 			    (IEEE80211_IS_CHAN_HT40U(c) ? 20 : -20),
    193  1.56.18.1      phil 			    c->ic_flags);
    194  1.56.18.1      phil 
    195  1.56.18.1      phil 		/* Update VHT math */
    196  1.56.18.1      phil 		/*
    197  1.56.18.1      phil 		 * XXX VHT again, note that this assumes VHT80 channels
    198  1.56.18.1      phil 		 * are legit already
    199  1.56.18.1      phil 		 */
    200  1.56.18.1      phil 		set_vht_extchan(c);
    201  1.56.18.1      phil 
    202  1.56.18.1      phil 		/* default max tx power to max regulatory */
    203  1.56.18.1      phil 		if (c->ic_maxpower == 0)
    204  1.56.18.1      phil 			c->ic_maxpower = 2*c->ic_maxregpower;
    205  1.56.18.1      phil 		setbit(ic->ic_chan_avail, c->ic_ieee);
    206  1.56.18.1      phil 		/*
    207  1.56.18.1      phil 		 * Identify mode capabilities.
    208  1.56.18.1      phil 		 */
    209  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_A(c))
    210  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_11A);
    211  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_B(c))
    212  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_11B);
    213  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_ANYG(c))
    214  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_11G);
    215  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_FHSS(c))
    216  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_FH);
    217  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_108A(c))
    218  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_A);
    219  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_108G(c))
    220  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_TURBO_G);
    221  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_ST(c))
    222  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_STURBO_A);
    223  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_HALF(c))
    224  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_HALF);
    225  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_QUARTER(c))
    226  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_QUARTER);
    227  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_HTA(c))
    228  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NA);
    229  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_HTG(c))
    230  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_11NG);
    231  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_VHTA(c))
    232  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_5GHZ);
    233  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_VHTG(c))
    234  1.56.18.1      phil 			setbit(ic->ic_modecaps, IEEE80211_MODE_VHT_2GHZ);
    235  1.56.18.1      phil 	}
    236  1.56.18.1      phil 	/* initialize candidate channels to all available */
    237  1.56.18.1      phil 	memcpy(ic->ic_chan_active, ic->ic_chan_avail,
    238  1.56.18.1      phil 		sizeof(ic->ic_chan_avail));
    239  1.56.18.1      phil 
    240  1.56.18.1      phil 	/* sort channel table to allow lookup optimizations */
    241  1.56.18.1      phil 	ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans);
    242  1.56.18.1      phil 
    243  1.56.18.1      phil 	/* invalidate any previous state */
    244  1.56.18.1      phil 	ic->ic_bsschan = IEEE80211_CHAN_ANYC;
    245  1.56.18.1      phil 	ic->ic_prevchan = NULL;
    246  1.56.18.1      phil 	ic->ic_csa_newchan = NULL;
    247  1.56.18.1      phil 	/* arbitrarily pick the first channel */
    248  1.56.18.1      phil 	ic->ic_curchan = &ic->ic_channels[0];
    249  1.56.18.1      phil 	ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
    250  1.56.18.1      phil 
    251  1.56.18.1      phil 	/* fillin well-known rate sets if driver has not specified */
    252  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_11B,	 ieee80211_rateset_11b);
    253  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_11G,	 ieee80211_rateset_11g);
    254  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_11A,	 ieee80211_rateset_11a);
    255  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_TURBO_A,	 ieee80211_rateset_11a);
    256  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_TURBO_G,	 ieee80211_rateset_11g);
    257  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_STURBO_A,	 ieee80211_rateset_11a);
    258  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_HALF,	 ieee80211_rateset_half);
    259  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_QUARTER,	 ieee80211_rateset_quarter);
    260  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_11NA,	 ieee80211_rateset_11a);
    261  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_11NG,	 ieee80211_rateset_11g);
    262  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_VHT_2GHZ,	 ieee80211_rateset_11g);
    263  1.56.18.1      phil 	DEFAULTRATES(IEEE80211_MODE_VHT_5GHZ,	 ieee80211_rateset_11a);
    264  1.56.18.1      phil 
    265  1.56.18.1      phil 	/*
    266  1.56.18.1      phil 	 * Setup required information to fill the mcsset field, if driver did
    267  1.56.18.1      phil 	 * not. Assume a 2T2R setup for historic reasons.
    268  1.56.18.1      phil 	 */
    269  1.56.18.1      phil 	if (ic->ic_rxstream == 0)
    270  1.56.18.1      phil 		ic->ic_rxstream = 2;
    271  1.56.18.1      phil 	if (ic->ic_txstream == 0)
    272  1.56.18.1      phil 		ic->ic_txstream = 2;
    273  1.56.18.1      phil 
    274  1.56.18.1      phil 	ieee80211_init_suphtrates(ic);
    275  1.56.18.1      phil 
    276  1.56.18.1      phil 	/*
    277  1.56.18.1      phil 	 * Set auto mode to reset active channel state and any desired channel.
    278  1.56.18.1      phil 	 */
    279  1.56.18.1      phil 	(void) ieee80211_setmode(ic, IEEE80211_MODE_AUTO);
    280  1.56.18.1      phil #undef DEFAULTRATES
    281  1.56.18.1      phil }
    282  1.56.18.1      phil 
    283  1.56.18.1      phil static void
    284  1.56.18.1      phil null_update_mcast(struct ieee80211com *ic)
    285  1.56.18.1      phil {
    286  1.56.18.1      phil 
    287  1.56.18.1      phil 	ic_printf(ic, "need multicast update callback\n");
    288  1.56.18.1      phil }
    289  1.56.18.1      phil 
    290  1.56.18.1      phil static void
    291  1.56.18.1      phil null_update_promisc(struct ieee80211com *ic)
    292  1.56.18.1      phil {
    293  1.56.18.1      phil 
    294  1.56.18.1      phil 	ic_printf(ic, "need promiscuous mode update callback\n");
    295       1.37    dyoung }
    296       1.37    dyoung 
    297       1.37    dyoung static void
    298  1.56.18.1      phil null_update_chw(struct ieee80211com *ic)
    299  1.56.18.1      phil {
    300  1.56.18.1      phil 
    301  1.56.18.1      phil 	ic_printf(ic, "%s: need callback\n", __func__);
    302  1.56.18.1      phil }
    303  1.56.18.1      phil 
    304  1.56.18.2      phil #ifdef __FreeBSD__
    305  1.56.18.1      phil int
    306  1.56.18.1      phil ic_printf(struct ieee80211com *ic, const char * fmt, ...)
    307  1.56.18.1      phil {
    308  1.56.18.1      phil 	va_list ap;
    309  1.56.18.1      phil 	int retval;
    310  1.56.18.1      phil 
    311  1.56.18.1      phil 	retval = printf("%s: ", ic->ic_name);
    312  1.56.18.1      phil 	va_start(ap, fmt);
    313  1.56.18.1      phil 	retval += vprintf(fmt, ap);
    314  1.56.18.1      phil 	va_end(ap);
    315  1.56.18.1      phil 	return (retval);
    316  1.56.18.1      phil }
    317  1.56.18.2      phil #elif __NetBSD__
    318  1.56.18.2      phil void
    319  1.56.18.2      phil ic_printf(struct ieee80211com *ic, const char * fmt, ...)
    320  1.56.18.2      phil {
    321  1.56.18.2      phil 	va_list ap;
    322  1.56.18.2      phil 
    323  1.56.18.2      phil 	printf("%s: ", ic->ic_name);
    324  1.56.18.2      phil 	va_start(ap, fmt);
    325  1.56.18.2      phil 	vprintf(fmt, ap);
    326  1.56.18.2      phil 	va_end(ap);
    327  1.56.18.2      phil }
    328  1.56.18.2      phil #endif
    329  1.56.18.1      phil 
    330  1.56.18.1      phil static LIST_HEAD(, ieee80211com) ic_head = LIST_HEAD_INITIALIZER(ic_head);
    331  1.56.18.2      phil #ifdef __FreeBSD__
    332  1.56.18.1      phil static struct mtx ic_list_mtx;
    333  1.56.18.1      phil MTX_SYSINIT(ic_list, &ic_list_mtx, "ieee80211com list", MTX_DEF);
    334  1.56.18.2      phil #elif __NetBSD__
    335  1.56.18.2      phil static kmutex_t ic_list_mtx;
    336  1.56.18.2      phil #endif
    337  1.56.18.1      phil 
    338  1.56.18.2      phil #if notyet
    339  1.56.18.1      phil static int
    340  1.56.18.1      phil sysctl_ieee80211coms(SYSCTL_HANDLER_ARGS)
    341  1.56.18.1      phil {
    342  1.56.18.1      phil 	struct ieee80211com *ic;
    343  1.56.18.1      phil 	struct sbuf sb;
    344  1.56.18.1      phil 	char *sp;
    345  1.56.18.1      phil 	int error;
    346  1.56.18.1      phil 
    347  1.56.18.1      phil 	error = sysctl_wire_old_buffer(req, 0);
    348  1.56.18.1      phil 	if (error)
    349  1.56.18.1      phil 		return (error);
    350  1.56.18.1      phil 	sbuf_new_for_sysctl(&sb, NULL, 8, req);
    351  1.56.18.1      phil 	sbuf_clear_flags(&sb, SBUF_INCLUDENUL);
    352  1.56.18.1      phil 	sp = "";
    353  1.56.18.1      phil 	mtx_lock(&ic_list_mtx);
    354  1.56.18.1      phil 	LIST_FOREACH(ic, &ic_head, ic_next) {
    355  1.56.18.1      phil 		sbuf_printf(&sb, "%s%s", sp, ic->ic_name);
    356  1.56.18.1      phil 		sp = " ";
    357  1.56.18.1      phil 	}
    358  1.56.18.1      phil 	mtx_unlock(&ic_list_mtx);
    359  1.56.18.1      phil 	error = sbuf_finish(&sb);
    360  1.56.18.1      phil 	sbuf_delete(&sb);
    361  1.56.18.1      phil 	return (error);
    362  1.56.18.1      phil }
    363  1.56.18.1      phil 
    364  1.56.18.1      phil SYSCTL_PROC(_net_wlan, OID_AUTO, devices,
    365  1.56.18.1      phil     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
    366  1.56.18.1      phil     sysctl_ieee80211coms, "A", "names of available 802.11 devices");
    367  1.56.18.2      phil #endif
    368  1.56.18.1      phil 
    369  1.56.18.4      phil #if __NetBSD__
    370  1.56.18.4      phil static int
    371  1.56.18.4      phil ic_list_mtx_init (void)
    372  1.56.18.4      phil {
    373  1.56.18.4      phil 	mutex_init(&ic_list_mtx, MUTEX_DEFAULT, IPL_NET);
    374  1.56.18.4      phil 	ieee80211_auth_setup();
    375  1.56.18.4      phil 	return 0;
    376  1.56.18.4      phil }
    377  1.56.18.4      phil #endif
    378  1.56.18.4      phil 
    379  1.56.18.1      phil /*
    380  1.56.18.1      phil  * Attach/setup the common net80211 state.  Called by
    381  1.56.18.1      phil  * the driver on attach to prior to creating any vap's.
    382  1.56.18.1      phil  */
    383  1.56.18.1      phil void
    384  1.56.18.1      phil ieee80211_ifattach(struct ieee80211com *ic)
    385  1.56.18.1      phil {
    386  1.56.18.3      phil #if __NetBSD__
    387  1.56.18.4      phil 	static ONCE_DECL(ic_list_mtx_once);
    388  1.56.18.4      phil 	RUN_ONCE(&ic_list_mtx_once, ic_list_mtx_init);
    389  1.56.18.3      phil #endif
    390  1.56.18.1      phil 
    391  1.56.18.1      phil 	IEEE80211_LOCK_INIT(ic, ic->ic_name);
    392  1.56.18.1      phil 	IEEE80211_TX_LOCK_INIT(ic, ic->ic_name);
    393  1.56.18.1      phil 	TAILQ_INIT(&ic->ic_vaps);
    394  1.56.18.1      phil 
    395  1.56.18.3      phil #if __FreeBSD__
    396  1.56.18.1      phil 	/* Create a taskqueue for all state changes */
    397  1.56.18.1      phil 	ic->ic_tq = taskqueue_create("ic_taskq", M_WAITOK | M_ZERO,
    398  1.56.18.1      phil 	    taskqueue_thread_enqueue, &ic->ic_tq);
    399  1.56.18.1      phil 	taskqueue_start_threads(&ic->ic_tq, 1, PI_NET, "%s net80211 taskq",
    400  1.56.18.1      phil 	    ic->ic_name);
    401  1.56.18.1      phil 	ic->ic_ierrors = counter_u64_alloc(M_WAITOK);
    402  1.56.18.1      phil 	ic->ic_oerrors = counter_u64_alloc(M_WAITOK);
    403  1.56.18.5      phil #elif __NetBSD__
    404  1.56.18.5      phil 	/*
    405  1.56.18.5      phil 	 * Create a workqueue for all state changes, ieee80211_netbsd.*
    406  1.56.18.5      phil 	 * has glue to translate taskqueue functions to workqueue.
    407  1.56.18.5      phil 	 */
    408  1.56.18.5      phil 	if (workqueue_create(&ic->ic_tq, "net80211_wq",
    409  1.56.18.5      phil 	    ieee80211_runwork, ic, PRI_SOFTNET, IPL_NET, WQ_MPSAFE))
    410  1.56.18.5      phil 		panic("net80211 workqueue not created");
    411  1.56.18.3      phil 	ic->ic_ierrors = 0;
    412  1.56.18.3      phil 	ic->ic_oerrors = 0;
    413  1.56.18.2      phil #endif
    414  1.56.18.3      phil 
    415  1.56.18.1      phil 	/*
    416  1.56.18.1      phil 	 * Fill in 802.11 available channel set, mark all
    417  1.56.18.1      phil 	 * available channels as active, and pick a default
    418  1.56.18.1      phil 	 * channel if not already specified.
    419  1.56.18.1      phil 	 */
    420  1.56.18.1      phil 	ieee80211_chan_init(ic);
    421  1.56.18.1      phil 
    422  1.56.18.1      phil 	ic->ic_update_mcast = null_update_mcast;
    423  1.56.18.1      phil 	ic->ic_update_promisc = null_update_promisc;
    424  1.56.18.1      phil 	ic->ic_update_chw = null_update_chw;
    425  1.56.18.1      phil 
    426  1.56.18.1      phil 	ic->ic_hash_key = arc4random();
    427  1.56.18.1      phil 	ic->ic_bintval = IEEE80211_BINTVAL_DEFAULT;
    428  1.56.18.1      phil 	ic->ic_lintval = ic->ic_bintval;
    429  1.56.18.1      phil 	ic->ic_txpowlimit = IEEE80211_TXPOWER_MAX;
    430  1.56.18.1      phil 
    431  1.56.18.1      phil 	ieee80211_crypto_attach(ic);
    432  1.56.18.1      phil 	ieee80211_node_attach(ic);
    433  1.56.18.1      phil 	ieee80211_power_attach(ic);
    434  1.56.18.1      phil 	ieee80211_proto_attach(ic);
    435  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    436  1.56.18.1      phil 	ieee80211_superg_attach(ic);
    437  1.56.18.1      phil #endif
    438  1.56.18.1      phil 	ieee80211_ht_attach(ic);
    439  1.56.18.1      phil 	ieee80211_vht_attach(ic);
    440  1.56.18.1      phil 	ieee80211_scan_attach(ic);
    441  1.56.18.1      phil 	ieee80211_regdomain_attach(ic);
    442  1.56.18.1      phil 	ieee80211_dfs_attach(ic);
    443  1.56.18.1      phil 
    444  1.56.18.1      phil 	ieee80211_sysctl_attach(ic);
    445  1.56.18.1      phil 
    446  1.56.18.1      phil 	mtx_lock(&ic_list_mtx);
    447  1.56.18.1      phil 	LIST_INSERT_HEAD(&ic_head, ic, ic_next);
    448  1.56.18.1      phil 	mtx_unlock(&ic_list_mtx);
    449  1.56.18.1      phil }
    450  1.56.18.1      phil 
    451  1.56.18.1      phil /*
    452  1.56.18.1      phil  * Detach net80211 state on device detach.  Tear down
    453  1.56.18.1      phil  * all vap's and reclaim all common state prior to the
    454  1.56.18.1      phil  * device state going away.  Note we may call back into
    455  1.56.18.1      phil  * driver; it must be prepared for this.
    456  1.56.18.1      phil  */
    457  1.56.18.1      phil void
    458  1.56.18.1      phil ieee80211_ifdetach(struct ieee80211com *ic)
    459  1.56.18.1      phil {
    460  1.56.18.1      phil 	struct ieee80211vap *vap;
    461  1.56.18.1      phil 
    462  1.56.18.1      phil 	mtx_lock(&ic_list_mtx);
    463  1.56.18.1      phil 	LIST_REMOVE(ic, ic_next);
    464  1.56.18.1      phil 	mtx_unlock(&ic_list_mtx);
    465  1.56.18.1      phil 
    466  1.56.18.5      phil #if __FreeBSD__
    467  1.56.18.1      phil 	taskqueue_drain(taskqueue_thread, &ic->ic_restart_task);
    468  1.56.18.5      phil #endif
    469  1.56.18.1      phil 
    470  1.56.18.1      phil 	/*
    471  1.56.18.1      phil 	 * The VAP is responsible for setting and clearing
    472  1.56.18.1      phil 	 * the VIMAGE context.
    473  1.56.18.1      phil 	 */
    474  1.56.18.1      phil 	while ((vap = TAILQ_FIRST(&ic->ic_vaps)) != NULL)
    475  1.56.18.1      phil 		ieee80211_vap_destroy(vap);
    476  1.56.18.1      phil 	ieee80211_waitfor_parent(ic);
    477  1.56.18.1      phil 
    478  1.56.18.1      phil 	ieee80211_sysctl_detach(ic);
    479  1.56.18.1      phil 	ieee80211_dfs_detach(ic);
    480  1.56.18.1      phil 	ieee80211_regdomain_detach(ic);
    481  1.56.18.1      phil 	ieee80211_scan_detach(ic);
    482  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    483  1.56.18.1      phil 	ieee80211_superg_detach(ic);
    484  1.56.18.1      phil #endif
    485  1.56.18.1      phil 	ieee80211_vht_detach(ic);
    486  1.56.18.1      phil 	ieee80211_ht_detach(ic);
    487  1.56.18.1      phil 	/* NB: must be called before ieee80211_node_detach */
    488  1.56.18.1      phil 	ieee80211_proto_detach(ic);
    489  1.56.18.1      phil 	ieee80211_crypto_detach(ic);
    490  1.56.18.1      phil 	ieee80211_power_detach(ic);
    491  1.56.18.1      phil 	ieee80211_node_detach(ic);
    492  1.56.18.1      phil 
    493  1.56.18.1      phil 	counter_u64_free(ic->ic_ierrors);
    494  1.56.18.1      phil 	counter_u64_free(ic->ic_oerrors);
    495  1.56.18.1      phil 
    496  1.56.18.1      phil 	taskqueue_free(ic->ic_tq);
    497  1.56.18.1      phil 	IEEE80211_TX_LOCK_DESTROY(ic);
    498  1.56.18.1      phil 	IEEE80211_LOCK_DESTROY(ic);
    499  1.56.18.1      phil }
    500  1.56.18.1      phil 
    501  1.56.18.1      phil struct ieee80211com *
    502  1.56.18.1      phil ieee80211_find_com(const char *name)
    503       1.37    dyoung {
    504  1.56.18.1      phil 	struct ieee80211com *ic;
    505  1.56.18.1      phil 
    506  1.56.18.1      phil 	mtx_lock(&ic_list_mtx);
    507  1.56.18.1      phil 	LIST_FOREACH(ic, &ic_head, ic_next)
    508  1.56.18.1      phil 		if (strcmp(ic->ic_name, name) == 0)
    509  1.56.18.1      phil 			break;
    510  1.56.18.1      phil 	mtx_unlock(&ic_list_mtx);
    511  1.56.18.1      phil 
    512  1.56.18.1      phil 	return (ic);
    513  1.56.18.1      phil }
    514  1.56.18.1      phil 
    515  1.56.18.1      phil void
    516  1.56.18.1      phil ieee80211_iterate_coms(ieee80211_com_iter_func *f, void *arg)
    517  1.56.18.1      phil {
    518  1.56.18.1      phil 	struct ieee80211com *ic;
    519       1.37    dyoung 
    520  1.56.18.1      phil 	mtx_lock(&ic_list_mtx);
    521  1.56.18.1      phil 	LIST_FOREACH(ic, &ic_head, ic_next)
    522  1.56.18.1      phil 		(*f)(arg, ic);
    523  1.56.18.1      phil 	mtx_unlock(&ic_list_mtx);
    524       1.37    dyoung }
    525       1.37    dyoung 
    526       1.37    dyoung /*
    527       1.37    dyoung  * Default reset method for use with the ioctl support.  This
    528       1.37    dyoung  * method is invoked after any state change in the 802.11
    529       1.37    dyoung  * layer that should be propagated to the hardware but not
    530       1.37    dyoung  * require re-initialization of the 802.11 state machine (e.g
    531       1.37    dyoung  * rescanning for an ap).  We always return ENETRESET which
    532       1.37    dyoung  * should cause the driver to re-initialize the device. Drivers
    533       1.37    dyoung  * can override this method to implement more optimized support.
    534       1.37    dyoung  */
    535       1.37    dyoung static int
    536  1.56.18.1      phil default_reset(struct ieee80211vap *vap, u_long cmd)
    537       1.37    dyoung {
    538       1.37    dyoung 	return ENETRESET;
    539       1.37    dyoung }
    540       1.37    dyoung 
    541  1.56.18.1      phil /*
    542  1.56.18.1      phil  * Default for updating the VAP default TX key index.
    543  1.56.18.1      phil  *
    544  1.56.18.1      phil  * Drivers that support TX offload as well as hardware encryption offload
    545  1.56.18.1      phil  * may need to be informed of key index changes separate from the key
    546  1.56.18.1      phil  * update.
    547  1.56.18.1      phil  */
    548  1.56.18.1      phil static void
    549  1.56.18.1      phil default_update_deftxkey(struct ieee80211vap *vap, ieee80211_keyix kid)
    550        1.1    dyoung {
    551        1.1    dyoung 
    552  1.56.18.1      phil 	/* XXX assert validity */
    553  1.56.18.1      phil 	/* XXX assert we're in a key update block */
    554  1.56.18.1      phil 	vap->iv_def_txkey = kid;
    555  1.56.18.1      phil }
    556       1.37    dyoung 
    557  1.56.18.1      phil /*
    558  1.56.18.1      phil  * Add underlying device errors to vap errors.
    559  1.56.18.1      phil  */
    560  1.56.18.2      phil static __unused uint64_t
    561  1.56.18.1      phil ieee80211_get_counter(struct ifnet *ifp, ift_counter cnt)
    562  1.56.18.1      phil {
    563  1.56.18.1      phil 	struct ieee80211vap *vap = ifp->if_softc;
    564  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    565  1.56.18.1      phil 	uint64_t rv;
    566  1.56.18.1      phil 
    567  1.56.18.1      phil 	rv = if_get_counter_default(ifp, cnt);
    568  1.56.18.1      phil 	switch (cnt) {
    569  1.56.18.1      phil 	case IFCOUNTER_OERRORS:
    570  1.56.18.1      phil 		rv += counter_u64_fetch(ic->ic_oerrors);
    571  1.56.18.1      phil 		break;
    572  1.56.18.1      phil 	case IFCOUNTER_IERRORS:
    573  1.56.18.1      phil 		rv += counter_u64_fetch(ic->ic_ierrors);
    574  1.56.18.1      phil 		break;
    575  1.56.18.1      phil 	default:
    576  1.56.18.1      phil 		break;
    577  1.56.18.1      phil 	}
    578        1.1    dyoung 
    579  1.56.18.1      phil 	return (rv);
    580  1.56.18.1      phil }
    581  1.56.18.1      phil 
    582  1.56.18.1      phil /*
    583  1.56.18.1      phil  * Prepare a vap for use.  Drivers use this call to
    584  1.56.18.1      phil  * setup net80211 state in new vap's prior attaching
    585  1.56.18.1      phil  * them with ieee80211_vap_attach (below).
    586  1.56.18.1      phil  */
    587  1.56.18.1      phil int
    588  1.56.18.1      phil ieee80211_vap_setup(struct ieee80211com *ic, struct ieee80211vap *vap,
    589  1.56.18.1      phil     const char name[IFNAMSIZ], int unit, enum ieee80211_opmode opmode,
    590  1.56.18.1      phil     int flags, const uint8_t bssid[IEEE80211_ADDR_LEN])
    591  1.56.18.1      phil {
    592  1.56.18.1      phil 	struct ifnet *ifp;
    593  1.56.18.1      phil 
    594  1.56.18.1      phil 	ifp = if_alloc(IFT_ETHER);
    595  1.56.18.1      phil 	if (ifp == NULL) {
    596  1.56.18.1      phil 		ic_printf(ic, "%s: unable to allocate ifnet\n",
    597  1.56.18.1      phil 		    __func__);
    598  1.56.18.1      phil 		return ENOMEM;
    599  1.56.18.1      phil 	}
    600  1.56.18.3      phil #if __NetBSD__
    601  1.56.18.3      phil 	if_initialize(ifp);
    602  1.56.18.3      phil #endif
    603  1.56.18.1      phil 	if_initname(ifp, name, unit);
    604  1.56.18.1      phil 	ifp->if_softc = vap;			/* back pointer */
    605  1.56.18.1      phil 	ifp->if_flags = IFF_SIMPLEX | IFF_BROADCAST | IFF_MULTICAST;
    606  1.56.18.1      phil 	ifp->if_transmit = ieee80211_vap_transmit;
    607  1.56.18.3      phil #if __FreeBSD__
    608  1.56.18.1      phil 	ifp->if_qflush = ieee80211_vap_qflush;
    609  1.56.18.2      phil #endif
    610  1.56.18.1      phil 	ifp->if_ioctl = ieee80211_ioctl;
    611  1.56.18.1      phil 	ifp->if_init = ieee80211_init;
    612  1.56.18.3      phil 
    613  1.56.18.3      phil #if notyet
    614  1.56.18.1      phil 	ifp->if_get_counter = ieee80211_get_counter;
    615  1.56.18.2      phil #endif
    616  1.56.18.1      phil 	vap->iv_ifp = ifp;
    617  1.56.18.1      phil 	vap->iv_ic = ic;
    618  1.56.18.1      phil 	vap->iv_flags = ic->ic_flags;		/* propagate common flags */
    619  1.56.18.1      phil 	vap->iv_flags_ext = ic->ic_flags_ext;
    620  1.56.18.1      phil 	vap->iv_flags_ven = ic->ic_flags_ven;
    621  1.56.18.1      phil 	vap->iv_caps = ic->ic_caps &~ IEEE80211_C_OPMODE;
    622  1.56.18.1      phil 
    623  1.56.18.1      phil 	/* 11n capabilities - XXX methodize */
    624  1.56.18.1      phil 	vap->iv_htcaps = ic->ic_htcaps;
    625  1.56.18.1      phil 	vap->iv_htextcaps = ic->ic_htextcaps;
    626  1.56.18.1      phil 
    627  1.56.18.1      phil 	/* 11ac capabilities - XXX methodize */
    628  1.56.18.1      phil 	vap->iv_vhtcaps = ic->ic_vhtcaps;
    629  1.56.18.1      phil 	vap->iv_vhtextcaps = ic->ic_vhtextcaps;
    630  1.56.18.1      phil 
    631  1.56.18.1      phil 	vap->iv_opmode = opmode;
    632  1.56.18.1      phil 	vap->iv_caps |= ieee80211_opcap[opmode];
    633  1.56.18.6      phil 	/* NNN Done in vap_attach, where is the correct place? */
    634  1.56.18.1      phil 	IEEE80211_ADDR_COPY(vap->iv_myaddr, ic->ic_macaddr);
    635  1.56.18.1      phil 	switch (opmode) {
    636  1.56.18.1      phil 	case IEEE80211_M_WDS:
    637  1.56.18.1      phil 		/*
    638  1.56.18.1      phil 		 * WDS links must specify the bssid of the far end.
    639  1.56.18.1      phil 		 * For legacy operation this is a static relationship.
    640  1.56.18.1      phil 		 * For non-legacy operation the station must associate
    641  1.56.18.1      phil 		 * and be authorized to pass traffic.  Plumbing the
    642  1.56.18.1      phil 		 * vap to the proper node happens when the vap
    643  1.56.18.1      phil 		 * transitions to RUN state.
    644  1.56.18.1      phil 		 */
    645  1.56.18.1      phil 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid);
    646  1.56.18.1      phil 		vap->iv_flags |= IEEE80211_F_DESBSSID;
    647  1.56.18.1      phil 		if (flags & IEEE80211_CLONE_WDSLEGACY)
    648  1.56.18.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_WDSLEGACY;
    649  1.56.18.1      phil 		break;
    650  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_TDMA
    651  1.56.18.1      phil 	case IEEE80211_M_AHDEMO:
    652  1.56.18.1      phil 		if (flags & IEEE80211_CLONE_TDMA) {
    653  1.56.18.1      phil 			/* NB: checked before clone operation allowed */
    654  1.56.18.1      phil 			KASSERT(ic->ic_caps & IEEE80211_C_TDMA,
    655  1.56.18.1      phil 			    ("not TDMA capable, ic_caps 0x%x", ic->ic_caps));
    656        1.1    dyoung 			/*
    657  1.56.18.1      phil 			 * Propagate TDMA capability to mark vap; this
    658  1.56.18.1      phil 			 * cannot be removed and is used to distinguish
    659  1.56.18.1      phil 			 * regular ahdemo operation from ahdemo+tdma.
    660        1.1    dyoung 			 */
    661  1.56.18.1      phil 			vap->iv_caps |= IEEE80211_C_TDMA;
    662        1.1    dyoung 		}
    663  1.56.18.1      phil 		break;
    664  1.56.18.1      phil #endif
    665  1.56.18.1      phil 	default:
    666  1.56.18.1      phil 		break;
    667        1.1    dyoung 	}
    668  1.56.18.1      phil 	/* auto-enable s/w beacon miss support */
    669  1.56.18.1      phil 	if (flags & IEEE80211_CLONE_NOBEACONS)
    670  1.56.18.1      phil 		vap->iv_flags_ext |= IEEE80211_FEXT_SWBMISS;
    671  1.56.18.1      phil 	/* auto-generated or user supplied MAC address */
    672  1.56.18.1      phil 	if (flags & (IEEE80211_CLONE_BSSID|IEEE80211_CLONE_MACADDR))
    673  1.56.18.1      phil 		vap->iv_flags_ext |= IEEE80211_FEXT_UNIQMAC;
    674  1.56.18.1      phil 	/*
    675  1.56.18.1      phil 	 * Enable various functionality by default if we're
    676  1.56.18.1      phil 	 * capable; the driver can override us if it knows better.
    677  1.56.18.1      phil 	 */
    678  1.56.18.1      phil 	if (vap->iv_caps & IEEE80211_C_WME)
    679  1.56.18.1      phil 		vap->iv_flags |= IEEE80211_F_WME;
    680  1.56.18.1      phil 	if (vap->iv_caps & IEEE80211_C_BURST)
    681  1.56.18.1      phil 		vap->iv_flags |= IEEE80211_F_BURST;
    682  1.56.18.1      phil 	/* NB: bg scanning only makes sense for station mode right now */
    683  1.56.18.1      phil 	if (vap->iv_opmode == IEEE80211_M_STA &&
    684  1.56.18.1      phil 	    (vap->iv_caps & IEEE80211_C_BGSCAN))
    685  1.56.18.1      phil 		vap->iv_flags |= IEEE80211_F_BGSCAN;
    686  1.56.18.1      phil 	vap->iv_flags |= IEEE80211_F_DOTH;	/* XXX no cap, just ena */
    687  1.56.18.1      phil 	/* NB: DFS support only makes sense for ap mode right now */
    688  1.56.18.1      phil 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
    689  1.56.18.1      phil 	    (vap->iv_caps & IEEE80211_C_DFS))
    690  1.56.18.1      phil 		vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
    691  1.56.18.1      phil 
    692  1.56.18.1      phil 	vap->iv_des_chan = IEEE80211_CHAN_ANYC;		/* any channel is ok */
    693  1.56.18.1      phil 	vap->iv_bmissthreshold = IEEE80211_HWBMISS_DEFAULT;
    694  1.56.18.1      phil 	vap->iv_dtim_period = IEEE80211_DTIM_DEFAULT;
    695  1.56.18.1      phil 	/*
    696  1.56.18.1      phil 	 * Install a default reset method for the ioctl support;
    697  1.56.18.1      phil 	 * the driver can override this.
    698  1.56.18.1      phil 	 */
    699  1.56.18.1      phil 	vap->iv_reset = default_reset;
    700  1.56.18.1      phil 
    701  1.56.18.1      phil 	/*
    702  1.56.18.1      phil 	 * Install a default crypto key update method, the driver
    703  1.56.18.1      phil 	 * can override this.
    704  1.56.18.1      phil 	 */
    705  1.56.18.1      phil 	vap->iv_update_deftxkey = default_update_deftxkey;
    706  1.56.18.1      phil 
    707  1.56.18.1      phil 	ieee80211_sysctl_vattach(vap);
    708  1.56.18.1      phil 	ieee80211_crypto_vattach(vap);
    709  1.56.18.1      phil 	ieee80211_node_vattach(vap);
    710  1.56.18.1      phil 	ieee80211_power_vattach(vap);
    711  1.56.18.1      phil 	ieee80211_proto_vattach(vap);
    712  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    713  1.56.18.1      phil 	ieee80211_superg_vattach(vap);
    714  1.56.18.1      phil #endif
    715  1.56.18.1      phil 	ieee80211_ht_vattach(vap);
    716  1.56.18.1      phil 	ieee80211_vht_vattach(vap);
    717  1.56.18.1      phil 	ieee80211_scan_vattach(vap);
    718  1.56.18.1      phil 	ieee80211_regdomain_vattach(vap);
    719  1.56.18.1      phil 	ieee80211_radiotap_vattach(vap);
    720  1.56.18.1      phil 	ieee80211_ratectl_set(vap, IEEE80211_RATECTL_NONE);
    721  1.56.18.1      phil 
    722  1.56.18.1      phil 	return 0;
    723  1.56.18.1      phil }
    724  1.56.18.1      phil 
    725  1.56.18.1      phil /*
    726  1.56.18.1      phil  * Activate a vap.  State should have been prepared with a
    727  1.56.18.1      phil  * call to ieee80211_vap_setup and by the driver.  On return
    728  1.56.18.1      phil  * from this call the vap is ready for use.
    729  1.56.18.1      phil  */
    730  1.56.18.1      phil int
    731  1.56.18.1      phil ieee80211_vap_attach(struct ieee80211vap *vap, ifm_change_cb_t media_change,
    732  1.56.18.1      phil     ifm_stat_cb_t media_stat, const uint8_t macaddr[IEEE80211_ADDR_LEN])
    733  1.56.18.1      phil {
    734  1.56.18.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    735  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    736  1.56.18.1      phil 	struct ifmediareq imr;
    737  1.56.18.1      phil 	int maxrate;
    738  1.56.18.1      phil 
    739  1.56.18.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
    740  1.56.18.1      phil 	    "%s: %s parent %s flags 0x%x flags_ext 0x%x\n",
    741  1.56.18.1      phil 	    __func__, ieee80211_opmode_name[vap->iv_opmode],
    742  1.56.18.1      phil 	    ic->ic_name, vap->iv_flags, vap->iv_flags_ext);
    743  1.56.18.1      phil 
    744       1.37    dyoung 	/*
    745  1.56.18.1      phil 	 * Do late attach work that cannot happen until after
    746  1.56.18.1      phil 	 * the driver has had a chance to override defaults.
    747       1.37    dyoung 	 */
    748  1.56.18.1      phil 	ieee80211_node_latevattach(vap);
    749  1.56.18.1      phil 	ieee80211_power_latevattach(vap);
    750  1.56.18.1      phil 
    751  1.56.18.1      phil 	maxrate = ieee80211_media_setup(ic, &vap->iv_media, vap->iv_caps,
    752  1.56.18.1      phil 	    vap->iv_opmode == IEEE80211_M_STA, media_change, media_stat);
    753  1.56.18.1      phil 	ieee80211_media_status(ifp, &imr);
    754  1.56.18.1      phil 	/* NB: strip explicit mode; we're actually in autoselect */
    755  1.56.18.1      phil 	ifmedia_set(&vap->iv_media,
    756  1.56.18.1      phil 	    imr.ifm_active &~ (IFM_MMASK | IFM_IEEE80211_TURBO));
    757  1.56.18.1      phil 	if (maxrate)
    758  1.56.18.1      phil 		ifp->if_baudrate = IF_Mbps(maxrate);
    759  1.56.18.1      phil 
    760  1.56.18.1      phil 	ether_ifattach(ifp, macaddr);
    761  1.56.18.6      phil 	/* NNN also done in vap_setup, which is correct? */
    762  1.56.18.1      phil 	IEEE80211_ADDR_COPY(vap->iv_myaddr, IF_LLADDR(ifp));
    763  1.56.18.6      phil 
    764  1.56.18.1      phil 	/* hook output method setup by ether_ifattach */
    765  1.56.18.1      phil 	vap->iv_output = ifp->if_output;
    766  1.56.18.1      phil 	ifp->if_output = ieee80211_output;
    767  1.56.18.3      phil 
    768  1.56.18.1      phil 	/* NB: if_mtu set by ether_ifattach to ETHERMTU */
    769  1.56.18.1      phil 
    770  1.56.18.1      phil 	IEEE80211_LOCK(ic);
    771  1.56.18.1      phil 	TAILQ_INSERT_TAIL(&ic->ic_vaps, vap, iv_next);
    772  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
    773  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    774  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
    775       1.37    dyoung #endif
    776  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
    777  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
    778  1.56.18.1      phil 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
    779  1.56.18.1      phil 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
    780  1.56.18.1      phil 
    781  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
    782  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
    783  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
    784  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
    785  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
    786  1.56.18.1      phil 	IEEE80211_UNLOCK(ic);
    787        1.1    dyoung 
    788  1.56.18.3      phil #if __NetBSD__
    789  1.56.18.3      phil 	if_register(ifp);
    790  1.56.18.3      phil #endif
    791  1.56.18.3      phil 
    792  1.56.18.1      phil 	return 1;
    793  1.56.18.1      phil }
    794       1.37    dyoung 
    795  1.56.18.1      phil /*
    796  1.56.18.1      phil  * Tear down vap state and reclaim the ifnet.
    797  1.56.18.1      phil  * The driver is assumed to have prepared for
    798  1.56.18.1      phil  * this; e.g. by turning off interrupts for the
    799  1.56.18.1      phil  * underlying device.
    800  1.56.18.1      phil  */
    801  1.56.18.1      phil void
    802  1.56.18.1      phil ieee80211_vap_detach(struct ieee80211vap *vap)
    803  1.56.18.1      phil {
    804  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    805  1.56.18.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    806        1.1    dyoung 
    807  1.56.18.1      phil 	CURVNET_SET(ifp->if_vnet);
    808       1.37    dyoung 
    809  1.56.18.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s parent %s\n",
    810  1.56.18.1      phil 	    __func__, ieee80211_opmode_name[vap->iv_opmode], ic->ic_name);
    811       1.37    dyoung 
    812  1.56.18.1      phil 	/* NB: bpfdetach is called by ether_ifdetach and claims all taps */
    813  1.56.18.1      phil 	ether_ifdetach(ifp);
    814  1.56.18.1      phil 
    815  1.56.18.1      phil 	ieee80211_stop(vap);
    816       1.37    dyoung 
    817       1.37    dyoung 	/*
    818  1.56.18.1      phil 	 * Flush any deferred vap tasks.
    819       1.37    dyoung 	 */
    820  1.56.18.1      phil 	ieee80211_draintask(ic, &vap->iv_nstate_task);
    821  1.56.18.1      phil 	ieee80211_draintask(ic, &vap->iv_swbmiss_task);
    822  1.56.18.1      phil 	ieee80211_draintask(ic, &vap->iv_wme_task);
    823  1.56.18.1      phil 	ieee80211_draintask(ic, &ic->ic_parent_task);
    824  1.56.18.1      phil 
    825  1.56.18.5      phil #if __FreeBSD__
    826  1.56.18.1      phil 	/* XXX band-aid until ifnet handles this for us */
    827  1.56.18.1      phil 	taskqueue_drain(taskqueue_swi, &ifp->if_linktask);
    828  1.56.18.5      phil #endif
    829  1.56.18.1      phil 
    830  1.56.18.1      phil 	IEEE80211_LOCK(ic);
    831  1.56.18.1      phil 	KASSERT(vap->iv_state == IEEE80211_S_INIT , ("vap still running"));
    832  1.56.18.1      phil 	TAILQ_REMOVE(&ic->ic_vaps, vap, iv_next);
    833  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_WME);
    834  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    835  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_TURBOP);
    836  1.56.18.1      phil #endif
    837  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_PCF);
    838  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, IEEE80211_F_BURST);
    839  1.56.18.1      phil 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_HT);
    840  1.56.18.1      phil 	ieee80211_syncflag_ht_locked(ic, IEEE80211_FHT_USEHT40);
    841  1.56.18.1      phil 
    842  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_VHT);
    843  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT40);
    844  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80);
    845  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT80P80);
    846  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, IEEE80211_FVHT_USEVHT160);
    847  1.56.18.1      phil 
    848  1.56.18.1      phil 	/* NB: this handles the bpfdetach done below */
    849  1.56.18.1      phil 	ieee80211_syncflag_ext_locked(ic, IEEE80211_FEXT_BPF);
    850  1.56.18.1      phil 	if (vap->iv_ifflags & IFF_PROMISC)
    851  1.56.18.1      phil 		ieee80211_promisc(vap, false);
    852  1.56.18.1      phil 	if (vap->iv_ifflags & IFF_ALLMULTI)
    853  1.56.18.1      phil 		ieee80211_allmulti(vap, false);
    854  1.56.18.1      phil 	IEEE80211_UNLOCK(ic);
    855  1.56.18.1      phil 
    856  1.56.18.1      phil 	ifmedia_removeall(&vap->iv_media);
    857  1.56.18.1      phil 
    858  1.56.18.1      phil 	ieee80211_radiotap_vdetach(vap);
    859  1.56.18.1      phil 	ieee80211_regdomain_vdetach(vap);
    860  1.56.18.1      phil 	ieee80211_scan_vdetach(vap);
    861  1.56.18.1      phil #ifdef IEEE80211_SUPPORT_SUPERG
    862  1.56.18.1      phil 	ieee80211_superg_vdetach(vap);
    863  1.56.18.1      phil #endif
    864  1.56.18.1      phil 	ieee80211_vht_vdetach(vap);
    865  1.56.18.1      phil 	ieee80211_ht_vdetach(vap);
    866  1.56.18.1      phil 	/* NB: must be before ieee80211_node_vdetach */
    867  1.56.18.1      phil 	ieee80211_proto_vdetach(vap);
    868  1.56.18.1      phil 	ieee80211_crypto_vdetach(vap);
    869  1.56.18.1      phil 	ieee80211_power_vdetach(vap);
    870  1.56.18.1      phil 	ieee80211_node_vdetach(vap);
    871  1.56.18.1      phil 	ieee80211_sysctl_vdetach(vap);
    872  1.56.18.1      phil 
    873  1.56.18.1      phil 	if_free(ifp);
    874  1.56.18.1      phil 
    875  1.56.18.1      phil 	CURVNET_RESTORE();
    876        1.1    dyoung }
    877        1.1    dyoung 
    878  1.56.18.1      phil /*
    879  1.56.18.1      phil  * Count number of vaps in promisc, and issue promisc on
    880  1.56.18.1      phil  * parent respectively.
    881  1.56.18.1      phil  */
    882        1.1    dyoung void
    883  1.56.18.1      phil ieee80211_promisc(struct ieee80211vap *vap, bool on)
    884        1.1    dyoung {
    885  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    886       1.37    dyoung 
    887  1.56.18.1      phil 	IEEE80211_LOCK_ASSERT(ic);
    888        1.1    dyoung 
    889  1.56.18.1      phil 	if (on) {
    890  1.56.18.1      phil 		if (++ic->ic_promisc == 1)
    891  1.56.18.1      phil 			ieee80211_runtask(ic, &ic->ic_promisc_task);
    892  1.56.18.1      phil 	} else {
    893  1.56.18.1      phil 		KASSERT(ic->ic_promisc > 0, ("%s: ic %p not promisc",
    894  1.56.18.1      phil 		    __func__, ic));
    895  1.56.18.1      phil 		if (--ic->ic_promisc == 0)
    896  1.56.18.1      phil 			ieee80211_runtask(ic, &ic->ic_promisc_task);
    897  1.56.18.1      phil 	}
    898  1.56.18.1      phil }
    899  1.56.18.1      phil 
    900  1.56.18.1      phil /*
    901  1.56.18.1      phil  * Count number of vaps in allmulti, and issue allmulti on
    902  1.56.18.1      phil  * parent respectively.
    903  1.56.18.1      phil  */
    904  1.56.18.1      phil void
    905  1.56.18.1      phil ieee80211_allmulti(struct ieee80211vap *vap, bool on)
    906  1.56.18.1      phil {
    907  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    908       1.37    dyoung 
    909  1.56.18.1      phil 	IEEE80211_LOCK_ASSERT(ic);
    910       1.37    dyoung 
    911  1.56.18.1      phil 	if (on) {
    912  1.56.18.1      phil 		if (++ic->ic_allmulti == 1)
    913  1.56.18.1      phil 			ieee80211_runtask(ic, &ic->ic_mcast_task);
    914  1.56.18.1      phil 	} else {
    915  1.56.18.1      phil 		KASSERT(ic->ic_allmulti > 0, ("%s: ic %p not allmulti",
    916  1.56.18.1      phil 		    __func__, ic));
    917  1.56.18.1      phil 		if (--ic->ic_allmulti == 0)
    918  1.56.18.1      phil 			ieee80211_runtask(ic, &ic->ic_mcast_task);
    919  1.56.18.1      phil 	}
    920        1.1    dyoung }
    921        1.1    dyoung 
    922        1.1    dyoung /*
    923  1.56.18.1      phil  * Synchronize flag bit state in the com structure
    924  1.56.18.1      phil  * according to the state of all vap's.  This is used,
    925  1.56.18.1      phil  * for example, to handle state changes via ioctls.
    926        1.1    dyoung  */
    927  1.56.18.1      phil static void
    928  1.56.18.1      phil ieee80211_syncflag_locked(struct ieee80211com *ic, int flag)
    929        1.1    dyoung {
    930  1.56.18.1      phil 	struct ieee80211vap *vap;
    931  1.56.18.1      phil 	int bit;
    932  1.56.18.1      phil 
    933  1.56.18.1      phil 	IEEE80211_LOCK_ASSERT(ic);
    934  1.56.18.1      phil 
    935  1.56.18.1      phil 	bit = 0;
    936  1.56.18.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
    937  1.56.18.1      phil 		if (vap->iv_flags & flag) {
    938  1.56.18.1      phil 			bit = 1;
    939  1.56.18.1      phil 			break;
    940  1.56.18.1      phil 		}
    941  1.56.18.1      phil 	if (bit)
    942  1.56.18.1      phil 		ic->ic_flags |= flag;
    943  1.56.18.1      phil 	else
    944  1.56.18.1      phil 		ic->ic_flags &= ~flag;
    945  1.56.18.1      phil }
    946  1.56.18.1      phil 
    947  1.56.18.1      phil void
    948  1.56.18.1      phil ieee80211_syncflag(struct ieee80211vap *vap, int flag)
    949  1.56.18.1      phil {
    950  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    951  1.56.18.1      phil 
    952  1.56.18.1      phil 	IEEE80211_LOCK(ic);
    953  1.56.18.1      phil 	if (flag < 0) {
    954  1.56.18.1      phil 		flag = -flag;
    955  1.56.18.1      phil 		vap->iv_flags &= ~flag;
    956  1.56.18.1      phil 	} else
    957  1.56.18.1      phil 		vap->iv_flags |= flag;
    958  1.56.18.1      phil 	ieee80211_syncflag_locked(ic, flag);
    959  1.56.18.1      phil 	IEEE80211_UNLOCK(ic);
    960        1.1    dyoung }
    961        1.1    dyoung 
    962        1.1    dyoung /*
    963  1.56.18.1      phil  * Synchronize flags_ht bit state in the com structure
    964  1.56.18.1      phil  * according to the state of all vap's.  This is used,
    965  1.56.18.1      phil  * for example, to handle state changes via ioctls.
    966        1.1    dyoung  */
    967  1.56.18.1      phil static void
    968  1.56.18.1      phil ieee80211_syncflag_ht_locked(struct ieee80211com *ic, int flag)
    969        1.1    dyoung {
    970  1.56.18.1      phil 	struct ieee80211vap *vap;
    971  1.56.18.1      phil 	int bit;
    972  1.56.18.1      phil 
    973  1.56.18.1      phil 	IEEE80211_LOCK_ASSERT(ic);
    974  1.56.18.1      phil 
    975  1.56.18.1      phil 	bit = 0;
    976  1.56.18.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
    977  1.56.18.1      phil 		if (vap->iv_flags_ht & flag) {
    978  1.56.18.1      phil 			bit = 1;
    979  1.56.18.1      phil 			break;
    980  1.56.18.1      phil 		}
    981  1.56.18.1      phil 	if (bit)
    982  1.56.18.1      phil 		ic->ic_flags_ht |= flag;
    983  1.56.18.1      phil 	else
    984  1.56.18.1      phil 		ic->ic_flags_ht &= ~flag;
    985  1.56.18.1      phil }
    986  1.56.18.1      phil 
    987  1.56.18.1      phil void
    988  1.56.18.1      phil ieee80211_syncflag_ht(struct ieee80211vap *vap, int flag)
    989  1.56.18.1      phil {
    990  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    991  1.56.18.1      phil 
    992  1.56.18.1      phil 	IEEE80211_LOCK(ic);
    993  1.56.18.1      phil 	if (flag < 0) {
    994  1.56.18.1      phil 		flag = -flag;
    995  1.56.18.1      phil 		vap->iv_flags_ht &= ~flag;
    996  1.56.18.1      phil 	} else
    997  1.56.18.1      phil 		vap->iv_flags_ht |= flag;
    998  1.56.18.1      phil 	ieee80211_syncflag_ht_locked(ic, flag);
    999  1.56.18.1      phil 	IEEE80211_UNLOCK(ic);
   1000        1.1    dyoung }
   1001        1.1    dyoung 
   1002        1.1    dyoung /*
   1003  1.56.18.1      phil  * Synchronize flags_vht bit state in the com structure
   1004  1.56.18.1      phil  * according to the state of all vap's.  This is used,
   1005  1.56.18.1      phil  * for example, to handle state changes via ioctls.
   1006        1.1    dyoung  */
   1007  1.56.18.1      phil static void
   1008  1.56.18.1      phil ieee80211_syncflag_vht_locked(struct ieee80211com *ic, int flag)
   1009        1.1    dyoung {
   1010  1.56.18.1      phil 	struct ieee80211vap *vap;
   1011  1.56.18.1      phil 	int bit;
   1012  1.56.18.1      phil 
   1013  1.56.18.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1014  1.56.18.1      phil 
   1015  1.56.18.1      phil 	bit = 0;
   1016  1.56.18.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   1017  1.56.18.1      phil 		if (vap->iv_flags_vht & flag) {
   1018  1.56.18.1      phil 			bit = 1;
   1019  1.56.18.1      phil 			break;
   1020  1.56.18.1      phil 		}
   1021  1.56.18.1      phil 	if (bit)
   1022  1.56.18.1      phil 		ic->ic_flags_vht |= flag;
   1023  1.56.18.1      phil 	else
   1024  1.56.18.1      phil 		ic->ic_flags_vht &= ~flag;
   1025  1.56.18.1      phil }
   1026  1.56.18.1      phil 
   1027  1.56.18.1      phil void
   1028  1.56.18.1      phil ieee80211_syncflag_vht(struct ieee80211vap *vap, int flag)
   1029  1.56.18.1      phil {
   1030  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1031  1.56.18.1      phil 
   1032  1.56.18.1      phil 	IEEE80211_LOCK(ic);
   1033  1.56.18.1      phil 	if (flag < 0) {
   1034  1.56.18.1      phil 		flag = -flag;
   1035  1.56.18.1      phil 		vap->iv_flags_vht &= ~flag;
   1036  1.56.18.1      phil 	} else
   1037  1.56.18.1      phil 		vap->iv_flags_vht |= flag;
   1038  1.56.18.1      phil 	ieee80211_syncflag_vht_locked(ic, flag);
   1039  1.56.18.1      phil 	IEEE80211_UNLOCK(ic);
   1040  1.56.18.1      phil }
   1041  1.56.18.1      phil 
   1042  1.56.18.1      phil /*
   1043  1.56.18.1      phil  * Synchronize flags_ext bit state in the com structure
   1044  1.56.18.1      phil  * according to the state of all vap's.  This is used,
   1045  1.56.18.1      phil  * for example, to handle state changes via ioctls.
   1046  1.56.18.1      phil  */
   1047  1.56.18.1      phil static void
   1048  1.56.18.1      phil ieee80211_syncflag_ext_locked(struct ieee80211com *ic, int flag)
   1049  1.56.18.1      phil {
   1050  1.56.18.1      phil 	struct ieee80211vap *vap;
   1051  1.56.18.1      phil 	int bit;
   1052  1.56.18.1      phil 
   1053  1.56.18.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1054  1.56.18.1      phil 
   1055  1.56.18.1      phil 	bit = 0;
   1056  1.56.18.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   1057  1.56.18.1      phil 		if (vap->iv_flags_ext & flag) {
   1058  1.56.18.1      phil 			bit = 1;
   1059  1.56.18.1      phil 			break;
   1060  1.56.18.1      phil 		}
   1061  1.56.18.1      phil 	if (bit)
   1062  1.56.18.1      phil 		ic->ic_flags_ext |= flag;
   1063  1.56.18.1      phil 	else
   1064  1.56.18.1      phil 		ic->ic_flags_ext &= ~flag;
   1065  1.56.18.1      phil }
   1066  1.56.18.1      phil 
   1067  1.56.18.1      phil void
   1068  1.56.18.1      phil ieee80211_syncflag_ext(struct ieee80211vap *vap, int flag)
   1069  1.56.18.1      phil {
   1070  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1071  1.56.18.1      phil 
   1072  1.56.18.1      phil 	IEEE80211_LOCK(ic);
   1073  1.56.18.1      phil 	if (flag < 0) {
   1074  1.56.18.1      phil 		flag = -flag;
   1075  1.56.18.1      phil 		vap->iv_flags_ext &= ~flag;
   1076  1.56.18.1      phil 	} else
   1077  1.56.18.1      phil 		vap->iv_flags_ext |= flag;
   1078  1.56.18.1      phil 	ieee80211_syncflag_ext_locked(ic, flag);
   1079  1.56.18.1      phil 	IEEE80211_UNLOCK(ic);
   1080  1.56.18.1      phil }
   1081  1.56.18.1      phil 
   1082  1.56.18.1      phil static __inline int
   1083  1.56.18.1      phil mapgsm(u_int freq, u_int flags)
   1084  1.56.18.1      phil {
   1085  1.56.18.1      phil 	freq *= 10;
   1086  1.56.18.1      phil 	if (flags & IEEE80211_CHAN_QUARTER)
   1087  1.56.18.1      phil 		freq += 5;
   1088  1.56.18.1      phil 	else if (flags & IEEE80211_CHAN_HALF)
   1089  1.56.18.1      phil 		freq += 10;
   1090  1.56.18.1      phil 	else
   1091  1.56.18.1      phil 		freq += 20;
   1092  1.56.18.1      phil 	/* NB: there is no 907/20 wide but leave room */
   1093  1.56.18.1      phil 	return (freq - 906*10) / 5;
   1094  1.56.18.1      phil }
   1095  1.56.18.1      phil 
   1096  1.56.18.1      phil static __inline int
   1097  1.56.18.1      phil mappsb(u_int freq, u_int flags)
   1098  1.56.18.1      phil {
   1099  1.56.18.1      phil 	return 37 + ((freq * 10) + ((freq % 5) == 2 ? 5 : 0) - 49400) / 5;
   1100  1.56.18.1      phil }
   1101  1.56.18.1      phil 
   1102  1.56.18.1      phil /*
   1103  1.56.18.1      phil  * Convert MHz frequency to IEEE channel number.
   1104  1.56.18.1      phil  */
   1105  1.56.18.1      phil int
   1106  1.56.18.1      phil ieee80211_mhz2ieee(u_int freq, u_int flags)
   1107  1.56.18.1      phil {
   1108  1.56.18.1      phil #define	IS_FREQ_IN_PSB(_freq) ((_freq) > 4940 && (_freq) < 4990)
   1109  1.56.18.1      phil 	if (flags & IEEE80211_CHAN_GSM)
   1110  1.56.18.1      phil 		return mapgsm(freq, flags);
   1111  1.56.18.1      phil 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
   1112  1.56.18.1      phil 		if (freq == 2484)
   1113  1.56.18.1      phil 			return 14;
   1114  1.56.18.1      phil 		if (freq < 2484)
   1115  1.56.18.1      phil 			return ((int) freq - 2407) / 5;
   1116  1.56.18.1      phil 		else
   1117  1.56.18.1      phil 			return 15 + ((freq - 2512) / 20);
   1118  1.56.18.1      phil 	} else if (flags & IEEE80211_CHAN_5GHZ) {	/* 5Ghz band */
   1119  1.56.18.1      phil 		if (freq <= 5000) {
   1120  1.56.18.1      phil 			/* XXX check regdomain? */
   1121  1.56.18.1      phil 			if (IS_FREQ_IN_PSB(freq))
   1122  1.56.18.1      phil 				return mappsb(freq, flags);
   1123  1.56.18.1      phil 			return (freq - 4000) / 5;
   1124  1.56.18.1      phil 		} else
   1125  1.56.18.1      phil 			return (freq - 5000) / 5;
   1126  1.56.18.1      phil 	} else {				/* either, guess */
   1127  1.56.18.1      phil 		if (freq == 2484)
   1128  1.56.18.1      phil 			return 14;
   1129  1.56.18.1      phil 		if (freq < 2484) {
   1130  1.56.18.1      phil 			if (907 <= freq && freq <= 922)
   1131  1.56.18.1      phil 				return mapgsm(freq, flags);
   1132  1.56.18.1      phil 			return ((int) freq - 2407) / 5;
   1133  1.56.18.1      phil 		}
   1134  1.56.18.1      phil 		if (freq < 5000) {
   1135  1.56.18.1      phil 			if (IS_FREQ_IN_PSB(freq))
   1136  1.56.18.1      phil 				return mappsb(freq, flags);
   1137  1.56.18.1      phil 			else if (freq > 4900)
   1138  1.56.18.1      phil 				return (freq - 4000) / 5;
   1139  1.56.18.1      phil 			else
   1140  1.56.18.1      phil 				return 15 + ((freq - 2512) / 20);
   1141  1.56.18.1      phil 		}
   1142  1.56.18.1      phil 		return (freq - 5000) / 5;
   1143  1.56.18.1      phil 	}
   1144  1.56.18.1      phil #undef IS_FREQ_IN_PSB
   1145  1.56.18.1      phil }
   1146  1.56.18.1      phil 
   1147  1.56.18.1      phil /*
   1148  1.56.18.1      phil  * Convert channel to IEEE channel number.
   1149  1.56.18.1      phil  */
   1150  1.56.18.1      phil int
   1151  1.56.18.1      phil ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
   1152  1.56.18.1      phil {
   1153  1.56.18.1      phil 	if (c == NULL) {
   1154  1.56.18.1      phil 		ic_printf(ic, "invalid channel (NULL)\n");
   1155  1.56.18.1      phil 		return 0;		/* XXX */
   1156  1.56.18.1      phil 	}
   1157  1.56.18.1      phil 	return (c == IEEE80211_CHAN_ANYC ?  IEEE80211_CHAN_ANY : c->ic_ieee);
   1158  1.56.18.1      phil }
   1159  1.56.18.1      phil 
   1160  1.56.18.1      phil /*
   1161  1.56.18.1      phil  * Convert IEEE channel number to MHz frequency.
   1162  1.56.18.1      phil  */
   1163  1.56.18.1      phil u_int
   1164  1.56.18.1      phil ieee80211_ieee2mhz(u_int chan, u_int flags)
   1165  1.56.18.1      phil {
   1166  1.56.18.1      phil 	if (flags & IEEE80211_CHAN_GSM)
   1167  1.56.18.1      phil 		return 907 + 5 * (chan / 10);
   1168  1.56.18.1      phil 	if (flags & IEEE80211_CHAN_2GHZ) {	/* 2GHz band */
   1169  1.56.18.1      phil 		if (chan == 14)
   1170  1.56.18.1      phil 			return 2484;
   1171  1.56.18.1      phil 		if (chan < 14)
   1172  1.56.18.1      phil 			return 2407 + chan*5;
   1173  1.56.18.1      phil 		else
   1174  1.56.18.1      phil 			return 2512 + ((chan-15)*20);
   1175  1.56.18.1      phil 	} else if (flags & IEEE80211_CHAN_5GHZ) {/* 5Ghz band */
   1176  1.56.18.1      phil 		if (flags & (IEEE80211_CHAN_HALF|IEEE80211_CHAN_QUARTER)) {
   1177  1.56.18.1      phil 			chan -= 37;
   1178  1.56.18.1      phil 			return 4940 + chan*5 + (chan % 5 ? 2 : 0);
   1179  1.56.18.1      phil 		}
   1180        1.1    dyoung 		return 5000 + (chan*5);
   1181        1.1    dyoung 	} else {				/* either, guess */
   1182  1.56.18.1      phil 		/* XXX can't distinguish PSB+GSM channels */
   1183        1.1    dyoung 		if (chan == 14)
   1184        1.1    dyoung 			return 2484;
   1185        1.1    dyoung 		if (chan < 14)			/* 0-13 */
   1186        1.1    dyoung 			return 2407 + chan*5;
   1187        1.1    dyoung 		if (chan < 27)			/* 15-26 */
   1188        1.1    dyoung 			return 2512 + ((chan-15)*20);
   1189        1.1    dyoung 		return 5000 + (chan*5);
   1190        1.1    dyoung 	}
   1191        1.1    dyoung }
   1192        1.1    dyoung 
   1193  1.56.18.1      phil static __inline void
   1194  1.56.18.1      phil set_extchan(struct ieee80211_channel *c)
   1195  1.56.18.1      phil {
   1196  1.56.18.1      phil 
   1197  1.56.18.1      phil 	/*
   1198  1.56.18.1      phil 	 * IEEE Std 802.11-2012, page 1738, subclause 20.3.15.4:
   1199  1.56.18.1      phil 	 * "the secondary channel number shall be 'N + [1,-1] * 4'
   1200  1.56.18.1      phil 	 */
   1201  1.56.18.1      phil 	if (c->ic_flags & IEEE80211_CHAN_HT40U)
   1202  1.56.18.1      phil 		c->ic_extieee = c->ic_ieee + 4;
   1203  1.56.18.1      phil 	else if (c->ic_flags & IEEE80211_CHAN_HT40D)
   1204  1.56.18.1      phil 		c->ic_extieee = c->ic_ieee - 4;
   1205  1.56.18.1      phil 	else
   1206  1.56.18.1      phil 		c->ic_extieee = 0;
   1207  1.56.18.1      phil }
   1208  1.56.18.1      phil 
   1209  1.56.18.1      phil /*
   1210  1.56.18.1      phil  * Populate the freq1/freq2 fields as appropriate for VHT channels.
   1211  1.56.18.1      phil  *
   1212  1.56.18.1      phil  * This for now uses a hard-coded list of 80MHz wide channels.
   1213  1.56.18.1      phil  *
   1214  1.56.18.1      phil  * For HT20/HT40, freq1 just is the centre frequency of the 40MHz
   1215  1.56.18.1      phil  * wide channel we've already decided upon.
   1216  1.56.18.1      phil  *
   1217  1.56.18.1      phil  * For VHT80 and VHT160, there are only a small number of fixed
   1218  1.56.18.1      phil  * 80/160MHz wide channels, so we just use those.
   1219  1.56.18.1      phil  *
   1220  1.56.18.1      phil  * This is all likely very very wrong - both the regulatory code
   1221  1.56.18.1      phil  * and this code needs to ensure that all four channels are
   1222  1.56.18.1      phil  * available and valid before the VHT80 (and eight for VHT160) channel
   1223  1.56.18.1      phil  * is created.
   1224  1.56.18.1      phil  */
   1225  1.56.18.1      phil 
   1226  1.56.18.1      phil struct vht_chan_range {
   1227  1.56.18.1      phil 	uint16_t freq_start;
   1228  1.56.18.1      phil 	uint16_t freq_end;
   1229  1.56.18.1      phil };
   1230  1.56.18.1      phil 
   1231  1.56.18.1      phil struct vht_chan_range vht80_chan_ranges[] = {
   1232  1.56.18.1      phil 	{ 5170, 5250 },
   1233  1.56.18.1      phil 	{ 5250, 5330 },
   1234  1.56.18.1      phil 	{ 5490, 5570 },
   1235  1.56.18.1      phil 	{ 5570, 5650 },
   1236  1.56.18.1      phil 	{ 5650, 5730 },
   1237  1.56.18.1      phil 	{ 5735, 5815 },
   1238  1.56.18.1      phil 	{ 0, 0, }
   1239  1.56.18.1      phil };
   1240  1.56.18.1      phil 
   1241  1.56.18.1      phil static int
   1242  1.56.18.1      phil set_vht_extchan(struct ieee80211_channel *c)
   1243  1.56.18.1      phil {
   1244  1.56.18.1      phil 	int i;
   1245  1.56.18.1      phil 
   1246  1.56.18.1      phil 	if (! IEEE80211_IS_CHAN_VHT(c)) {
   1247  1.56.18.1      phil 		return (0);
   1248  1.56.18.1      phil 	}
   1249  1.56.18.1      phil 
   1250  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_VHT20(c)) {
   1251  1.56.18.1      phil 		c->ic_vht_ch_freq1 = c->ic_ieee;
   1252  1.56.18.1      phil 		return (1);
   1253  1.56.18.1      phil 	}
   1254  1.56.18.1      phil 
   1255  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_VHT40(c)) {
   1256  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_HT40U(c))
   1257  1.56.18.1      phil 			c->ic_vht_ch_freq1 = c->ic_ieee + 2;
   1258  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_HT40D(c))
   1259  1.56.18.1      phil 			c->ic_vht_ch_freq1 = c->ic_ieee - 2;
   1260  1.56.18.1      phil 		else
   1261  1.56.18.1      phil 			return (0);
   1262  1.56.18.1      phil 		return (1);
   1263  1.56.18.1      phil 	}
   1264  1.56.18.1      phil 
   1265  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_VHT80(c)) {
   1266  1.56.18.1      phil 		for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
   1267  1.56.18.1      phil 			if (c->ic_freq >= vht80_chan_ranges[i].freq_start &&
   1268  1.56.18.1      phil 			    c->ic_freq < vht80_chan_ranges[i].freq_end) {
   1269  1.56.18.1      phil 				int midpoint;
   1270  1.56.18.1      phil 
   1271  1.56.18.1      phil 				midpoint = vht80_chan_ranges[i].freq_start + 40;
   1272  1.56.18.1      phil 				c->ic_vht_ch_freq1 =
   1273  1.56.18.1      phil 				    ieee80211_mhz2ieee(midpoint, c->ic_flags);
   1274  1.56.18.1      phil 				c->ic_vht_ch_freq2 = 0;
   1275  1.56.18.1      phil #if 0
   1276  1.56.18.1      phil 				printf("%s: %d, freq=%d, midpoint=%d, freq1=%d, freq2=%d\n",
   1277  1.56.18.1      phil 				    __func__, c->ic_ieee, c->ic_freq, midpoint,
   1278  1.56.18.1      phil 				    c->ic_vht_ch_freq1, c->ic_vht_ch_freq2);
   1279  1.56.18.1      phil #endif
   1280  1.56.18.1      phil 				return (1);
   1281  1.56.18.1      phil 			}
   1282  1.56.18.1      phil 		}
   1283  1.56.18.1      phil 		return (0);
   1284  1.56.18.1      phil 	}
   1285  1.56.18.1      phil 
   1286  1.56.18.1      phil 	printf("%s: unknown VHT channel type (ieee=%d, flags=0x%08x)\n",
   1287  1.56.18.1      phil 	    __func__,
   1288  1.56.18.1      phil 	    c->ic_ieee,
   1289  1.56.18.1      phil 	    c->ic_flags);
   1290  1.56.18.1      phil 
   1291  1.56.18.1      phil 	return (0);
   1292  1.56.18.1      phil }
   1293  1.56.18.1      phil 
   1294  1.56.18.1      phil /*
   1295  1.56.18.1      phil  * Return whether the current channel could possibly be a part of
   1296  1.56.18.1      phil  * a VHT80 channel.
   1297  1.56.18.1      phil  *
   1298  1.56.18.1      phil  * This doesn't check that the whole range is in the allowed list
   1299  1.56.18.1      phil  * according to regulatory.
   1300  1.56.18.1      phil  */
   1301  1.56.18.1      phil static int
   1302  1.56.18.1      phil is_vht80_valid_freq(uint16_t freq)
   1303  1.56.18.1      phil {
   1304  1.56.18.1      phil 	int i;
   1305  1.56.18.1      phil 	for (i = 0; vht80_chan_ranges[i].freq_start != 0; i++) {
   1306  1.56.18.1      phil 		if (freq >= vht80_chan_ranges[i].freq_start &&
   1307  1.56.18.1      phil 		    freq < vht80_chan_ranges[i].freq_end)
   1308  1.56.18.1      phil 			return (1);
   1309  1.56.18.1      phil 	}
   1310  1.56.18.1      phil 	return (0);
   1311  1.56.18.1      phil }
   1312  1.56.18.1      phil 
   1313  1.56.18.1      phil static int
   1314  1.56.18.1      phil addchan(struct ieee80211_channel chans[], int maxchans, int *nchans,
   1315  1.56.18.1      phil     uint8_t ieee, uint16_t freq, int8_t maxregpower, uint32_t flags)
   1316  1.56.18.1      phil {
   1317  1.56.18.1      phil 	struct ieee80211_channel *c;
   1318  1.56.18.1      phil 
   1319  1.56.18.1      phil 	if (*nchans >= maxchans)
   1320  1.56.18.1      phil 		return (ENOBUFS);
   1321  1.56.18.1      phil 
   1322  1.56.18.1      phil #if 0
   1323  1.56.18.1      phil 	printf("%s: %d: ieee=%d, freq=%d, flags=0x%08x\n",
   1324  1.56.18.1      phil 	    __func__,
   1325  1.56.18.1      phil 	    *nchans,
   1326  1.56.18.1      phil 	    ieee,
   1327  1.56.18.1      phil 	    freq,
   1328  1.56.18.1      phil 	    flags);
   1329  1.56.18.1      phil #endif
   1330  1.56.18.1      phil 
   1331  1.56.18.1      phil 	c = &chans[(*nchans)++];
   1332  1.56.18.1      phil 	c->ic_ieee = ieee;
   1333  1.56.18.1      phil 	c->ic_freq = freq != 0 ? freq : ieee80211_ieee2mhz(ieee, flags);
   1334  1.56.18.1      phil 	c->ic_maxregpower = maxregpower;
   1335  1.56.18.1      phil 	c->ic_maxpower = 2 * maxregpower;
   1336  1.56.18.1      phil 	c->ic_flags = flags;
   1337  1.56.18.1      phil 	c->ic_vht_ch_freq1 = 0;
   1338  1.56.18.1      phil 	c->ic_vht_ch_freq2 = 0;
   1339  1.56.18.1      phil 	set_extchan(c);
   1340  1.56.18.1      phil 	set_vht_extchan(c);
   1341  1.56.18.1      phil 
   1342  1.56.18.1      phil 	return (0);
   1343  1.56.18.1      phil }
   1344  1.56.18.1      phil 
   1345  1.56.18.1      phil static int
   1346  1.56.18.1      phil copychan_prev(struct ieee80211_channel chans[], int maxchans, int *nchans,
   1347  1.56.18.1      phil     uint32_t flags)
   1348  1.56.18.1      phil {
   1349  1.56.18.1      phil 	struct ieee80211_channel *c;
   1350  1.56.18.1      phil 
   1351  1.56.18.1      phil 	KASSERT(*nchans > 0, ("channel list is empty\n"));
   1352  1.56.18.1      phil 
   1353  1.56.18.1      phil 	if (*nchans >= maxchans)
   1354  1.56.18.1      phil 		return (ENOBUFS);
   1355  1.56.18.1      phil 
   1356  1.56.18.1      phil #if 0
   1357  1.56.18.1      phil 	printf("%s: %d: flags=0x%08x\n",
   1358  1.56.18.1      phil 	    __func__,
   1359  1.56.18.1      phil 	    *nchans,
   1360  1.56.18.1      phil 	    flags);
   1361  1.56.18.1      phil #endif
   1362  1.56.18.1      phil 
   1363  1.56.18.1      phil 	c = &chans[(*nchans)++];
   1364  1.56.18.1      phil 	c[0] = c[-1];
   1365  1.56.18.1      phil 	c->ic_flags = flags;
   1366  1.56.18.1      phil 	c->ic_vht_ch_freq1 = 0;
   1367  1.56.18.1      phil 	c->ic_vht_ch_freq2 = 0;
   1368  1.56.18.1      phil 	set_extchan(c);
   1369  1.56.18.1      phil 	set_vht_extchan(c);
   1370  1.56.18.1      phil 
   1371  1.56.18.1      phil 	return (0);
   1372  1.56.18.1      phil }
   1373  1.56.18.1      phil 
   1374  1.56.18.1      phil /*
   1375  1.56.18.1      phil  * XXX VHT-2GHz
   1376  1.56.18.1      phil  */
   1377  1.56.18.1      phil static void
   1378  1.56.18.1      phil getflags_2ghz(const uint8_t bands[], uint32_t flags[], int ht40)
   1379  1.56.18.1      phil {
   1380  1.56.18.1      phil 	int nmodes;
   1381  1.56.18.1      phil 
   1382  1.56.18.1      phil 	nmodes = 0;
   1383  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_11B))
   1384  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_B;
   1385  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_11G))
   1386  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_G;
   1387  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_11NG))
   1388  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT20;
   1389  1.56.18.1      phil 	if (ht40) {
   1390  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40U;
   1391  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_G | IEEE80211_CHAN_HT40D;
   1392  1.56.18.1      phil 	}
   1393  1.56.18.1      phil 	flags[nmodes] = 0;
   1394  1.56.18.1      phil }
   1395  1.56.18.1      phil 
   1396  1.56.18.1      phil static void
   1397  1.56.18.1      phil getflags_5ghz(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
   1398  1.56.18.1      phil {
   1399  1.56.18.1      phil 	int nmodes;
   1400  1.56.18.1      phil 
   1401  1.56.18.1      phil 	/*
   1402  1.56.18.1      phil 	 * the addchan_list function seems to expect the flags array to
   1403  1.56.18.1      phil 	 * be in channel width order, so the VHT bits are interspersed
   1404  1.56.18.1      phil 	 * as appropriate to maintain said order.
   1405  1.56.18.1      phil 	 *
   1406  1.56.18.1      phil 	 * It also assumes HT40U is before HT40D.
   1407  1.56.18.1      phil 	 */
   1408  1.56.18.1      phil 	nmodes = 0;
   1409  1.56.18.1      phil 
   1410  1.56.18.1      phil 	/* 20MHz */
   1411  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_11A))
   1412  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A;
   1413  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_11NA))
   1414  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20;
   1415  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
   1416  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT20 |
   1417  1.56.18.1      phil 		    IEEE80211_CHAN_VHT20;
   1418  1.56.18.1      phil 	}
   1419  1.56.18.1      phil 
   1420  1.56.18.1      phil 	/* 40MHz */
   1421  1.56.18.1      phil 	if (ht40) {
   1422  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U;
   1423  1.56.18.1      phil 	}
   1424  1.56.18.1      phil 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
   1425  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40U
   1426  1.56.18.1      phil 		    | IEEE80211_CHAN_VHT40U;
   1427  1.56.18.1      phil 	}
   1428  1.56.18.1      phil 	if (ht40) {
   1429  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D;
   1430  1.56.18.1      phil 	}
   1431  1.56.18.1      phil 	if (ht40 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
   1432  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A | IEEE80211_CHAN_HT40D
   1433  1.56.18.1      phil 		    | IEEE80211_CHAN_VHT40D;
   1434  1.56.18.1      phil 	}
   1435  1.56.18.1      phil 
   1436  1.56.18.1      phil 	/* 80MHz */
   1437  1.56.18.1      phil 	if (vht80 && isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
   1438  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A |
   1439  1.56.18.1      phil 		    IEEE80211_CHAN_HT40U | IEEE80211_CHAN_VHT80;
   1440  1.56.18.1      phil 		flags[nmodes++] = IEEE80211_CHAN_A |
   1441  1.56.18.1      phil 		    IEEE80211_CHAN_HT40D | IEEE80211_CHAN_VHT80;
   1442  1.56.18.1      phil 	}
   1443  1.56.18.1      phil 
   1444  1.56.18.1      phil 	/* XXX VHT80+80 */
   1445  1.56.18.1      phil 	/* XXX VHT160 */
   1446  1.56.18.1      phil 	flags[nmodes] = 0;
   1447  1.56.18.1      phil }
   1448  1.56.18.1      phil 
   1449  1.56.18.1      phil static void
   1450  1.56.18.1      phil getflags(const uint8_t bands[], uint32_t flags[], int ht40, int vht80)
   1451  1.56.18.1      phil {
   1452  1.56.18.1      phil 
   1453  1.56.18.1      phil 	flags[0] = 0;
   1454  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_11A) ||
   1455  1.56.18.1      phil 	    isset(bands, IEEE80211_MODE_11NA) ||
   1456  1.56.18.1      phil 	    isset(bands, IEEE80211_MODE_VHT_5GHZ)) {
   1457  1.56.18.1      phil 		if (isset(bands, IEEE80211_MODE_11B) ||
   1458  1.56.18.1      phil 		    isset(bands, IEEE80211_MODE_11G) ||
   1459  1.56.18.1      phil 		    isset(bands, IEEE80211_MODE_11NG) ||
   1460  1.56.18.1      phil 		    isset(bands, IEEE80211_MODE_VHT_2GHZ))
   1461  1.56.18.1      phil 			return;
   1462  1.56.18.1      phil 
   1463  1.56.18.1      phil 		getflags_5ghz(bands, flags, ht40, vht80);
   1464  1.56.18.1      phil 	} else
   1465  1.56.18.1      phil 		getflags_2ghz(bands, flags, ht40);
   1466  1.56.18.1      phil }
   1467  1.56.18.1      phil 
   1468  1.56.18.1      phil /*
   1469  1.56.18.1      phil  * Add one 20 MHz channel into specified channel list.
   1470  1.56.18.1      phil  */
   1471  1.56.18.1      phil /* XXX VHT */
   1472  1.56.18.1      phil int
   1473  1.56.18.1      phil ieee80211_add_channel(struct ieee80211_channel chans[], int maxchans,
   1474  1.56.18.1      phil     int *nchans, uint8_t ieee, uint16_t freq, int8_t maxregpower,
   1475  1.56.18.1      phil     uint32_t chan_flags, const uint8_t bands[])
   1476  1.56.18.1      phil {
   1477  1.56.18.1      phil 	uint32_t flags[IEEE80211_MODE_MAX];
   1478  1.56.18.1      phil 	int i, error;
   1479  1.56.18.1      phil 
   1480  1.56.18.1      phil 	getflags(bands, flags, 0, 0);
   1481  1.56.18.1      phil 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
   1482  1.56.18.1      phil 
   1483  1.56.18.1      phil 	error = addchan(chans, maxchans, nchans, ieee, freq, maxregpower,
   1484  1.56.18.1      phil 	    flags[0] | chan_flags);
   1485  1.56.18.1      phil 	for (i = 1; flags[i] != 0 && error == 0; i++) {
   1486  1.56.18.1      phil 		error = copychan_prev(chans, maxchans, nchans,
   1487  1.56.18.1      phil 		    flags[i] | chan_flags);
   1488  1.56.18.1      phil 	}
   1489  1.56.18.1      phil 
   1490  1.56.18.1      phil 	return (error);
   1491  1.56.18.1      phil }
   1492  1.56.18.1      phil 
   1493  1.56.18.1      phil static struct ieee80211_channel *
   1494  1.56.18.1      phil findchannel(struct ieee80211_channel chans[], int nchans, uint16_t freq,
   1495  1.56.18.1      phil     uint32_t flags)
   1496  1.56.18.1      phil {
   1497  1.56.18.1      phil 	struct ieee80211_channel *c;
   1498  1.56.18.1      phil 	int i;
   1499  1.56.18.1      phil 
   1500  1.56.18.1      phil 	flags &= IEEE80211_CHAN_ALLTURBO;
   1501  1.56.18.1      phil 	/* brute force search */
   1502  1.56.18.1      phil 	for (i = 0; i < nchans; i++) {
   1503  1.56.18.1      phil 		c = &chans[i];
   1504  1.56.18.1      phil 		if (c->ic_freq == freq &&
   1505  1.56.18.1      phil 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
   1506  1.56.18.1      phil 			return c;
   1507  1.56.18.1      phil 	}
   1508  1.56.18.1      phil 	return NULL;
   1509  1.56.18.1      phil }
   1510  1.56.18.1      phil 
   1511  1.56.18.1      phil /*
   1512  1.56.18.1      phil  * Add 40 MHz channel pair into specified channel list.
   1513  1.56.18.1      phil  */
   1514  1.56.18.1      phil /* XXX VHT */
   1515  1.56.18.1      phil int
   1516  1.56.18.1      phil ieee80211_add_channel_ht40(struct ieee80211_channel chans[], int maxchans,
   1517  1.56.18.1      phil     int *nchans, uint8_t ieee, int8_t maxregpower, uint32_t flags)
   1518  1.56.18.1      phil {
   1519  1.56.18.1      phil 	struct ieee80211_channel *cent, *extc;
   1520  1.56.18.1      phil 	uint16_t freq;
   1521  1.56.18.1      phil 	int error;
   1522  1.56.18.1      phil 
   1523  1.56.18.1      phil 	freq = ieee80211_ieee2mhz(ieee, flags);
   1524  1.56.18.1      phil 
   1525  1.56.18.1      phil 	/*
   1526  1.56.18.1      phil 	 * Each entry defines an HT40 channel pair; find the
   1527  1.56.18.1      phil 	 * center channel, then the extension channel above.
   1528  1.56.18.1      phil 	 */
   1529  1.56.18.1      phil 	flags |= IEEE80211_CHAN_HT20;
   1530  1.56.18.1      phil 	cent = findchannel(chans, *nchans, freq, flags);
   1531  1.56.18.1      phil 	if (cent == NULL)
   1532  1.56.18.1      phil 		return (EINVAL);
   1533  1.56.18.1      phil 
   1534  1.56.18.1      phil 	extc = findchannel(chans, *nchans, freq + 20, flags);
   1535  1.56.18.1      phil 	if (extc == NULL)
   1536  1.56.18.1      phil 		return (ENOENT);
   1537  1.56.18.1      phil 
   1538  1.56.18.1      phil 	flags &= ~IEEE80211_CHAN_HT;
   1539  1.56.18.1      phil 	error = addchan(chans, maxchans, nchans, cent->ic_ieee, cent->ic_freq,
   1540  1.56.18.1      phil 	    maxregpower, flags | IEEE80211_CHAN_HT40U);
   1541  1.56.18.1      phil 	if (error != 0)
   1542  1.56.18.1      phil 		return (error);
   1543  1.56.18.1      phil 
   1544  1.56.18.1      phil 	error = addchan(chans, maxchans, nchans, extc->ic_ieee, extc->ic_freq,
   1545  1.56.18.1      phil 	    maxregpower, flags | IEEE80211_CHAN_HT40D);
   1546  1.56.18.1      phil 
   1547  1.56.18.1      phil 	return (error);
   1548  1.56.18.1      phil }
   1549  1.56.18.1      phil 
   1550  1.56.18.1      phil /*
   1551  1.56.18.1      phil  * Fetch the center frequency for the primary channel.
   1552  1.56.18.1      phil  */
   1553  1.56.18.1      phil uint32_t
   1554  1.56.18.1      phil ieee80211_get_channel_center_freq(const struct ieee80211_channel *c)
   1555  1.56.18.1      phil {
   1556  1.56.18.1      phil 
   1557  1.56.18.1      phil 	return (c->ic_freq);
   1558  1.56.18.1      phil }
   1559  1.56.18.1      phil 
   1560  1.56.18.1      phil /*
   1561  1.56.18.1      phil  * Fetch the center frequency for the primary BAND channel.
   1562  1.56.18.1      phil  *
   1563  1.56.18.1      phil  * For 5, 10, 20MHz channels it'll be the normally configured channel
   1564  1.56.18.1      phil  * frequency.
   1565  1.56.18.1      phil  *
   1566  1.56.18.1      phil  * For 40MHz, 80MHz, 160Mhz channels it'll the the centre of the
   1567  1.56.18.1      phil  * wide channel, not the centre of the primary channel (that's ic_freq).
   1568  1.56.18.1      phil  *
   1569  1.56.18.1      phil  * For 80+80MHz channels this will be the centre of the primary
   1570  1.56.18.1      phil  * 80MHz channel; the secondary 80MHz channel will be center_freq2().
   1571  1.56.18.1      phil  */
   1572  1.56.18.1      phil uint32_t
   1573  1.56.18.1      phil ieee80211_get_channel_center_freq1(const struct ieee80211_channel *c)
   1574  1.56.18.1      phil {
   1575  1.56.18.1      phil 
   1576  1.56.18.1      phil 	/*
   1577  1.56.18.1      phil 	 * VHT - use the pre-calculated centre frequency
   1578  1.56.18.1      phil 	 * of the given channel.
   1579  1.56.18.1      phil 	 */
   1580  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_VHT(c))
   1581  1.56.18.1      phil 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq1, c->ic_flags));
   1582  1.56.18.1      phil 
   1583  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_HT40U(c)) {
   1584  1.56.18.1      phil 		return (c->ic_freq + 10);
   1585  1.56.18.1      phil 	}
   1586  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_HT40D(c)) {
   1587  1.56.18.1      phil 		return (c->ic_freq - 10);
   1588  1.56.18.1      phil 	}
   1589  1.56.18.1      phil 
   1590  1.56.18.1      phil 	return (c->ic_freq);
   1591  1.56.18.1      phil }
   1592  1.56.18.1      phil 
   1593  1.56.18.1      phil /*
   1594  1.56.18.1      phil  * For now, no 80+80 support; it will likely always return 0.
   1595  1.56.18.1      phil  */
   1596  1.56.18.1      phil uint32_t
   1597  1.56.18.1      phil ieee80211_get_channel_center_freq2(const struct ieee80211_channel *c)
   1598  1.56.18.1      phil {
   1599  1.56.18.1      phil 
   1600  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_VHT(c) && (c->ic_vht_ch_freq2 != 0))
   1601  1.56.18.1      phil 		return (ieee80211_ieee2mhz(c->ic_vht_ch_freq2, c->ic_flags));
   1602  1.56.18.1      phil 
   1603  1.56.18.1      phil 	return (0);
   1604  1.56.18.1      phil }
   1605  1.56.18.1      phil 
   1606  1.56.18.1      phil /*
   1607  1.56.18.1      phil  * Adds channels into specified channel list (ieee[] array must be sorted).
   1608  1.56.18.1      phil  * Channels are already sorted.
   1609  1.56.18.1      phil  */
   1610  1.56.18.1      phil static int
   1611  1.56.18.1      phil add_chanlist(struct ieee80211_channel chans[], int maxchans, int *nchans,
   1612  1.56.18.1      phil     const uint8_t ieee[], int nieee, uint32_t flags[])
   1613  1.56.18.1      phil {
   1614  1.56.18.1      phil 	uint16_t freq;
   1615  1.56.18.1      phil 	int i, j, error;
   1616  1.56.18.1      phil 	int is_vht;
   1617  1.56.18.1      phil 
   1618  1.56.18.1      phil 	for (i = 0; i < nieee; i++) {
   1619  1.56.18.1      phil 		freq = ieee80211_ieee2mhz(ieee[i], flags[0]);
   1620  1.56.18.1      phil 		for (j = 0; flags[j] != 0; j++) {
   1621  1.56.18.1      phil 			/*
   1622  1.56.18.1      phil 			 * Notes:
   1623  1.56.18.1      phil 			 * + HT40 and VHT40 channels occur together, so
   1624  1.56.18.1      phil 			 *   we need to be careful that we actually allow that.
   1625  1.56.18.1      phil 			 * + VHT80, VHT160 will coexist with HT40/VHT40, so
   1626  1.56.18.1      phil 			 *   make sure it's not skipped because of the overlap
   1627  1.56.18.1      phil 			 *   check used for (V)HT40.
   1628  1.56.18.1      phil 			 */
   1629  1.56.18.1      phil 			is_vht = !! (flags[j] & IEEE80211_CHAN_VHT);
   1630  1.56.18.1      phil 
   1631  1.56.18.1      phil 			/*
   1632  1.56.18.1      phil 			 * Test for VHT80.
   1633  1.56.18.1      phil 			 * XXX This is all very broken right now.
   1634  1.56.18.1      phil 			 * What we /should/ do is:
   1635  1.56.18.1      phil 			 *
   1636  1.56.18.1      phil 			 * + check that the frequency is in the list of
   1637  1.56.18.1      phil 			 *   allowed VHT80 ranges; and
   1638  1.56.18.1      phil 			 * + the other 3 channels in the list are actually
   1639  1.56.18.1      phil 			 *   also available.
   1640  1.56.18.1      phil 			 */
   1641  1.56.18.1      phil 			if (is_vht && flags[j] & IEEE80211_CHAN_VHT80)
   1642  1.56.18.1      phil 				if (! is_vht80_valid_freq(freq))
   1643  1.56.18.1      phil 					continue;
   1644  1.56.18.1      phil 
   1645  1.56.18.1      phil 			/*
   1646  1.56.18.1      phil 			 * Test for (V)HT40.
   1647  1.56.18.1      phil 			 *
   1648  1.56.18.1      phil 			 * This is also a fall through from VHT80; as we only
   1649  1.56.18.1      phil 			 * allow a VHT80 channel if the VHT40 combination is
   1650  1.56.18.1      phil 			 * also valid.  If the VHT40 form is not valid then
   1651  1.56.18.1      phil 			 * we certainly can't do VHT80..
   1652  1.56.18.1      phil 			 */
   1653  1.56.18.1      phil 			if (flags[j] & IEEE80211_CHAN_HT40D)
   1654  1.56.18.1      phil 				/*
   1655  1.56.18.1      phil 				 * Can't have a "lower" channel if we are the
   1656  1.56.18.1      phil 				 * first channel.
   1657  1.56.18.1      phil 				 *
   1658  1.56.18.1      phil 				 * Can't have a "lower" channel if it's below/
   1659  1.56.18.1      phil 				 * within 20MHz of the first channel.
   1660  1.56.18.1      phil 				 *
   1661  1.56.18.1      phil 				 * Can't have a "lower" channel if the channel
   1662  1.56.18.1      phil 				 * below it is not 20MHz away.
   1663  1.56.18.1      phil 				 */
   1664  1.56.18.1      phil 				if (i == 0 || ieee[i] < ieee[0] + 4 ||
   1665  1.56.18.1      phil 				    freq - 20 !=
   1666  1.56.18.1      phil 				    ieee80211_ieee2mhz(ieee[i] - 4, flags[j]))
   1667  1.56.18.1      phil 					continue;
   1668  1.56.18.1      phil 			if (flags[j] & IEEE80211_CHAN_HT40U)
   1669  1.56.18.1      phil 				/*
   1670  1.56.18.1      phil 				 * Can't have an "upper" channel if we are
   1671  1.56.18.1      phil 				 * the last channel.
   1672  1.56.18.1      phil 				 *
   1673  1.56.18.1      phil 				 * Can't have an "upper" channel be above the
   1674  1.56.18.1      phil 				 * last channel in the list.
   1675  1.56.18.1      phil 				 *
   1676  1.56.18.1      phil 				 * Can't have an "upper" channel if the next
   1677  1.56.18.1      phil 				 * channel according to the math isn't 20MHz
   1678  1.56.18.1      phil 				 * away.  (Likely for channel 13/14.)
   1679  1.56.18.1      phil 				 */
   1680  1.56.18.1      phil 				if (i == nieee - 1 ||
   1681  1.56.18.1      phil 				    ieee[i] + 4 > ieee[nieee - 1] ||
   1682  1.56.18.1      phil 				    freq + 20 !=
   1683  1.56.18.1      phil 				    ieee80211_ieee2mhz(ieee[i] + 4, flags[j]))
   1684  1.56.18.1      phil 					continue;
   1685  1.56.18.1      phil 
   1686  1.56.18.1      phil 			if (j == 0) {
   1687  1.56.18.1      phil 				error = addchan(chans, maxchans, nchans,
   1688  1.56.18.1      phil 				    ieee[i], freq, 0, flags[j]);
   1689  1.56.18.1      phil 			} else {
   1690  1.56.18.1      phil 				error = copychan_prev(chans, maxchans, nchans,
   1691  1.56.18.1      phil 				    flags[j]);
   1692  1.56.18.1      phil 			}
   1693  1.56.18.1      phil 			if (error != 0)
   1694  1.56.18.1      phil 				return (error);
   1695  1.56.18.1      phil 		}
   1696  1.56.18.1      phil 	}
   1697  1.56.18.1      phil 
   1698  1.56.18.1      phil 	return (0);
   1699  1.56.18.1      phil }
   1700  1.56.18.1      phil 
   1701  1.56.18.1      phil int
   1702  1.56.18.1      phil ieee80211_add_channel_list_2ghz(struct ieee80211_channel chans[], int maxchans,
   1703  1.56.18.1      phil     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
   1704  1.56.18.1      phil     int ht40)
   1705  1.56.18.1      phil {
   1706  1.56.18.1      phil 	uint32_t flags[IEEE80211_MODE_MAX];
   1707  1.56.18.1      phil 
   1708  1.56.18.1      phil 	/* XXX no VHT for now */
   1709  1.56.18.1      phil 	getflags_2ghz(bands, flags, ht40);
   1710  1.56.18.1      phil 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
   1711  1.56.18.1      phil 
   1712  1.56.18.1      phil 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
   1713  1.56.18.1      phil }
   1714  1.56.18.1      phil 
   1715  1.56.18.1      phil int
   1716  1.56.18.1      phil ieee80211_add_channel_list_5ghz(struct ieee80211_channel chans[], int maxchans,
   1717  1.56.18.1      phil     int *nchans, const uint8_t ieee[], int nieee, const uint8_t bands[],
   1718  1.56.18.1      phil     int ht40)
   1719  1.56.18.1      phil {
   1720  1.56.18.1      phil 	uint32_t flags[IEEE80211_MODE_MAX];
   1721  1.56.18.1      phil 	int vht80 = 0;
   1722  1.56.18.1      phil 
   1723  1.56.18.1      phil 	/*
   1724  1.56.18.1      phil 	 * For now, assume VHT == VHT80 support as a minimum.
   1725  1.56.18.1      phil 	 */
   1726  1.56.18.1      phil 	if (isset(bands, IEEE80211_MODE_VHT_5GHZ))
   1727  1.56.18.1      phil 		vht80 = 1;
   1728  1.56.18.1      phil 
   1729  1.56.18.1      phil 	getflags_5ghz(bands, flags, ht40, vht80);
   1730  1.56.18.1      phil 	KASSERT(flags[0] != 0, ("%s: no correct mode provided\n", __func__));
   1731  1.56.18.1      phil 
   1732  1.56.18.1      phil 	return (add_chanlist(chans, maxchans, nchans, ieee, nieee, flags));
   1733  1.56.18.1      phil }
   1734  1.56.18.1      phil 
   1735  1.56.18.1      phil /*
   1736  1.56.18.1      phil  * Locate a channel given a frequency+flags.  We cache
   1737  1.56.18.1      phil  * the previous lookup to optimize switching between two
   1738  1.56.18.1      phil  * channels--as happens with dynamic turbo.
   1739  1.56.18.1      phil  */
   1740  1.56.18.1      phil struct ieee80211_channel *
   1741  1.56.18.1      phil ieee80211_find_channel(struct ieee80211com *ic, int freq, int flags)
   1742  1.56.18.1      phil {
   1743  1.56.18.1      phil 	struct ieee80211_channel *c;
   1744  1.56.18.1      phil 
   1745  1.56.18.1      phil 	flags &= IEEE80211_CHAN_ALLTURBO;
   1746  1.56.18.1      phil 	c = ic->ic_prevchan;
   1747  1.56.18.1      phil 	if (c != NULL && c->ic_freq == freq &&
   1748  1.56.18.1      phil 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
   1749  1.56.18.1      phil 		return c;
   1750  1.56.18.1      phil 	/* brute force search */
   1751  1.56.18.1      phil 	return (findchannel(ic->ic_channels, ic->ic_nchans, freq, flags));
   1752  1.56.18.1      phil }
   1753  1.56.18.1      phil 
   1754  1.56.18.1      phil /*
   1755  1.56.18.1      phil  * Locate a channel given a channel number+flags.  We cache
   1756  1.56.18.1      phil  * the previous lookup to optimize switching between two
   1757  1.56.18.1      phil  * channels--as happens with dynamic turbo.
   1758  1.56.18.1      phil  */
   1759  1.56.18.1      phil struct ieee80211_channel *
   1760  1.56.18.1      phil ieee80211_find_channel_byieee(struct ieee80211com *ic, int ieee, int flags)
   1761  1.56.18.1      phil {
   1762  1.56.18.1      phil 	struct ieee80211_channel *c;
   1763  1.56.18.1      phil 	int i;
   1764  1.56.18.1      phil 
   1765  1.56.18.1      phil 	flags &= IEEE80211_CHAN_ALLTURBO;
   1766  1.56.18.1      phil 	c = ic->ic_prevchan;
   1767  1.56.18.1      phil 	if (c != NULL && c->ic_ieee == ieee &&
   1768  1.56.18.1      phil 	    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
   1769  1.56.18.1      phil 		return c;
   1770  1.56.18.1      phil 	/* brute force search */
   1771  1.56.18.1      phil 	for (i = 0; i < ic->ic_nchans; i++) {
   1772  1.56.18.1      phil 		c = &ic->ic_channels[i];
   1773  1.56.18.1      phil 		if (c->ic_ieee == ieee &&
   1774  1.56.18.1      phil 		    (c->ic_flags & IEEE80211_CHAN_ALLTURBO) == flags)
   1775  1.56.18.1      phil 			return c;
   1776  1.56.18.1      phil 	}
   1777  1.56.18.1      phil 	return NULL;
   1778  1.56.18.1      phil }
   1779  1.56.18.1      phil 
   1780  1.56.18.1      phil /*
   1781  1.56.18.1      phil  * Lookup a channel suitable for the given rx status.
   1782  1.56.18.1      phil  *
   1783  1.56.18.1      phil  * This is used to find a channel for a frame (eg beacon, probe
   1784  1.56.18.1      phil  * response) based purely on the received PHY information.
   1785  1.56.18.1      phil  *
   1786  1.56.18.1      phil  * For now it tries to do it based on R_FREQ / R_IEEE.
   1787  1.56.18.1      phil  * This is enough for 11bg and 11a (and thus 11ng/11na)
   1788  1.56.18.1      phil  * but it will not be enough for GSM, PSB channels and the
   1789  1.56.18.1      phil  * like.  It also doesn't know about legacy-turbog and
   1790  1.56.18.1      phil  * legacy-turbo modes, which some offload NICs actually
   1791  1.56.18.1      phil  * support in weird ways.
   1792  1.56.18.1      phil  *
   1793  1.56.18.1      phil  * Takes the ic and rxstatus; returns the channel or NULL
   1794  1.56.18.1      phil  * if not found.
   1795  1.56.18.1      phil  *
   1796  1.56.18.1      phil  * XXX TODO: Add support for that when the need arises.
   1797  1.56.18.1      phil  */
   1798  1.56.18.1      phil struct ieee80211_channel *
   1799  1.56.18.1      phil ieee80211_lookup_channel_rxstatus(struct ieee80211vap *vap,
   1800  1.56.18.1      phil     const struct ieee80211_rx_stats *rxs)
   1801  1.56.18.1      phil {
   1802  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1803  1.56.18.1      phil 	uint32_t flags;
   1804  1.56.18.1      phil 	struct ieee80211_channel *c;
   1805  1.56.18.1      phil 
   1806  1.56.18.1      phil 	if (rxs == NULL)
   1807  1.56.18.1      phil 		return (NULL);
   1808  1.56.18.1      phil 
   1809  1.56.18.1      phil 	/*
   1810  1.56.18.1      phil 	 * Strictly speaking we only use freq for now,
   1811  1.56.18.1      phil 	 * however later on we may wish to just store
   1812  1.56.18.1      phil 	 * the ieee for verification.
   1813  1.56.18.1      phil 	 */
   1814  1.56.18.1      phil 	if ((rxs->r_flags & IEEE80211_R_FREQ) == 0)
   1815  1.56.18.1      phil 		return (NULL);
   1816  1.56.18.1      phil 	if ((rxs->r_flags & IEEE80211_R_IEEE) == 0)
   1817  1.56.18.1      phil 		return (NULL);
   1818  1.56.18.1      phil 
   1819  1.56.18.1      phil 	/*
   1820  1.56.18.1      phil 	 * If the rx status contains a valid ieee/freq, then
   1821  1.56.18.1      phil 	 * ensure we populate the correct channel information
   1822  1.56.18.1      phil 	 * in rxchan before passing it up to the scan infrastructure.
   1823  1.56.18.1      phil 	 * Offload NICs will pass up beacons from all channels
   1824  1.56.18.1      phil 	 * during background scans.
   1825  1.56.18.1      phil 	 */
   1826  1.56.18.1      phil 
   1827  1.56.18.1      phil 	/* Determine a band */
   1828  1.56.18.1      phil 	/* XXX should be done by the driver? */
   1829  1.56.18.1      phil 	if (rxs->c_freq < 3000) {
   1830  1.56.18.1      phil 		flags = IEEE80211_CHAN_G;
   1831  1.56.18.1      phil 	} else {
   1832  1.56.18.1      phil 		flags = IEEE80211_CHAN_A;
   1833  1.56.18.1      phil 	}
   1834  1.56.18.1      phil 
   1835  1.56.18.1      phil 	/* Channel lookup */
   1836  1.56.18.1      phil 	c = ieee80211_find_channel(ic, rxs->c_freq, flags);
   1837  1.56.18.1      phil 
   1838  1.56.18.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_INPUT,
   1839  1.56.18.1      phil 	    "%s: freq=%d, ieee=%d, flags=0x%08x; c=%p\n",
   1840  1.56.18.1      phil 	    __func__,
   1841  1.56.18.1      phil 	    (int) rxs->c_freq,
   1842  1.56.18.1      phil 	    (int) rxs->c_ieee,
   1843  1.56.18.1      phil 	    flags,
   1844  1.56.18.1      phil 	    c);
   1845  1.56.18.1      phil 
   1846  1.56.18.1      phil 	return (c);
   1847  1.56.18.1      phil }
   1848  1.56.18.1      phil 
   1849  1.56.18.1      phil static void
   1850  1.56.18.1      phil addmedia(struct ifmedia *media, int caps, int addsta, int mode, int mword)
   1851  1.56.18.1      phil {
   1852  1.56.18.1      phil #define	ADD(_ic, _s, _o) \
   1853  1.56.18.1      phil 	ifmedia_add(media, \
   1854  1.56.18.1      phil 		IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
   1855  1.56.18.1      phil 	static const u_int mopts[IEEE80211_MODE_MAX] = {
   1856  1.56.18.1      phil 	    [IEEE80211_MODE_AUTO]	= IFM_AUTO,
   1857  1.56.18.1      phil 	    [IEEE80211_MODE_11A]	= IFM_IEEE80211_11A,
   1858  1.56.18.1      phil 	    [IEEE80211_MODE_11B]	= IFM_IEEE80211_11B,
   1859  1.56.18.1      phil 	    [IEEE80211_MODE_11G]	= IFM_IEEE80211_11G,
   1860  1.56.18.1      phil 	    [IEEE80211_MODE_FH]		= IFM_IEEE80211_FH,
   1861  1.56.18.1      phil 	    [IEEE80211_MODE_TURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
   1862  1.56.18.1      phil 	    [IEEE80211_MODE_TURBO_G]	= IFM_IEEE80211_11G|IFM_IEEE80211_TURBO,
   1863  1.56.18.1      phil 	    [IEEE80211_MODE_STURBO_A]	= IFM_IEEE80211_11A|IFM_IEEE80211_TURBO,
   1864  1.56.18.1      phil 	    [IEEE80211_MODE_HALF]	= IFM_IEEE80211_11A,	/* XXX */
   1865  1.56.18.1      phil 	    [IEEE80211_MODE_QUARTER]	= IFM_IEEE80211_11A,	/* XXX */
   1866  1.56.18.1      phil 	    [IEEE80211_MODE_11NA]	= IFM_IEEE80211_11NA,
   1867  1.56.18.1      phil 	    [IEEE80211_MODE_11NG]	= IFM_IEEE80211_11NG,
   1868  1.56.18.1      phil 	    [IEEE80211_MODE_VHT_2GHZ]	= IFM_IEEE80211_VHT2G,
   1869  1.56.18.1      phil 	    [IEEE80211_MODE_VHT_5GHZ]	= IFM_IEEE80211_VHT5G,
   1870  1.56.18.1      phil 	};
   1871  1.56.18.1      phil 	u_int mopt;
   1872  1.56.18.1      phil 
   1873  1.56.18.1      phil 	mopt = mopts[mode];
   1874  1.56.18.1      phil 	if (addsta)
   1875  1.56.18.1      phil 		ADD(ic, mword, mopt);	/* STA mode has no cap */
   1876  1.56.18.1      phil 	if (caps & IEEE80211_C_IBSS)
   1877  1.56.18.1      phil 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC);
   1878  1.56.18.1      phil 	if (caps & IEEE80211_C_HOSTAP)
   1879  1.56.18.1      phil 		ADD(media, mword, mopt | IFM_IEEE80211_HOSTAP);
   1880  1.56.18.1      phil 	if (caps & IEEE80211_C_AHDEMO)
   1881  1.56.18.1      phil 		ADD(media, mword, mopt | IFM_IEEE80211_ADHOC | IFM_FLAG0);
   1882  1.56.18.1      phil 	if (caps & IEEE80211_C_MONITOR)
   1883  1.56.18.1      phil 		ADD(media, mword, mopt | IFM_IEEE80211_MONITOR);
   1884  1.56.18.1      phil 	if (caps & IEEE80211_C_WDS)
   1885  1.56.18.1      phil 		ADD(media, mword, mopt | IFM_IEEE80211_WDS);
   1886  1.56.18.1      phil 	if (caps & IEEE80211_C_MBSS)
   1887  1.56.18.1      phil 		ADD(media, mword, mopt | IFM_IEEE80211_MBSS);
   1888  1.56.18.1      phil #undef ADD
   1889  1.56.18.1      phil }
   1890  1.56.18.1      phil 
   1891        1.1    dyoung /*
   1892        1.1    dyoung  * Setup the media data structures according to the channel and
   1893  1.56.18.1      phil  * rate tables.
   1894        1.1    dyoung  */
   1895  1.56.18.1      phil static int
   1896  1.56.18.1      phil ieee80211_media_setup(struct ieee80211com *ic,
   1897  1.56.18.1      phil 	struct ifmedia *media, int caps, int addsta,
   1898        1.1    dyoung 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
   1899        1.1    dyoung {
   1900  1.56.18.1      phil 	int i, j, rate, maxrate, mword, r;
   1901  1.56.18.1      phil 	enum ieee80211_phymode mode;
   1902       1.51    dyoung 	const struct ieee80211_rateset *rs;
   1903        1.1    dyoung 	struct ieee80211_rateset allrates;
   1904        1.1    dyoung 
   1905        1.1    dyoung 	/*
   1906        1.1    dyoung 	 * Fill in media characteristics.
   1907        1.1    dyoung 	 */
   1908  1.56.18.1      phil 	ifmedia_init(media, 0, media_change, media_stat);
   1909        1.1    dyoung 	maxrate = 0;
   1910  1.56.18.1      phil 	/*
   1911  1.56.18.1      phil 	 * Add media for legacy operating modes.
   1912  1.56.18.1      phil 	 */
   1913        1.1    dyoung 	memset(&allrates, 0, sizeof(allrates));
   1914  1.56.18.1      phil 	for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_11NA; mode++) {
   1915  1.56.18.1      phil 		if (isclr(ic->ic_modecaps, mode))
   1916        1.1    dyoung 			continue;
   1917  1.56.18.1      phil 		addmedia(media, caps, addsta, mode, IFM_AUTO);
   1918        1.1    dyoung 		if (mode == IEEE80211_MODE_AUTO)
   1919        1.1    dyoung 			continue;
   1920        1.1    dyoung 		rs = &ic->ic_sup_rates[mode];
   1921        1.1    dyoung 		for (i = 0; i < rs->rs_nrates; i++) {
   1922        1.1    dyoung 			rate = rs->rs_rates[i];
   1923        1.1    dyoung 			mword = ieee80211_rate2media(ic, rate, mode);
   1924        1.1    dyoung 			if (mword == 0)
   1925        1.1    dyoung 				continue;
   1926  1.56.18.1      phil 			addmedia(media, caps, addsta, mode, mword);
   1927        1.1    dyoung 			/*
   1928  1.56.18.1      phil 			 * Add legacy rate to the collection of all rates.
   1929        1.1    dyoung 			 */
   1930        1.1    dyoung 			r = rate & IEEE80211_RATE_VAL;
   1931        1.1    dyoung 			for (j = 0; j < allrates.rs_nrates; j++)
   1932        1.1    dyoung 				if (allrates.rs_rates[j] == r)
   1933        1.1    dyoung 					break;
   1934        1.1    dyoung 			if (j == allrates.rs_nrates) {
   1935        1.1    dyoung 				/* unique, add to the set */
   1936        1.1    dyoung 				allrates.rs_rates[j] = r;
   1937        1.1    dyoung 				allrates.rs_nrates++;
   1938        1.1    dyoung 			}
   1939        1.1    dyoung 			rate = (rate & IEEE80211_RATE_VAL) / 2;
   1940        1.1    dyoung 			if (rate > maxrate)
   1941        1.1    dyoung 				maxrate = rate;
   1942        1.1    dyoung 		}
   1943        1.1    dyoung 	}
   1944        1.1    dyoung 	for (i = 0; i < allrates.rs_nrates; i++) {
   1945        1.1    dyoung 		mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
   1946        1.1    dyoung 				IEEE80211_MODE_AUTO);
   1947        1.1    dyoung 		if (mword == 0)
   1948        1.1    dyoung 			continue;
   1949  1.56.18.1      phil 		/* NB: remove media options from mword */
   1950  1.56.18.1      phil 		addmedia(media, caps, addsta,
   1951  1.56.18.1      phil 		    IEEE80211_MODE_AUTO, IFM_SUBTYPE(mword));
   1952  1.56.18.1      phil 	}
   1953  1.56.18.1      phil 	/*
   1954  1.56.18.1      phil 	 * Add HT/11n media.  Note that we do not have enough
   1955  1.56.18.1      phil 	 * bits in the media subtype to express the MCS so we
   1956  1.56.18.1      phil 	 * use a "placeholder" media subtype and any fixed MCS
   1957  1.56.18.1      phil 	 * must be specified with a different mechanism.
   1958  1.56.18.1      phil 	 */
   1959  1.56.18.1      phil 	for (; mode <= IEEE80211_MODE_11NG; mode++) {
   1960  1.56.18.1      phil 		if (isclr(ic->ic_modecaps, mode))
   1961  1.56.18.1      phil 			continue;
   1962  1.56.18.1      phil 		addmedia(media, caps, addsta, mode, IFM_AUTO);
   1963  1.56.18.1      phil 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_MCS);
   1964  1.56.18.1      phil 	}
   1965  1.56.18.1      phil 	if (isset(ic->ic_modecaps, IEEE80211_MODE_11NA) ||
   1966  1.56.18.1      phil 	    isset(ic->ic_modecaps, IEEE80211_MODE_11NG)) {
   1967  1.56.18.1      phil 		addmedia(media, caps, addsta,
   1968  1.56.18.1      phil 		    IEEE80211_MODE_AUTO, IFM_IEEE80211_MCS);
   1969  1.56.18.1      phil 		i = ic->ic_txstream * 8 - 1;
   1970  1.56.18.1      phil 		if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) &&
   1971  1.56.18.1      phil 		    (ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI40))
   1972  1.56.18.1      phil 			rate = ieee80211_htrates[i].ht40_rate_400ns;
   1973  1.56.18.1      phil 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40))
   1974  1.56.18.1      phil 			rate = ieee80211_htrates[i].ht40_rate_800ns;
   1975  1.56.18.1      phil 		else if ((ic->ic_htcaps & IEEE80211_HTCAP_SHORTGI20))
   1976  1.56.18.1      phil 			rate = ieee80211_htrates[i].ht20_rate_400ns;
   1977  1.56.18.1      phil 		else
   1978  1.56.18.1      phil 			rate = ieee80211_htrates[i].ht20_rate_800ns;
   1979  1.56.18.1      phil 		if (rate > maxrate)
   1980  1.56.18.1      phil 			maxrate = rate;
   1981        1.1    dyoung 	}
   1982       1.37    dyoung 
   1983  1.56.18.1      phil 	/*
   1984  1.56.18.1      phil 	 * Add VHT media.
   1985  1.56.18.1      phil 	 */
   1986  1.56.18.1      phil 	for (; mode <= IEEE80211_MODE_VHT_5GHZ; mode++) {
   1987  1.56.18.1      phil 		if (isclr(ic->ic_modecaps, mode))
   1988  1.56.18.1      phil 			continue;
   1989  1.56.18.1      phil 		addmedia(media, caps, addsta, mode, IFM_AUTO);
   1990  1.56.18.1      phil 		addmedia(media, caps, addsta, mode, IFM_IEEE80211_VHT);
   1991  1.56.18.1      phil 
   1992  1.56.18.1      phil 		/* XXX TODO: VHT maxrate */
   1993  1.56.18.1      phil 	}
   1994  1.56.18.1      phil 
   1995  1.56.18.1      phil 	return maxrate;
   1996  1.56.18.1      phil }
   1997  1.56.18.1      phil 
   1998  1.56.18.1      phil /* XXX inline or eliminate? */
   1999  1.56.18.1      phil const struct ieee80211_rateset *
   2000  1.56.18.1      phil ieee80211_get_suprates(struct ieee80211com *ic, const struct ieee80211_channel *c)
   2001  1.56.18.1      phil {
   2002  1.56.18.1      phil 	/* XXX does this work for 11ng basic rates? */
   2003  1.56.18.1      phil 	return &ic->ic_sup_rates[ieee80211_chan2mode(c)];
   2004  1.56.18.1      phil }
   2005  1.56.18.1      phil 
   2006  1.56.18.1      phil /* XXX inline or eliminate? */
   2007  1.56.18.1      phil const struct ieee80211_htrateset *
   2008  1.56.18.1      phil ieee80211_get_suphtrates(struct ieee80211com *ic,
   2009  1.56.18.1      phil     const struct ieee80211_channel *c)
   2010  1.56.18.1      phil {
   2011  1.56.18.1      phil 	return &ic->ic_sup_htrates;
   2012        1.1    dyoung }
   2013        1.1    dyoung 
   2014       1.37    dyoung void
   2015  1.56.18.8    martin ieee80211_media_init(struct ieee80211com *ic,
   2016  1.56.18.8    martin 	ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
   2017  1.56.18.8    martin {
   2018  1.56.18.8    martin 
   2019  1.56.18.9       nat 	struct ieee80211vap *vap;
   2020  1.56.18.9       nat 
   2021  1.56.18.9       nat 	vap = TAILQ_FIRST(&ic->ic_vaps);
   2022  1.56.18.9       nat 	KASSERT(vap != NULL, ("media vap is null"));
   2023  1.56.18.9       nat 
   2024  1.56.18.9       nat 	ifmedia_init_with_lock(&vap->iv_media, 0, media_change,
   2025  1.56.18.9       nat 	    media_stat, NULL);
   2026  1.56.18.8    martin }
   2027  1.56.18.8    martin 
   2028  1.56.18.8    martin void
   2029       1.37    dyoung ieee80211_announce(struct ieee80211com *ic)
   2030       1.37    dyoung {
   2031  1.56.18.1      phil 	int i, rate, mword;
   2032  1.56.18.1      phil 	enum ieee80211_phymode mode;
   2033  1.56.18.1      phil 	const struct ieee80211_rateset *rs;
   2034       1.37    dyoung 
   2035  1.56.18.1      phil 	/* NB: skip AUTO since it has no rates */
   2036  1.56.18.1      phil 	for (mode = IEEE80211_MODE_AUTO+1; mode < IEEE80211_MODE_11NA; mode++) {
   2037  1.56.18.1      phil 		if (isclr(ic->ic_modecaps, mode))
   2038       1.37    dyoung 			continue;
   2039  1.56.18.9       nat 		//aprint_debug("%s: %s rates: ", ifp->if_xname,
   2040  1.56.18.9       nat 		 //   ieee80211_phymode_name[mode]);
   2041  1.56.18.1      phil 		ic_printf(ic, "%s rates: ", ieee80211_phymode_name[mode]);
   2042       1.37    dyoung 		rs = &ic->ic_sup_rates[mode];
   2043       1.37    dyoung 		for (i = 0; i < rs->rs_nrates; i++) {
   2044  1.56.18.1      phil 			mword = ieee80211_rate2media(ic, rs->rs_rates[i], mode);
   2045       1.37    dyoung 			if (mword == 0)
   2046       1.37    dyoung 				continue;
   2047  1.56.18.1      phil 			rate = ieee80211_media2rate(mword);
   2048  1.56.18.8    martin 			aprint_debug("%s%d%sMbps", (i != 0 ? " " : ""),
   2049  1.56.18.1      phil 			    rate / 2, ((rate & 0x1) != 0 ? ".5" : ""));
   2050       1.37    dyoung 		}
   2051  1.56.18.8    martin 		aprint_debug("\n");
   2052       1.37    dyoung 	}
   2053  1.56.18.1      phil 	ieee80211_ht_announce(ic);
   2054  1.56.18.1      phil 	ieee80211_vht_announce(ic);
   2055       1.37    dyoung }
   2056       1.37    dyoung 
   2057  1.56.18.1      phil void
   2058  1.56.18.1      phil ieee80211_announce_channels(struct ieee80211com *ic)
   2059       1.37    dyoung {
   2060  1.56.18.1      phil 	const struct ieee80211_channel *c;
   2061  1.56.18.1      phil 	char type;
   2062  1.56.18.1      phil 	int i, cw;
   2063       1.37    dyoung 
   2064  1.56.18.1      phil 	printf("Chan  Freq  CW  RegPwr  MinPwr  MaxPwr\n");
   2065  1.56.18.1      phil 	for (i = 0; i < ic->ic_nchans; i++) {
   2066  1.56.18.1      phil 		c = &ic->ic_channels[i];
   2067  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_ST(c))
   2068  1.56.18.1      phil 			type = 'S';
   2069  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_108A(c))
   2070  1.56.18.1      phil 			type = 'T';
   2071  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_108G(c))
   2072  1.56.18.1      phil 			type = 'G';
   2073  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_HT(c))
   2074  1.56.18.1      phil 			type = 'n';
   2075  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_A(c))
   2076  1.56.18.1      phil 			type = 'a';
   2077  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_ANYG(c))
   2078  1.56.18.1      phil 			type = 'g';
   2079  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_B(c))
   2080  1.56.18.1      phil 			type = 'b';
   2081  1.56.18.1      phil 		else
   2082  1.56.18.1      phil 			type = 'f';
   2083  1.56.18.1      phil 		if (IEEE80211_IS_CHAN_HT40(c) || IEEE80211_IS_CHAN_TURBO(c))
   2084  1.56.18.1      phil 			cw = 40;
   2085  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_HALF(c))
   2086  1.56.18.1      phil 			cw = 10;
   2087  1.56.18.1      phil 		else if (IEEE80211_IS_CHAN_QUARTER(c))
   2088  1.56.18.1      phil 			cw = 5;
   2089  1.56.18.1      phil 		else
   2090  1.56.18.1      phil 			cw = 20;
   2091  1.56.18.1      phil 		printf("%4d  %4d%c %2d%c %6d  %4d.%d  %4d.%d\n"
   2092  1.56.18.1      phil 			, c->ic_ieee, c->ic_freq, type
   2093  1.56.18.1      phil 			, cw
   2094  1.56.18.1      phil 			, IEEE80211_IS_CHAN_HT40U(c) ? '+' :
   2095  1.56.18.1      phil 			  IEEE80211_IS_CHAN_HT40D(c) ? '-' : ' '
   2096  1.56.18.1      phil 			, c->ic_maxregpower
   2097  1.56.18.1      phil 			, c->ic_minpower / 2, c->ic_minpower & 1 ? 5 : 0
   2098  1.56.18.1      phil 			, c->ic_maxpower / 2, c->ic_maxpower & 1 ? 5 : 0
   2099  1.56.18.1      phil 		);
   2100  1.56.18.1      phil 	}
   2101       1.37    dyoung }
   2102       1.37    dyoung 
   2103  1.56.18.1      phil static int
   2104  1.56.18.1      phil media2mode(const struct ifmedia_entry *ime, uint32_t flags, uint16_t *mode)
   2105        1.1    dyoung {
   2106        1.1    dyoung 	switch (IFM_MODE(ime->ifm_media)) {
   2107        1.1    dyoung 	case IFM_IEEE80211_11A:
   2108  1.56.18.1      phil 		*mode = IEEE80211_MODE_11A;
   2109        1.1    dyoung 		break;
   2110        1.1    dyoung 	case IFM_IEEE80211_11B:
   2111  1.56.18.1      phil 		*mode = IEEE80211_MODE_11B;
   2112        1.1    dyoung 		break;
   2113        1.1    dyoung 	case IFM_IEEE80211_11G:
   2114  1.56.18.1      phil 		*mode = IEEE80211_MODE_11G;
   2115        1.1    dyoung 		break;
   2116        1.5    dyoung 	case IFM_IEEE80211_FH:
   2117  1.56.18.1      phil 		*mode = IEEE80211_MODE_FH;
   2118  1.56.18.1      phil 		break;
   2119  1.56.18.1      phil 	case IFM_IEEE80211_11NA:
   2120  1.56.18.1      phil 		*mode = IEEE80211_MODE_11NA;
   2121  1.56.18.1      phil 		break;
   2122  1.56.18.1      phil 	case IFM_IEEE80211_11NG:
   2123  1.56.18.1      phil 		*mode = IEEE80211_MODE_11NG;
   2124        1.5    dyoung 		break;
   2125        1.1    dyoung 	case IFM_AUTO:
   2126  1.56.18.1      phil 		*mode = IEEE80211_MODE_AUTO;
   2127        1.1    dyoung 		break;
   2128        1.1    dyoung 	default:
   2129  1.56.18.1      phil 		return 0;
   2130        1.1    dyoung 	}
   2131        1.1    dyoung 	/*
   2132       1.37    dyoung 	 * Turbo mode is an ``option''.
   2133       1.37    dyoung 	 * XXX does not apply to AUTO
   2134        1.1    dyoung 	 */
   2135        1.1    dyoung 	if (ime->ifm_media & IFM_IEEE80211_TURBO) {
   2136  1.56.18.1      phil 		if (*mode == IEEE80211_MODE_11A) {
   2137  1.56.18.1      phil 			if (flags & IEEE80211_F_TURBOP)
   2138  1.56.18.1      phil 				*mode = IEEE80211_MODE_TURBO_A;
   2139  1.56.18.1      phil 			else
   2140  1.56.18.1      phil 				*mode = IEEE80211_MODE_STURBO_A;
   2141  1.56.18.1      phil 		} else if (*mode == IEEE80211_MODE_11G)
   2142  1.56.18.1      phil 			*mode = IEEE80211_MODE_TURBO_G;
   2143       1.37    dyoung 		else
   2144  1.56.18.1      phil 			return 0;
   2145        1.1    dyoung 	}
   2146  1.56.18.1      phil 	/* XXX HT40 +/- */
   2147  1.56.18.1      phil 	return 1;
   2148  1.56.18.1      phil }
   2149        1.1    dyoung 
   2150  1.56.18.1      phil /*
   2151  1.56.18.1      phil  * Handle a media change request on the vap interface.
   2152  1.56.18.1      phil  */
   2153  1.56.18.1      phil int
   2154  1.56.18.1      phil ieee80211_media_change(struct ifnet *ifp)
   2155  1.56.18.1      phil {
   2156  1.56.18.1      phil 	struct ieee80211vap *vap = ifp->if_softc;
   2157  1.56.18.1      phil 	struct ifmedia_entry *ime = vap->iv_media.ifm_cur;
   2158  1.56.18.1      phil 	uint16_t newmode;
   2159        1.1    dyoung 
   2160  1.56.18.1      phil 	if (!media2mode(ime, vap->iv_flags, &newmode))
   2161  1.56.18.1      phil 		return EINVAL;
   2162  1.56.18.1      phil 	if (vap->iv_des_mode != newmode) {
   2163  1.56.18.1      phil 		vap->iv_des_mode = newmode;
   2164  1.56.18.1      phil 		/* XXX kick state machine if up+running */
   2165        1.1    dyoung 	}
   2166  1.56.18.1      phil 	return 0;
   2167  1.56.18.1      phil }
   2168        1.1    dyoung 
   2169  1.56.18.1      phil /*
   2170  1.56.18.1      phil  * Common code to calculate the media status word
   2171  1.56.18.1      phil  * from the operating mode and channel state.
   2172  1.56.18.1      phil  */
   2173  1.56.18.1      phil static int
   2174  1.56.18.1      phil media_status(enum ieee80211_opmode opmode, const struct ieee80211_channel *chan)
   2175  1.56.18.1      phil {
   2176  1.56.18.1      phil 	int status;
   2177        1.1    dyoung 
   2178  1.56.18.1      phil 	status = IFM_IEEE80211;
   2179  1.56.18.1      phil 	switch (opmode) {
   2180  1.56.18.1      phil 	case IEEE80211_M_STA:
   2181  1.56.18.1      phil 		break;
   2182  1.56.18.1      phil 	case IEEE80211_M_IBSS:
   2183  1.56.18.1      phil 		status |= IFM_IEEE80211_ADHOC;
   2184  1.56.18.1      phil 		break;
   2185  1.56.18.1      phil 	case IEEE80211_M_HOSTAP:
   2186  1.56.18.1      phil 		status |= IFM_IEEE80211_HOSTAP;
   2187  1.56.18.1      phil 		break;
   2188  1.56.18.1      phil 	case IEEE80211_M_MONITOR:
   2189  1.56.18.1      phil 		status |= IFM_IEEE80211_MONITOR;
   2190  1.56.18.1      phil 		break;
   2191  1.56.18.1      phil 	case IEEE80211_M_AHDEMO:
   2192  1.56.18.1      phil 		status |= IFM_IEEE80211_ADHOC | IFM_FLAG0;
   2193  1.56.18.1      phil 		break;
   2194  1.56.18.1      phil 	case IEEE80211_M_WDS:
   2195  1.56.18.1      phil 		status |= IFM_IEEE80211_WDS;
   2196  1.56.18.1      phil 		break;
   2197  1.56.18.1      phil 	case IEEE80211_M_MBSS:
   2198  1.56.18.1      phil 		status |= IFM_IEEE80211_MBSS;
   2199  1.56.18.1      phil 		break;
   2200  1.56.18.1      phil 	}
   2201  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_HTA(chan)) {
   2202  1.56.18.1      phil 		status |= IFM_IEEE80211_11NA;
   2203  1.56.18.1      phil 	} else if (IEEE80211_IS_CHAN_HTG(chan)) {
   2204  1.56.18.1      phil 		status |= IFM_IEEE80211_11NG;
   2205  1.56.18.1      phil 	} else if (IEEE80211_IS_CHAN_A(chan)) {
   2206  1.56.18.1      phil 		status |= IFM_IEEE80211_11A;
   2207  1.56.18.1      phil 	} else if (IEEE80211_IS_CHAN_B(chan)) {
   2208  1.56.18.1      phil 		status |= IFM_IEEE80211_11B;
   2209  1.56.18.1      phil 	} else if (IEEE80211_IS_CHAN_ANYG(chan)) {
   2210  1.56.18.1      phil 		status |= IFM_IEEE80211_11G;
   2211  1.56.18.1      phil 	} else if (IEEE80211_IS_CHAN_FHSS(chan)) {
   2212  1.56.18.1      phil 		status |= IFM_IEEE80211_FH;
   2213        1.1    dyoung 	}
   2214  1.56.18.1      phil 	/* XXX else complain? */
   2215        1.1    dyoung 
   2216  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_TURBO(chan))
   2217  1.56.18.1      phil 		status |= IFM_IEEE80211_TURBO;
   2218  1.56.18.1      phil #if 0
   2219  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_HT20(chan))
   2220  1.56.18.1      phil 		status |= IFM_IEEE80211_HT20;
   2221  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_HT40(chan))
   2222  1.56.18.1      phil 		status |= IFM_IEEE80211_HT40;
   2223        1.1    dyoung #endif
   2224  1.56.18.1      phil 	return status;
   2225        1.1    dyoung }
   2226        1.1    dyoung 
   2227        1.1    dyoung void
   2228        1.1    dyoung ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
   2229        1.1    dyoung {
   2230  1.56.18.1      phil 	struct ieee80211vap *vap = ifp->if_softc;
   2231  1.56.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2232  1.56.18.1      phil 	enum ieee80211_phymode mode;
   2233        1.1    dyoung 
   2234        1.1    dyoung 	imr->ifm_status = IFM_AVALID;
   2235  1.56.18.1      phil 	/*
   2236  1.56.18.1      phil 	 * NB: use the current channel's mode to lock down a xmit
   2237  1.56.18.1      phil 	 * rate only when running; otherwise we may have a mismatch
   2238  1.56.18.1      phil 	 * in which case the rate will not be convertible.
   2239  1.56.18.1      phil 	 */
   2240  1.56.18.1      phil 	if (vap->iv_state == IEEE80211_S_RUN ||
   2241  1.56.18.1      phil 	    vap->iv_state == IEEE80211_S_SLEEP) {
   2242        1.1    dyoung 		imr->ifm_status |= IFM_ACTIVE;
   2243  1.56.18.1      phil 		mode = ieee80211_chan2mode(ic->ic_curchan);
   2244  1.56.18.1      phil 	} else
   2245  1.56.18.1      phil 		mode = IEEE80211_MODE_AUTO;
   2246  1.56.18.1      phil 	imr->ifm_active = media_status(vap->iv_opmode, ic->ic_curchan);
   2247       1.37    dyoung 	/*
   2248       1.37    dyoung 	 * Calculate a current rate if possible.
   2249       1.37    dyoung 	 */
   2250  1.56.18.1      phil 	if (vap->iv_txparms[mode].ucastrate != IEEE80211_FIXED_RATE_NONE) {
   2251       1.37    dyoung 		/*
   2252       1.37    dyoung 		 * A fixed rate is set, report that.
   2253       1.37    dyoung 		 */
   2254       1.37    dyoung 		imr->ifm_active |= ieee80211_rate2media(ic,
   2255  1.56.18.1      phil 			vap->iv_txparms[mode].ucastrate, mode);
   2256  1.56.18.1      phil 	} else if (vap->iv_opmode == IEEE80211_M_STA) {
   2257       1.37    dyoung 		/*
   2258       1.37    dyoung 		 * In station mode report the current transmit rate.
   2259       1.37    dyoung 		 */
   2260       1.37    dyoung 		imr->ifm_active |= ieee80211_rate2media(ic,
   2261  1.56.18.1      phil 			vap->iv_bss->ni_txrate, mode);
   2262       1.37    dyoung 	} else
   2263       1.37    dyoung 		imr->ifm_active |= IFM_AUTO;
   2264  1.56.18.1      phil 	if (imr->ifm_status & IFM_ACTIVE)
   2265  1.56.18.1      phil 		imr->ifm_current = imr->ifm_active;
   2266        1.1    dyoung }
   2267        1.1    dyoung 
   2268       1.49    cegger /*
   2269        1.1    dyoung  * Set the current phy mode and recalculate the active channel
   2270        1.1    dyoung  * set based on the available channels for this mode.  Also
   2271        1.1    dyoung  * select a new default/current channel if the current one is
   2272        1.1    dyoung  * inappropriate for this mode.
   2273        1.1    dyoung  */
   2274        1.1    dyoung int
   2275        1.1    dyoung ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
   2276        1.1    dyoung {
   2277        1.1    dyoung 	/*
   2278  1.56.18.1      phil 	 * Adjust basic rates in 11b/11g supported rate set.
   2279  1.56.18.1      phil 	 * Note that if operating on a hal/quarter rate channel
   2280  1.56.18.1      phil 	 * this is a noop as those rates sets are different
   2281  1.56.18.1      phil 	 * and used instead.
   2282        1.1    dyoung 	 */
   2283  1.56.18.1      phil 	if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11B)
   2284  1.56.18.1      phil 		ieee80211_setbasicrates(&ic->ic_sup_rates[mode], mode);
   2285        1.1    dyoung 
   2286        1.1    dyoung 	ic->ic_curmode = mode;
   2287       1.37    dyoung 	ieee80211_reset_erp(ic);	/* reset ERP state */
   2288       1.37    dyoung 
   2289        1.1    dyoung 	return 0;
   2290        1.1    dyoung }
   2291        1.1    dyoung 
   2292        1.1    dyoung /*
   2293  1.56.18.1      phil  * Return the phy mode for with the specified channel.
   2294        1.1    dyoung  */
   2295        1.1    dyoung enum ieee80211_phymode
   2296  1.56.18.1      phil ieee80211_chan2mode(const struct ieee80211_channel *chan)
   2297        1.1    dyoung {
   2298  1.56.18.1      phil 
   2299  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_VHT_2GHZ(chan))
   2300  1.56.18.1      phil 		return IEEE80211_MODE_VHT_2GHZ;
   2301  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_VHT_5GHZ(chan))
   2302  1.56.18.1      phil 		return IEEE80211_MODE_VHT_5GHZ;
   2303  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_HTA(chan))
   2304  1.56.18.1      phil 		return IEEE80211_MODE_11NA;
   2305  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_HTG(chan))
   2306  1.56.18.1      phil 		return IEEE80211_MODE_11NG;
   2307  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_108G(chan))
   2308  1.56.18.1      phil 		return IEEE80211_MODE_TURBO_G;
   2309  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_ST(chan))
   2310  1.56.18.1      phil 		return IEEE80211_MODE_STURBO_A;
   2311  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_TURBO(chan))
   2312       1.44    dyoung 		return IEEE80211_MODE_TURBO_A;
   2313  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_HALF(chan))
   2314  1.56.18.1      phil 		return IEEE80211_MODE_HALF;
   2315  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_QUARTER(chan))
   2316  1.56.18.1      phil 		return IEEE80211_MODE_QUARTER;
   2317  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_A(chan))
   2318        1.1    dyoung 		return IEEE80211_MODE_11A;
   2319  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_ANYG(chan))
   2320        1.1    dyoung 		return IEEE80211_MODE_11G;
   2321  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_B(chan))
   2322        1.1    dyoung 		return IEEE80211_MODE_11B;
   2323  1.56.18.1      phil 	else if (IEEE80211_IS_CHAN_FHSS(chan))
   2324  1.56.18.1      phil 		return IEEE80211_MODE_FH;
   2325  1.56.18.1      phil 
   2326  1.56.18.1      phil 	/* NB: should not get here */
   2327  1.56.18.1      phil 	printf("%s: cannot map channel to mode; freq %u flags 0x%x\n",
   2328  1.56.18.1      phil 		__func__, chan->ic_freq, chan->ic_flags);
   2329  1.56.18.1      phil 	return IEEE80211_MODE_11B;
   2330  1.56.18.1      phil }
   2331  1.56.18.1      phil 
   2332  1.56.18.1      phil struct ratemedia {
   2333  1.56.18.1      phil 	u_int	match;	/* rate + mode */
   2334  1.56.18.1      phil 	u_int	media;	/* if_media rate */
   2335  1.56.18.1      phil };
   2336  1.56.18.1      phil 
   2337  1.56.18.1      phil static int
   2338  1.56.18.1      phil findmedia(const struct ratemedia rates[], int n, u_int match)
   2339  1.56.18.1      phil {
   2340  1.56.18.1      phil 	int i;
   2341  1.56.18.1      phil 
   2342  1.56.18.1      phil 	for (i = 0; i < n; i++)
   2343  1.56.18.1      phil 		if (rates[i].match == match)
   2344  1.56.18.1      phil 			return rates[i].media;
   2345  1.56.18.1      phil 	return IFM_AUTO;
   2346        1.1    dyoung }
   2347        1.1    dyoung 
   2348        1.1    dyoung /*
   2349  1.56.18.1      phil  * Convert IEEE80211 rate value to ifmedia subtype.
   2350  1.56.18.1      phil  * Rate is either a legacy rate in units of 0.5Mbps
   2351  1.56.18.1      phil  * or an MCS index.
   2352        1.1    dyoung  */
   2353        1.1    dyoung int
   2354        1.1    dyoung ieee80211_rate2media(struct ieee80211com *ic, int rate, enum ieee80211_phymode mode)
   2355        1.1    dyoung {
   2356  1.56.18.1      phil 	static const struct ratemedia rates[] = {
   2357        1.5    dyoung 		{   2 | IFM_IEEE80211_FH, IFM_IEEE80211_FH1 },
   2358        1.5    dyoung 		{   4 | IFM_IEEE80211_FH, IFM_IEEE80211_FH2 },
   2359        1.5    dyoung 		{   2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
   2360        1.5    dyoung 		{   4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
   2361        1.5    dyoung 		{  11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
   2362        1.5    dyoung 		{  22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
   2363        1.5    dyoung 		{  44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
   2364        1.5    dyoung 		{  12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
   2365        1.5    dyoung 		{  18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
   2366        1.5    dyoung 		{  24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
   2367        1.5    dyoung 		{  36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
   2368        1.5    dyoung 		{  48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
   2369        1.5    dyoung 		{  72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
   2370        1.5    dyoung 		{  96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
   2371        1.5    dyoung 		{ 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
   2372        1.5    dyoung 		{   2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
   2373        1.5    dyoung 		{   4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
   2374        1.5    dyoung 		{  11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
   2375        1.5    dyoung 		{  22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
   2376        1.5    dyoung 		{  12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
   2377        1.5    dyoung 		{  18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
   2378        1.5    dyoung 		{  24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
   2379        1.5    dyoung 		{  36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
   2380        1.5    dyoung 		{  48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
   2381        1.5    dyoung 		{  72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
   2382        1.5    dyoung 		{  96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
   2383        1.5    dyoung 		{ 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
   2384  1.56.18.1      phil 		{   6 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM3 },
   2385  1.56.18.1      phil 		{   9 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM4 },
   2386  1.56.18.1      phil 		{  54 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM27 },
   2387  1.56.18.1      phil 		/* NB: OFDM72 doesn't really exist so we don't handle it */
   2388  1.56.18.1      phil 	};
   2389  1.56.18.1      phil 	static const struct ratemedia htrates[] = {
   2390  1.56.18.1      phil 		{   0, IFM_IEEE80211_MCS },
   2391  1.56.18.1      phil 		{   1, IFM_IEEE80211_MCS },
   2392  1.56.18.1      phil 		{   2, IFM_IEEE80211_MCS },
   2393  1.56.18.1      phil 		{   3, IFM_IEEE80211_MCS },
   2394  1.56.18.1      phil 		{   4, IFM_IEEE80211_MCS },
   2395  1.56.18.1      phil 		{   5, IFM_IEEE80211_MCS },
   2396  1.56.18.1      phil 		{   6, IFM_IEEE80211_MCS },
   2397  1.56.18.1      phil 		{   7, IFM_IEEE80211_MCS },
   2398  1.56.18.1      phil 		{   8, IFM_IEEE80211_MCS },
   2399  1.56.18.1      phil 		{   9, IFM_IEEE80211_MCS },
   2400  1.56.18.1      phil 		{  10, IFM_IEEE80211_MCS },
   2401  1.56.18.1      phil 		{  11, IFM_IEEE80211_MCS },
   2402  1.56.18.1      phil 		{  12, IFM_IEEE80211_MCS },
   2403  1.56.18.1      phil 		{  13, IFM_IEEE80211_MCS },
   2404  1.56.18.1      phil 		{  14, IFM_IEEE80211_MCS },
   2405  1.56.18.1      phil 		{  15, IFM_IEEE80211_MCS },
   2406  1.56.18.1      phil 		{  16, IFM_IEEE80211_MCS },
   2407  1.56.18.1      phil 		{  17, IFM_IEEE80211_MCS },
   2408  1.56.18.1      phil 		{  18, IFM_IEEE80211_MCS },
   2409  1.56.18.1      phil 		{  19, IFM_IEEE80211_MCS },
   2410  1.56.18.1      phil 		{  20, IFM_IEEE80211_MCS },
   2411  1.56.18.1      phil 		{  21, IFM_IEEE80211_MCS },
   2412  1.56.18.1      phil 		{  22, IFM_IEEE80211_MCS },
   2413  1.56.18.1      phil 		{  23, IFM_IEEE80211_MCS },
   2414  1.56.18.1      phil 		{  24, IFM_IEEE80211_MCS },
   2415  1.56.18.1      phil 		{  25, IFM_IEEE80211_MCS },
   2416  1.56.18.1      phil 		{  26, IFM_IEEE80211_MCS },
   2417  1.56.18.1      phil 		{  27, IFM_IEEE80211_MCS },
   2418  1.56.18.1      phil 		{  28, IFM_IEEE80211_MCS },
   2419  1.56.18.1      phil 		{  29, IFM_IEEE80211_MCS },
   2420  1.56.18.1      phil 		{  30, IFM_IEEE80211_MCS },
   2421  1.56.18.1      phil 		{  31, IFM_IEEE80211_MCS },
   2422  1.56.18.1      phil 		{  32, IFM_IEEE80211_MCS },
   2423  1.56.18.1      phil 		{  33, IFM_IEEE80211_MCS },
   2424  1.56.18.1      phil 		{  34, IFM_IEEE80211_MCS },
   2425  1.56.18.1      phil 		{  35, IFM_IEEE80211_MCS },
   2426  1.56.18.1      phil 		{  36, IFM_IEEE80211_MCS },
   2427  1.56.18.1      phil 		{  37, IFM_IEEE80211_MCS },
   2428  1.56.18.1      phil 		{  38, IFM_IEEE80211_MCS },
   2429  1.56.18.1      phil 		{  39, IFM_IEEE80211_MCS },
   2430  1.56.18.1      phil 		{  40, IFM_IEEE80211_MCS },
   2431  1.56.18.1      phil 		{  41, IFM_IEEE80211_MCS },
   2432  1.56.18.1      phil 		{  42, IFM_IEEE80211_MCS },
   2433  1.56.18.1      phil 		{  43, IFM_IEEE80211_MCS },
   2434  1.56.18.1      phil 		{  44, IFM_IEEE80211_MCS },
   2435  1.56.18.1      phil 		{  45, IFM_IEEE80211_MCS },
   2436  1.56.18.1      phil 		{  46, IFM_IEEE80211_MCS },
   2437  1.56.18.1      phil 		{  47, IFM_IEEE80211_MCS },
   2438  1.56.18.1      phil 		{  48, IFM_IEEE80211_MCS },
   2439  1.56.18.1      phil 		{  49, IFM_IEEE80211_MCS },
   2440  1.56.18.1      phil 		{  50, IFM_IEEE80211_MCS },
   2441  1.56.18.1      phil 		{  51, IFM_IEEE80211_MCS },
   2442  1.56.18.1      phil 		{  52, IFM_IEEE80211_MCS },
   2443  1.56.18.1      phil 		{  53, IFM_IEEE80211_MCS },
   2444  1.56.18.1      phil 		{  54, IFM_IEEE80211_MCS },
   2445  1.56.18.1      phil 		{  55, IFM_IEEE80211_MCS },
   2446  1.56.18.1      phil 		{  56, IFM_IEEE80211_MCS },
   2447  1.56.18.1      phil 		{  57, IFM_IEEE80211_MCS },
   2448  1.56.18.1      phil 		{  58, IFM_IEEE80211_MCS },
   2449  1.56.18.1      phil 		{  59, IFM_IEEE80211_MCS },
   2450  1.56.18.1      phil 		{  60, IFM_IEEE80211_MCS },
   2451  1.56.18.1      phil 		{  61, IFM_IEEE80211_MCS },
   2452  1.56.18.1      phil 		{  62, IFM_IEEE80211_MCS },
   2453  1.56.18.1      phil 		{  63, IFM_IEEE80211_MCS },
   2454  1.56.18.1      phil 		{  64, IFM_IEEE80211_MCS },
   2455  1.56.18.1      phil 		{  65, IFM_IEEE80211_MCS },
   2456  1.56.18.1      phil 		{  66, IFM_IEEE80211_MCS },
   2457  1.56.18.1      phil 		{  67, IFM_IEEE80211_MCS },
   2458  1.56.18.1      phil 		{  68, IFM_IEEE80211_MCS },
   2459  1.56.18.1      phil 		{  69, IFM_IEEE80211_MCS },
   2460  1.56.18.1      phil 		{  70, IFM_IEEE80211_MCS },
   2461  1.56.18.1      phil 		{  71, IFM_IEEE80211_MCS },
   2462  1.56.18.1      phil 		{  72, IFM_IEEE80211_MCS },
   2463  1.56.18.1      phil 		{  73, IFM_IEEE80211_MCS },
   2464  1.56.18.1      phil 		{  74, IFM_IEEE80211_MCS },
   2465  1.56.18.1      phil 		{  75, IFM_IEEE80211_MCS },
   2466  1.56.18.1      phil 		{  76, IFM_IEEE80211_MCS },
   2467        1.1    dyoung 	};
   2468  1.56.18.1      phil 	int m;
   2469        1.1    dyoung 
   2470  1.56.18.1      phil 	/*
   2471  1.56.18.1      phil 	 * Check 11n rates first for match as an MCS.
   2472  1.56.18.1      phil 	 */
   2473  1.56.18.1      phil 	if (mode == IEEE80211_MODE_11NA) {
   2474  1.56.18.1      phil 		if (rate & IEEE80211_RATE_MCS) {
   2475  1.56.18.1      phil 			rate &= ~IEEE80211_RATE_MCS;
   2476  1.56.18.1      phil 			m = findmedia(htrates, nitems(htrates), rate);
   2477  1.56.18.1      phil 			if (m != IFM_AUTO)
   2478  1.56.18.1      phil 				return m | IFM_IEEE80211_11NA;
   2479  1.56.18.1      phil 		}
   2480  1.56.18.1      phil 	} else if (mode == IEEE80211_MODE_11NG) {
   2481  1.56.18.1      phil 		/* NB: 12 is ambiguous, it will be treated as an MCS */
   2482  1.56.18.1      phil 		if (rate & IEEE80211_RATE_MCS) {
   2483  1.56.18.1      phil 			rate &= ~IEEE80211_RATE_MCS;
   2484  1.56.18.1      phil 			m = findmedia(htrates, nitems(htrates), rate);
   2485  1.56.18.1      phil 			if (m != IFM_AUTO)
   2486  1.56.18.1      phil 				return m | IFM_IEEE80211_11NG;
   2487  1.56.18.1      phil 		}
   2488  1.56.18.1      phil 	}
   2489  1.56.18.1      phil 	rate &= IEEE80211_RATE_VAL;
   2490        1.1    dyoung 	switch (mode) {
   2491        1.1    dyoung 	case IEEE80211_MODE_11A:
   2492  1.56.18.1      phil 	case IEEE80211_MODE_HALF:		/* XXX good 'nuf */
   2493  1.56.18.1      phil 	case IEEE80211_MODE_QUARTER:
   2494  1.56.18.1      phil 	case IEEE80211_MODE_11NA:
   2495       1.37    dyoung 	case IEEE80211_MODE_TURBO_A:
   2496  1.56.18.1      phil 	case IEEE80211_MODE_STURBO_A:
   2497  1.56.18.1      phil 		return findmedia(rates, nitems(rates),
   2498  1.56.18.1      phil 		    rate | IFM_IEEE80211_11A);
   2499        1.1    dyoung 	case IEEE80211_MODE_11B:
   2500  1.56.18.1      phil 		return findmedia(rates, nitems(rates),
   2501  1.56.18.1      phil 		    rate | IFM_IEEE80211_11B);
   2502        1.5    dyoung 	case IEEE80211_MODE_FH:
   2503  1.56.18.1      phil 		return findmedia(rates, nitems(rates),
   2504  1.56.18.1      phil 		    rate | IFM_IEEE80211_FH);
   2505        1.1    dyoung 	case IEEE80211_MODE_AUTO:
   2506       1.10    dyoung 		/* NB: ic may be NULL for some drivers */
   2507  1.56.18.1      phil 		if (ic != NULL && ic->ic_phytype == IEEE80211_T_FH)
   2508  1.56.18.1      phil 			return findmedia(rates, nitems(rates),
   2509  1.56.18.1      phil 			    rate | IFM_IEEE80211_FH);
   2510       1.10    dyoung 		/* NB: hack, 11g matches both 11b+11a rates */
   2511       1.10    dyoung 		/* fall thru... */
   2512       1.10    dyoung 	case IEEE80211_MODE_11G:
   2513  1.56.18.1      phil 	case IEEE80211_MODE_11NG:
   2514       1.37    dyoung 	case IEEE80211_MODE_TURBO_G:
   2515  1.56.18.1      phil 		return findmedia(rates, nitems(rates), rate | IFM_IEEE80211_11G);
   2516  1.56.18.1      phil 	case IEEE80211_MODE_VHT_2GHZ:
   2517  1.56.18.1      phil 	case IEEE80211_MODE_VHT_5GHZ:
   2518  1.56.18.1      phil 		/* XXX TODO: need to figure out mapping for VHT rates */
   2519  1.56.18.1      phil 		return IFM_AUTO;
   2520        1.1    dyoung 	}
   2521        1.1    dyoung 	return IFM_AUTO;
   2522        1.1    dyoung }
   2523        1.1    dyoung 
   2524        1.1    dyoung int
   2525        1.1    dyoung ieee80211_media2rate(int mword)
   2526        1.1    dyoung {
   2527       1.26   mycroft 	static const int ieeerates[] = {
   2528       1.26   mycroft 		-1,		/* IFM_AUTO */
   2529       1.26   mycroft 		0,		/* IFM_MANUAL */
   2530       1.26   mycroft 		0,		/* IFM_NONE */
   2531       1.26   mycroft 		2,		/* IFM_IEEE80211_FH1 */
   2532       1.26   mycroft 		4,		/* IFM_IEEE80211_FH2 */
   2533  1.56.18.1      phil 		2,		/* IFM_IEEE80211_DS1 */
   2534       1.26   mycroft 		4,		/* IFM_IEEE80211_DS2 */
   2535       1.26   mycroft 		11,		/* IFM_IEEE80211_DS5 */
   2536       1.26   mycroft 		22,		/* IFM_IEEE80211_DS11 */
   2537       1.26   mycroft 		44,		/* IFM_IEEE80211_DS22 */
   2538       1.26   mycroft 		12,		/* IFM_IEEE80211_OFDM6 */
   2539       1.26   mycroft 		18,		/* IFM_IEEE80211_OFDM9 */
   2540       1.26   mycroft 		24,		/* IFM_IEEE80211_OFDM12 */
   2541       1.26   mycroft 		36,		/* IFM_IEEE80211_OFDM18 */
   2542       1.26   mycroft 		48,		/* IFM_IEEE80211_OFDM24 */
   2543       1.26   mycroft 		72,		/* IFM_IEEE80211_OFDM36 */
   2544       1.26   mycroft 		96,		/* IFM_IEEE80211_OFDM48 */
   2545       1.26   mycroft 		108,		/* IFM_IEEE80211_OFDM54 */
   2546       1.26   mycroft 		144,		/* IFM_IEEE80211_OFDM72 */
   2547  1.56.18.1      phil 		0,		/* IFM_IEEE80211_DS354k */
   2548  1.56.18.1      phil 		0,		/* IFM_IEEE80211_DS512k */
   2549  1.56.18.1      phil 		6,		/* IFM_IEEE80211_OFDM3 */
   2550  1.56.18.1      phil 		9,		/* IFM_IEEE80211_OFDM4 */
   2551  1.56.18.1      phil 		54,		/* IFM_IEEE80211_OFDM27 */
   2552  1.56.18.1      phil 		-1,		/* IFM_IEEE80211_MCS */
   2553  1.56.18.1      phil 		-1,		/* IFM_IEEE80211_VHT */
   2554        1.1    dyoung 	};
   2555  1.56.18.1      phil 	return IFM_SUBTYPE(mword) < nitems(ieeerates) ?
   2556       1.26   mycroft 		ieeerates[IFM_SUBTYPE(mword)] : 0;
   2557  1.56.18.1      phil }
   2558  1.56.18.1      phil 
   2559  1.56.18.1      phil /*
   2560  1.56.18.1      phil  * The following hash function is adapted from "Hash Functions" by Bob Jenkins
   2561  1.56.18.1      phil  * ("Algorithm Alley", Dr. Dobbs Journal, September 1997).
   2562  1.56.18.1      phil  */
   2563  1.56.18.1      phil #define	mix(a, b, c)							\
   2564  1.56.18.1      phil do {									\
   2565  1.56.18.1      phil 	a -= b; a -= c; a ^= (c >> 13);					\
   2566  1.56.18.1      phil 	b -= c; b -= a; b ^= (a << 8);					\
   2567  1.56.18.1      phil 	c -= a; c -= b; c ^= (b >> 13);					\
   2568  1.56.18.1      phil 	a -= b; a -= c; a ^= (c >> 12);					\
   2569  1.56.18.1      phil 	b -= c; b -= a; b ^= (a << 16);					\
   2570  1.56.18.1      phil 	c -= a; c -= b; c ^= (b >> 5);					\
   2571  1.56.18.1      phil 	a -= b; a -= c; a ^= (c >> 3);					\
   2572  1.56.18.1      phil 	b -= c; b -= a; b ^= (a << 10);					\
   2573  1.56.18.1      phil 	c -= a; c -= b; c ^= (b >> 15);					\
   2574  1.56.18.1      phil } while (/*CONSTCOND*/0)
   2575  1.56.18.1      phil 
   2576  1.56.18.1      phil uint32_t
   2577  1.56.18.1      phil ieee80211_mac_hash(const struct ieee80211com *ic,
   2578  1.56.18.1      phil 	const uint8_t addr[IEEE80211_ADDR_LEN])
   2579  1.56.18.1      phil {
   2580  1.56.18.1      phil 	uint32_t a = 0x9e3779b9, b = 0x9e3779b9, c = ic->ic_hash_key;
   2581  1.56.18.1      phil 
   2582  1.56.18.1      phil 	b += addr[5] << 8;
   2583  1.56.18.1      phil 	b += addr[4];
   2584  1.56.18.1      phil 	a += addr[3] << 24;
   2585  1.56.18.1      phil 	a += addr[2] << 16;
   2586  1.56.18.1      phil 	a += addr[1] << 8;
   2587  1.56.18.1      phil 	a += addr[0];
   2588  1.56.18.1      phil 
   2589  1.56.18.1      phil 	mix(a, b, c);
   2590  1.56.18.1      phil 
   2591  1.56.18.1      phil 	return c;
   2592  1.56.18.1      phil }
   2593  1.56.18.1      phil #undef mix
   2594  1.56.18.1      phil 
   2595  1.56.18.1      phil char
   2596  1.56.18.1      phil ieee80211_channel_type_char(const struct ieee80211_channel *c)
   2597  1.56.18.1      phil {
   2598  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_ST(c))
   2599  1.56.18.1      phil 		return 'S';
   2600  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_108A(c))
   2601  1.56.18.1      phil 		return 'T';
   2602  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_108G(c))
   2603  1.56.18.1      phil 		return 'G';
   2604  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_VHT(c))
   2605  1.56.18.1      phil 		return 'v';
   2606  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_HT(c))
   2607  1.56.18.1      phil 		return 'n';
   2608  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_A(c))
   2609  1.56.18.1      phil 		return 'a';
   2610  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_ANYG(c))
   2611  1.56.18.1      phil 		return 'g';
   2612  1.56.18.1      phil 	if (IEEE80211_IS_CHAN_B(c))
   2613  1.56.18.1      phil 		return 'b';
   2614  1.56.18.1      phil 	return 'f';
   2615        1.1    dyoung }
   2616