Home | History | Annotate | Line # | Download | only in net80211
ieee80211_proto.c revision 1.34.14.8
      1  1.34.14.8  christos /*	$NetBSD: ieee80211_proto.c,v 1.34.14.8 2019/06/10 22:09:46 christos Exp $ */
      2  1.34.14.2      phil 
      3        1.1    dyoung /*-
      4  1.34.14.1      phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  1.34.14.1      phil  *
      6        1.1    dyoung  * Copyright (c) 2001 Atsushi Onoe
      7  1.34.14.1      phil  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
      8  1.34.14.1      phil  * Copyright (c) 2012 IEEE
      9        1.1    dyoung  * All rights reserved.
     10        1.1    dyoung  *
     11        1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     12        1.1    dyoung  * modification, are permitted provided that the following conditions
     13        1.1    dyoung  * are met:
     14        1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     15        1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     16        1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     17        1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     18        1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     19        1.1    dyoung  *
     20        1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     21        1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22        1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     23        1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     24        1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     25        1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26        1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27        1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28        1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     29        1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30        1.1    dyoung  */
     31        1.1    dyoung 
     32        1.1    dyoung #include <sys/cdefs.h>
     33  1.34.14.8  christos #ifdef __NetBSD__
     34  1.34.14.8  christos __KERNEL_RCSID(0, "$NetBSD: ieee80211_proto.c,v 1.34.14.8 2019/06/10 22:09:46 christos Exp $");
     35  1.34.14.2      phil #endif
     36        1.1    dyoung 
     37        1.1    dyoung /*
     38        1.1    dyoung  * IEEE 802.11 protocol support.
     39        1.1    dyoung  */
     40        1.1    dyoung 
     41  1.34.14.8  christos #ifdef _KERNEL_OPT
     42        1.1    dyoung #include "opt_inet.h"
     43  1.34.14.1      phil #include "opt_wlan.h"
     44  1.34.14.8  christos #endif
     45        1.1    dyoung 
     46        1.1    dyoung #include <sys/param.h>
     47  1.34.14.1      phil #include <sys/systm.h>
     48        1.1    dyoung #include <sys/kernel.h>
     49  1.34.14.1      phil #include <sys/malloc.h>
     50  1.34.14.2      phil #ifdef __NetBSD__
     51  1.34.14.2      phil #include <sys/mbuf.h>
     52  1.34.14.4      phil #include <sys/once.h>
     53  1.34.14.2      phil #endif
     54  1.34.14.1      phil 
     55        1.1    dyoung #include <sys/socket.h>
     56        1.1    dyoung #include <sys/sockio.h>
     57        1.1    dyoung 
     58        1.1    dyoung #include <net/if.h>
     59  1.34.14.2      phil #if __FreeBSD__
     60  1.34.14.1      phil #include <net/if_var.h>
     61  1.34.14.2      phil #endif
     62        1.1    dyoung #include <net/if_media.h>
     63  1.34.14.2      phil #if __FreeBSD__
     64  1.34.14.1      phil #include <net/ethernet.h>		/* XXX for ether_sprintf */
     65  1.34.14.2      phil #endif
     66  1.34.14.2      phil #ifdef __NetBSD__
     67  1.34.14.2      phil #include <net/if_ether.h>
     68  1.34.14.2      phil #include <net/route.h>
     69  1.34.14.2      phil #endif
     70        1.1    dyoung 
     71        1.1    dyoung #include <net80211/ieee80211_var.h>
     72  1.34.14.1      phil #include <net80211/ieee80211_adhoc.h>
     73  1.34.14.1      phil #include <net80211/ieee80211_sta.h>
     74  1.34.14.1      phil #include <net80211/ieee80211_hostap.h>
     75  1.34.14.1      phil #include <net80211/ieee80211_wds.h>
     76  1.34.14.1      phil #ifdef IEEE80211_SUPPORT_MESH
     77  1.34.14.1      phil #include <net80211/ieee80211_mesh.h>
     78        1.4    dyoung #endif
     79  1.34.14.1      phil #include <net80211/ieee80211_monitor.h>
     80  1.34.14.1      phil #include <net80211/ieee80211_input.h>
     81        1.1    dyoung 
     82  1.34.14.2      phil #ifdef __NetBSD__
     83  1.34.14.2      phil #undef  KASSERT
     84  1.34.14.2      phil #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     85  1.34.14.2      phil #endif
     86  1.34.14.2      phil 
     87  1.34.14.3      phil #if __NetBSD__
     88  1.34.14.3      phil extern const struct ieee80211_authenticator auth_xauth;
     89  1.34.14.3      phil #endif
     90  1.34.14.3      phil 
     91       1.19    dyoung /* XXX tunables */
     92       1.19    dyoung #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
     93       1.19    dyoung #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
     94        1.9    dyoung 
     95  1.34.14.1      phil const char *mgt_subtype_name[] = {
     96        1.1    dyoung 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
     97  1.34.14.1      phil 	"probe_req",	"probe_resp",	"timing_adv",	"reserved#7",
     98        1.1    dyoung 	"beacon",	"atim",		"disassoc",	"auth",
     99  1.34.14.1      phil 	"deauth",	"action",	"action_noack",	"reserved#15"
    100        1.1    dyoung };
    101  1.34.14.1      phil const char *ctl_subtype_name[] = {
    102       1.19    dyoung 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
    103  1.34.14.1      phil 	"reserved#4",	"reserved#5",	"reserved#6",	"control_wrap",
    104  1.34.14.1      phil 	"bar",		"ba",		"ps_poll",	"rts",
    105       1.19    dyoung 	"cts",		"ack",		"cf_end",	"cf_end_ack"
    106       1.19    dyoung };
    107  1.34.14.1      phil const char *ieee80211_opmode_name[IEEE80211_OPMODE_MAX] = {
    108  1.34.14.1      phil 	"IBSS",		/* IEEE80211_M_IBSS */
    109  1.34.14.1      phil 	"STA",		/* IEEE80211_M_STA */
    110  1.34.14.1      phil 	"WDS",		/* IEEE80211_M_WDS */
    111  1.34.14.1      phil 	"AHDEMO",	/* IEEE80211_M_AHDEMO */
    112  1.34.14.1      phil 	"HOSTAP",	/* IEEE80211_M_HOSTAP */
    113  1.34.14.1      phil 	"MONITOR",	/* IEEE80211_M_MONITOR */
    114  1.34.14.1      phil 	"MBSS"		/* IEEE80211_M_MBSS */
    115  1.34.14.1      phil };
    116        1.1    dyoung const char *ieee80211_state_name[IEEE80211_S_MAX] = {
    117        1.1    dyoung 	"INIT",		/* IEEE80211_S_INIT */
    118        1.1    dyoung 	"SCAN",		/* IEEE80211_S_SCAN */
    119        1.1    dyoung 	"AUTH",		/* IEEE80211_S_AUTH */
    120        1.1    dyoung 	"ASSOC",	/* IEEE80211_S_ASSOC */
    121  1.34.14.1      phil 	"CAC",		/* IEEE80211_S_CAC */
    122  1.34.14.1      phil 	"RUN",		/* IEEE80211_S_RUN */
    123  1.34.14.1      phil 	"CSA",		/* IEEE80211_S_CSA */
    124  1.34.14.1      phil 	"SLEEP",	/* IEEE80211_S_SLEEP */
    125        1.1    dyoung };
    126       1.19    dyoung const char *ieee80211_wme_acnames[] = {
    127       1.19    dyoung 	"WME_AC_BE",
    128       1.19    dyoung 	"WME_AC_BK",
    129       1.19    dyoung 	"WME_AC_VI",
    130       1.19    dyoung 	"WME_AC_VO",
    131       1.19    dyoung 	"WME_UPSD",
    132       1.19    dyoung };
    133        1.1    dyoung 
    134  1.34.14.1      phil 
    135  1.34.14.1      phil /*
    136  1.34.14.1      phil  * Reason code descriptions were (mostly) obtained from
    137  1.34.14.1      phil  * IEEE Std 802.11-2012, pp. 442-445 Table 8-36.
    138  1.34.14.1      phil  */
    139  1.34.14.1      phil const char *
    140  1.34.14.1      phil ieee80211_reason_to_string(uint16_t reason)
    141  1.34.14.1      phil {
    142  1.34.14.1      phil 	switch (reason) {
    143  1.34.14.1      phil 	case IEEE80211_REASON_UNSPECIFIED:
    144  1.34.14.1      phil 		return ("unspecified");
    145  1.34.14.1      phil 	case IEEE80211_REASON_AUTH_EXPIRE:
    146  1.34.14.1      phil 		return ("previous authentication is expired");
    147  1.34.14.1      phil 	case IEEE80211_REASON_AUTH_LEAVE:
    148  1.34.14.1      phil 		return ("sending STA is leaving/has left IBSS or ESS");
    149  1.34.14.1      phil 	case IEEE80211_REASON_ASSOC_EXPIRE:
    150  1.34.14.1      phil 		return ("disassociated due to inactivity");
    151  1.34.14.1      phil 	case IEEE80211_REASON_ASSOC_TOOMANY:
    152  1.34.14.1      phil 		return ("too many associated STAs");
    153  1.34.14.1      phil 	case IEEE80211_REASON_NOT_AUTHED:
    154  1.34.14.1      phil 		return ("class 2 frame received from nonauthenticated STA");
    155  1.34.14.1      phil 	case IEEE80211_REASON_NOT_ASSOCED:
    156  1.34.14.1      phil 		return ("class 3 frame received from nonassociated STA");
    157  1.34.14.1      phil 	case IEEE80211_REASON_ASSOC_LEAVE:
    158  1.34.14.1      phil 		return ("sending STA is leaving/has left BSS");
    159  1.34.14.1      phil 	case IEEE80211_REASON_ASSOC_NOT_AUTHED:
    160  1.34.14.1      phil 		return ("STA requesting (re)association is not authenticated");
    161  1.34.14.1      phil 	case IEEE80211_REASON_DISASSOC_PWRCAP_BAD:
    162  1.34.14.1      phil 		return ("information in the Power Capability element is "
    163  1.34.14.1      phil 			"unacceptable");
    164  1.34.14.1      phil 	case IEEE80211_REASON_DISASSOC_SUPCHAN_BAD:
    165  1.34.14.1      phil 		return ("information in the Supported Channels element is "
    166  1.34.14.1      phil 			"unacceptable");
    167  1.34.14.1      phil 	case IEEE80211_REASON_IE_INVALID:
    168  1.34.14.1      phil 		return ("invalid element");
    169  1.34.14.1      phil 	case IEEE80211_REASON_MIC_FAILURE:
    170  1.34.14.1      phil 		return ("MIC failure");
    171  1.34.14.1      phil 	case IEEE80211_REASON_4WAY_HANDSHAKE_TIMEOUT:
    172  1.34.14.1      phil 		return ("4-Way handshake timeout");
    173  1.34.14.1      phil 	case IEEE80211_REASON_GROUP_KEY_UPDATE_TIMEOUT:
    174  1.34.14.1      phil 		return ("group key update timeout");
    175  1.34.14.1      phil 	case IEEE80211_REASON_IE_IN_4WAY_DIFFERS:
    176  1.34.14.1      phil 		return ("element in 4-Way handshake different from "
    177  1.34.14.1      phil 			"(re)association request/probe response/beacon frame");
    178  1.34.14.1      phil 	case IEEE80211_REASON_GROUP_CIPHER_INVALID:
    179  1.34.14.1      phil 		return ("invalid group cipher");
    180  1.34.14.1      phil 	case IEEE80211_REASON_PAIRWISE_CIPHER_INVALID:
    181  1.34.14.1      phil 		return ("invalid pairwise cipher");
    182  1.34.14.1      phil 	case IEEE80211_REASON_AKMP_INVALID:
    183  1.34.14.1      phil 		return ("invalid AKMP");
    184  1.34.14.1      phil 	case IEEE80211_REASON_UNSUPP_RSN_IE_VERSION:
    185  1.34.14.1      phil 		return ("unsupported version in RSN IE");
    186  1.34.14.1      phil 	case IEEE80211_REASON_INVALID_RSN_IE_CAP:
    187  1.34.14.1      phil 		return ("invalid capabilities in RSN IE");
    188  1.34.14.1      phil 	case IEEE80211_REASON_802_1X_AUTH_FAILED:
    189  1.34.14.1      phil 		return ("IEEE 802.1X authentication failed");
    190  1.34.14.1      phil 	case IEEE80211_REASON_CIPHER_SUITE_REJECTED:
    191  1.34.14.1      phil 		return ("cipher suite rejected because of the security "
    192  1.34.14.1      phil 			"policy");
    193  1.34.14.1      phil 	case IEEE80211_REASON_UNSPECIFIED_QOS:
    194  1.34.14.1      phil 		return ("unspecified (QoS-related)");
    195  1.34.14.1      phil 	case IEEE80211_REASON_INSUFFICIENT_BW:
    196  1.34.14.1      phil 		return ("QoS AP lacks sufficient bandwidth for this QoS STA");
    197  1.34.14.1      phil 	case IEEE80211_REASON_TOOMANY_FRAMES:
    198  1.34.14.1      phil 		return ("too many frames need to be acknowledged");
    199  1.34.14.1      phil 	case IEEE80211_REASON_OUTSIDE_TXOP:
    200  1.34.14.1      phil 		return ("STA is transmitting outside the limits of its TXOPs");
    201  1.34.14.1      phil 	case IEEE80211_REASON_LEAVING_QBSS:
    202  1.34.14.1      phil 		return ("requested from peer STA (the STA is "
    203  1.34.14.1      phil 			"resetting/leaving the BSS)");
    204  1.34.14.1      phil 	case IEEE80211_REASON_BAD_MECHANISM:
    205  1.34.14.1      phil 		return ("requested from peer STA (it does not want to use "
    206  1.34.14.1      phil 			"the mechanism)");
    207  1.34.14.1      phil 	case IEEE80211_REASON_SETUP_NEEDED:
    208  1.34.14.1      phil 		return ("requested from peer STA (setup is required for the "
    209  1.34.14.1      phil 			"used mechanism)");
    210  1.34.14.1      phil 	case IEEE80211_REASON_TIMEOUT:
    211  1.34.14.1      phil 		return ("requested from peer STA (timeout)");
    212  1.34.14.1      phil 	case IEEE80211_REASON_PEER_LINK_CANCELED:
    213  1.34.14.1      phil 		return ("SME cancels the mesh peering instance (not related "
    214  1.34.14.1      phil 			"to the maximum number of peer mesh STAs)");
    215  1.34.14.1      phil 	case IEEE80211_REASON_MESH_MAX_PEERS:
    216  1.34.14.1      phil 		return ("maximum number of peer mesh STAs was reached");
    217  1.34.14.1      phil 	case IEEE80211_REASON_MESH_CPVIOLATION:
    218  1.34.14.1      phil 		return ("the received information violates the Mesh "
    219  1.34.14.1      phil 			"Configuration policy configured in the mesh STA "
    220  1.34.14.1      phil 			"profile");
    221  1.34.14.1      phil 	case IEEE80211_REASON_MESH_CLOSE_RCVD:
    222  1.34.14.1      phil 		return ("the mesh STA has received a Mesh Peering Close "
    223  1.34.14.1      phil 			"message requesting to close the mesh peering");
    224  1.34.14.1      phil 	case IEEE80211_REASON_MESH_MAX_RETRIES:
    225  1.34.14.1      phil 		return ("the mesh STA has resent dot11MeshMaxRetries Mesh "
    226  1.34.14.1      phil 			"Peering Open messages, without receiving a Mesh "
    227  1.34.14.1      phil 			"Peering Confirm message");
    228  1.34.14.1      phil 	case IEEE80211_REASON_MESH_CONFIRM_TIMEOUT:
    229  1.34.14.1      phil 		return ("the confirmTimer for the mesh peering instance times "
    230  1.34.14.1      phil 			"out");
    231  1.34.14.1      phil 	case IEEE80211_REASON_MESH_INVALID_GTK:
    232  1.34.14.1      phil 		return ("the mesh STA fails to unwrap the GTK or the values "
    233  1.34.14.1      phil 			"in the wrapped contents do not match");
    234  1.34.14.1      phil 	case IEEE80211_REASON_MESH_INCONS_PARAMS:
    235  1.34.14.1      phil 		return ("the mesh STA receives inconsistent information about "
    236  1.34.14.1      phil 			"the mesh parameters between Mesh Peering Management "
    237  1.34.14.1      phil 			"frames");
    238  1.34.14.1      phil 	case IEEE80211_REASON_MESH_INVALID_SECURITY:
    239  1.34.14.1      phil 		return ("the mesh STA fails the authenticated mesh peering "
    240  1.34.14.1      phil 			"exchange because due to failure in selecting "
    241  1.34.14.1      phil 			"pairwise/group ciphersuite");
    242  1.34.14.1      phil 	case IEEE80211_REASON_MESH_PERR_NO_PROXY:
    243  1.34.14.1      phil 		return ("the mesh STA does not have proxy information for "
    244  1.34.14.1      phil 			"this external destination");
    245  1.34.14.1      phil 	case IEEE80211_REASON_MESH_PERR_NO_FI:
    246  1.34.14.1      phil 		return ("the mesh STA does not have forwarding information "
    247  1.34.14.1      phil 			"for this destination");
    248  1.34.14.1      phil 	case IEEE80211_REASON_MESH_PERR_DEST_UNREACH:
    249  1.34.14.1      phil 		return ("the mesh STA determines that the link to the next "
    250  1.34.14.1      phil 			"hop of an active path in its forwarding information "
    251  1.34.14.1      phil 			"is no longer usable");
    252  1.34.14.1      phil 	case IEEE80211_REASON_MESH_MAC_ALRDY_EXISTS_MBSS:
    253  1.34.14.1      phil 		return ("the MAC address of the STA already exists in the "
    254  1.34.14.1      phil 			"mesh BSS");
    255  1.34.14.1      phil 	case IEEE80211_REASON_MESH_CHAN_SWITCH_REG:
    256  1.34.14.1      phil 		return ("the mesh STA performs channel switch to meet "
    257  1.34.14.1      phil 			"regulatory requirements");
    258  1.34.14.1      phil 	case IEEE80211_REASON_MESH_CHAN_SWITCH_UNSPEC:
    259  1.34.14.1      phil 		return ("the mesh STA performs channel switch with "
    260  1.34.14.1      phil 			"unspecified reason");
    261  1.34.14.1      phil 	default:
    262  1.34.14.1      phil 		return ("reserved/unknown");
    263  1.34.14.1      phil 	}
    264  1.34.14.1      phil }
    265  1.34.14.1      phil 
    266  1.34.14.1      phil static void beacon_miss(void *, int);
    267  1.34.14.1      phil static void beacon_swmiss(void *, int);
    268  1.34.14.1      phil static void parent_updown(void *, int);
    269  1.34.14.1      phil static void update_mcast(void *, int);
    270  1.34.14.1      phil static void update_promisc(void *, int);
    271  1.34.14.1      phil static void update_channel(void *, int);
    272  1.34.14.1      phil static void update_chw(void *, int);
    273  1.34.14.1      phil static void vap_update_wme(void *, int);
    274  1.34.14.1      phil static void restart_vaps(void *, int);
    275  1.34.14.1      phil static void ieee80211_newstate_cb(void *, int);
    276  1.34.14.1      phil 
    277  1.34.14.1      phil static int
    278  1.34.14.1      phil null_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
    279  1.34.14.1      phil 	const struct ieee80211_bpf_params *params)
    280  1.34.14.1      phil {
    281  1.34.14.1      phil 
    282  1.34.14.1      phil 	ic_printf(ni->ni_ic, "missing ic_raw_xmit callback, drop frame\n");
    283  1.34.14.1      phil 	m_freem(m);
    284  1.34.14.1      phil 	return ENETDOWN;
    285  1.34.14.1      phil }
    286        1.1    dyoung 
    287        1.1    dyoung void
    288       1.19    dyoung ieee80211_proto_attach(struct ieee80211com *ic)
    289        1.1    dyoung {
    290  1.34.14.1      phil 	uint8_t hdrlen;
    291        1.1    dyoung 
    292  1.34.14.5      phil 	printf ("i33380211_proto_attach called\n");  /* NNN debug */
    293  1.34.14.5      phil 
    294  1.34.14.1      phil 	/* override the 802.3 setting */
    295  1.34.14.1      phil 	hdrlen = ic->ic_headroom
    296  1.34.14.1      phil 		+ sizeof(struct ieee80211_qosframe_addr4)
    297  1.34.14.1      phil 		+ IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
    298  1.34.14.1      phil 		+ IEEE80211_WEP_EXTIVLEN;
    299  1.34.14.1      phil 	/* XXX no way to recalculate on ifdetach */
    300  1.34.14.1      phil 	if (ALIGN(hdrlen) > max_linkhdr) {
    301  1.34.14.1      phil 		/* XXX sanity check... */
    302  1.34.14.1      phil 		max_linkhdr = ALIGN(hdrlen);
    303  1.34.14.1      phil 		max_hdr = max_linkhdr + max_protohdr;
    304  1.34.14.1      phil 		max_datalen = MHLEN - max_hdr;
    305  1.34.14.1      phil 	}
    306        1.8    dyoung 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
    307  1.34.14.1      phil 
    308  1.34.14.1      phil 	TASK_INIT(&ic->ic_parent_task, 0, parent_updown, ic);
    309  1.34.14.5      phil 	printf ("parent task: t_work.wk_dummy 0x%lx, t_func 0x%lx t_arg 0x%lx\n",
    310  1.34.14.5      phil 		(long)(ic->ic_parent_task.t_work.wk_dummy),
    311  1.34.14.5      phil 		(long)(ic->ic_parent_task.t_func),
    312  1.34.14.5      phil 		(long)(ic->ic_parent_task.t_arg));
    313  1.34.14.1      phil 	TASK_INIT(&ic->ic_mcast_task, 0, update_mcast, ic);
    314  1.34.14.1      phil 	TASK_INIT(&ic->ic_promisc_task, 0, update_promisc, ic);
    315  1.34.14.1      phil 	TASK_INIT(&ic->ic_chan_task, 0, update_channel, ic);
    316  1.34.14.1      phil 	TASK_INIT(&ic->ic_bmiss_task, 0, beacon_miss, ic);
    317  1.34.14.1      phil 	TASK_INIT(&ic->ic_chw_task, 0, update_chw, ic);
    318  1.34.14.1      phil 	TASK_INIT(&ic->ic_restart_task, 0, restart_vaps, ic);
    319        1.1    dyoung 
    320       1.19    dyoung 	ic->ic_wme.wme_hipri_switch_hysteresis =
    321       1.19    dyoung 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
    322        1.1    dyoung 
    323        1.1    dyoung 	/* initialize management frame handlers */
    324        1.1    dyoung 	ic->ic_send_mgmt = ieee80211_send_mgmt;
    325  1.34.14.1      phil 	ic->ic_raw_xmit = null_raw_xmit;
    326  1.34.14.1      phil 
    327  1.34.14.1      phil 	ieee80211_adhoc_attach(ic);
    328  1.34.14.1      phil 	ieee80211_sta_attach(ic);
    329  1.34.14.1      phil 	ieee80211_wds_attach(ic);
    330  1.34.14.1      phil 	ieee80211_hostap_attach(ic);
    331  1.34.14.1      phil #ifdef IEEE80211_SUPPORT_MESH
    332  1.34.14.1      phil 	ieee80211_mesh_attach(ic);
    333  1.34.14.1      phil #endif
    334  1.34.14.1      phil 	ieee80211_monitor_attach(ic);
    335        1.1    dyoung }
    336        1.1    dyoung 
    337        1.1    dyoung void
    338       1.19    dyoung ieee80211_proto_detach(struct ieee80211com *ic)
    339        1.1    dyoung {
    340  1.34.14.1      phil 	ieee80211_monitor_detach(ic);
    341  1.34.14.1      phil #ifdef IEEE80211_SUPPORT_MESH
    342  1.34.14.1      phil 	ieee80211_mesh_detach(ic);
    343  1.34.14.1      phil #endif
    344  1.34.14.1      phil 	ieee80211_hostap_detach(ic);
    345  1.34.14.1      phil 	ieee80211_wds_detach(ic);
    346  1.34.14.1      phil 	ieee80211_adhoc_detach(ic);
    347  1.34.14.1      phil 	ieee80211_sta_detach(ic);
    348  1.34.14.1      phil }
    349  1.34.14.1      phil 
    350  1.34.14.1      phil static void
    351  1.34.14.1      phil null_update_beacon(struct ieee80211vap *vap, int item)
    352  1.34.14.1      phil {
    353  1.34.14.1      phil }
    354  1.34.14.1      phil 
    355  1.34.14.1      phil void
    356  1.34.14.1      phil ieee80211_proto_vattach(struct ieee80211vap *vap)
    357  1.34.14.1      phil {
    358  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    359  1.34.14.1      phil 	struct ifnet *ifp = vap->iv_ifp;
    360  1.34.14.1      phil 	int i;
    361  1.34.14.1      phil 
    362  1.34.14.1      phil 	/* override the 802.3 setting */
    363  1.34.14.1      phil 	ifp->if_hdrlen = ic->ic_headroom
    364  1.34.14.1      phil                 + sizeof(struct ieee80211_qosframe_addr4)
    365  1.34.14.1      phil                 + IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN
    366  1.34.14.1      phil                 + IEEE80211_WEP_EXTIVLEN;
    367  1.34.14.1      phil 
    368  1.34.14.1      phil 	vap->iv_rtsthreshold = IEEE80211_RTS_DEFAULT;
    369  1.34.14.1      phil 	vap->iv_fragthreshold = IEEE80211_FRAG_DEFAULT;
    370  1.34.14.1      phil 	vap->iv_bmiss_max = IEEE80211_BMISS_MAX;
    371  1.34.14.2      phil #if __FreeBSD__
    372  1.34.14.1      phil 	callout_init_mtx(&vap->iv_swbmiss, IEEE80211_LOCK_OBJ(ic), 0);
    373  1.34.14.1      phil 	callout_init(&vap->iv_mgtsend, 1);
    374  1.34.14.3      phil #elif __NetBSD__
    375  1.34.14.3      phil 	/* NNN need to do something with iv_swbmiss ... */
    376  1.34.14.6      phil 	callout_init(&vap->iv_swbmiss, CALLOUT_MPSAFE);
    377  1.34.14.3      phil 	callout_init(&vap->iv_mgtsend, CALLOUT_MPSAFE);
    378  1.34.14.3      phil #endif
    379  1.34.14.1      phil 	TASK_INIT(&vap->iv_nstate_task, 0, ieee80211_newstate_cb, vap);
    380  1.34.14.1      phil 	TASK_INIT(&vap->iv_swbmiss_task, 0, beacon_swmiss, vap);
    381  1.34.14.1      phil 	TASK_INIT(&vap->iv_wme_task, 0, vap_update_wme, vap);
    382  1.34.14.5      phil 
    383  1.34.14.1      phil 	/*
    384  1.34.14.1      phil 	 * Install default tx rate handling: no fixed rate, lowest
    385  1.34.14.1      phil 	 * supported rate for mgmt and multicast frames.  Default
    386  1.34.14.1      phil 	 * max retry count.  These settings can be changed by the
    387  1.34.14.1      phil 	 * driver and/or user applications.
    388  1.34.14.1      phil 	 */
    389  1.34.14.1      phil 	for (i = IEEE80211_MODE_11A; i < IEEE80211_MODE_MAX; i++) {
    390  1.34.14.1      phil 		const struct ieee80211_rateset *rs = &ic->ic_sup_rates[i];
    391  1.34.14.1      phil 
    392  1.34.14.1      phil 		vap->iv_txparms[i].ucastrate = IEEE80211_FIXED_RATE_NONE;
    393  1.34.14.1      phil 
    394  1.34.14.1      phil 		/*
    395  1.34.14.1      phil 		 * Setting the management rate to MCS 0 assumes that the
    396  1.34.14.1      phil 		 * BSS Basic rate set is empty and the BSS Basic MCS set
    397  1.34.14.1      phil 		 * is not.
    398  1.34.14.1      phil 		 *
    399  1.34.14.1      phil 		 * Since we're not checking this, default to the lowest
    400  1.34.14.1      phil 		 * defined rate for this mode.
    401  1.34.14.1      phil 		 *
    402  1.34.14.1      phil 		 * At least one 11n AP (DLINK DIR-825) is reported to drop
    403  1.34.14.1      phil 		 * some MCS management traffic (eg BA response frames.)
    404  1.34.14.1      phil 		 *
    405  1.34.14.1      phil 		 * See also: 9.6.0 of the 802.11n-2009 specification.
    406  1.34.14.1      phil 		 */
    407  1.34.14.1      phil #ifdef	NOTYET
    408  1.34.14.1      phil 		if (i == IEEE80211_MODE_11NA || i == IEEE80211_MODE_11NG) {
    409  1.34.14.1      phil 			vap->iv_txparms[i].mgmtrate = 0 | IEEE80211_RATE_MCS;
    410  1.34.14.1      phil 			vap->iv_txparms[i].mcastrate = 0 | IEEE80211_RATE_MCS;
    411  1.34.14.1      phil 		} else {
    412  1.34.14.1      phil 			vap->iv_txparms[i].mgmtrate =
    413  1.34.14.1      phil 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
    414  1.34.14.1      phil 			vap->iv_txparms[i].mcastrate =
    415  1.34.14.1      phil 			    rs->rs_rates[0] & IEEE80211_RATE_VAL;
    416  1.34.14.1      phil 		}
    417  1.34.14.1      phil #endif
    418  1.34.14.1      phil 		vap->iv_txparms[i].mgmtrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
    419  1.34.14.1      phil 		vap->iv_txparms[i].mcastrate = rs->rs_rates[0] & IEEE80211_RATE_VAL;
    420  1.34.14.1      phil 		vap->iv_txparms[i].maxretry = IEEE80211_TXMAX_DEFAULT;
    421  1.34.14.1      phil 	}
    422  1.34.14.1      phil 	vap->iv_roaming = IEEE80211_ROAMING_AUTO;
    423  1.34.14.1      phil 
    424  1.34.14.1      phil 	vap->iv_update_beacon = null_update_beacon;
    425  1.34.14.1      phil 	vap->iv_deliver_data = ieee80211_deliver_data;
    426  1.34.14.1      phil 
    427  1.34.14.1      phil 	/* attach support for operating mode */
    428  1.34.14.1      phil 	ic->ic_vattach[vap->iv_opmode](vap);
    429  1.34.14.1      phil }
    430        1.1    dyoung 
    431  1.34.14.1      phil void
    432  1.34.14.1      phil ieee80211_proto_vdetach(struct ieee80211vap *vap)
    433  1.34.14.1      phil {
    434  1.34.14.1      phil #define	FREEAPPIE(ie) do { \
    435  1.34.14.1      phil 	if (ie != NULL) \
    436  1.34.14.1      phil 		IEEE80211_FREE(ie, M_80211_NODE_IE); \
    437  1.34.14.1      phil } while (0)
    438  1.34.14.1      phil 	/*
    439  1.34.14.1      phil 	 * Detach operating mode module.
    440  1.34.14.1      phil 	 */
    441  1.34.14.1      phil 	if (vap->iv_opdetach != NULL)
    442  1.34.14.1      phil 		vap->iv_opdetach(vap);
    443       1.19    dyoung 	/*
    444       1.19    dyoung 	 * This should not be needed as we detach when reseting
    445       1.19    dyoung 	 * the state but be conservative here since the
    446       1.19    dyoung 	 * authenticator may do things like spawn kernel threads.
    447       1.19    dyoung 	 */
    448  1.34.14.1      phil 	if (vap->iv_auth->ia_detach != NULL)
    449  1.34.14.1      phil 		vap->iv_auth->ia_detach(vap);
    450       1.19    dyoung 	/*
    451       1.19    dyoung 	 * Detach any ACL'ator.
    452       1.19    dyoung 	 */
    453  1.34.14.1      phil 	if (vap->iv_acl != NULL)
    454  1.34.14.1      phil 		vap->iv_acl->iac_detach(vap);
    455  1.34.14.1      phil 
    456  1.34.14.1      phil 	FREEAPPIE(vap->iv_appie_beacon);
    457  1.34.14.1      phil 	FREEAPPIE(vap->iv_appie_probereq);
    458  1.34.14.1      phil 	FREEAPPIE(vap->iv_appie_proberesp);
    459  1.34.14.1      phil 	FREEAPPIE(vap->iv_appie_assocreq);
    460  1.34.14.1      phil 	FREEAPPIE(vap->iv_appie_assocresp);
    461  1.34.14.1      phil 	FREEAPPIE(vap->iv_appie_wpa);
    462  1.34.14.1      phil #undef FREEAPPIE
    463       1.19    dyoung }
    464       1.19    dyoung 
    465       1.19    dyoung /*
    466       1.19    dyoung  * Simple-minded authenticator module support.
    467       1.19    dyoung  */
    468       1.19    dyoung 
    469       1.19    dyoung #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
    470       1.19    dyoung /* XXX well-known names */
    471  1.34.14.3      phil #if __FreeBSD__
    472       1.19    dyoung static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
    473       1.19    dyoung 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
    474       1.19    dyoung 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
    475       1.19    dyoung 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
    476       1.19    dyoung 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
    477       1.19    dyoung 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
    478       1.19    dyoung 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
    479       1.19    dyoung };
    480  1.34.14.3      phil #endif
    481  1.34.14.3      phil 
    482       1.19    dyoung static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
    483       1.19    dyoung 
    484       1.19    dyoung static const struct ieee80211_authenticator auth_internal = {
    485       1.19    dyoung 	.ia_name		= "wlan_internal",
    486       1.19    dyoung 	.ia_attach		= NULL,
    487       1.19    dyoung 	.ia_detach		= NULL,
    488       1.19    dyoung 	.ia_node_join		= NULL,
    489       1.19    dyoung 	.ia_node_leave		= NULL,
    490       1.19    dyoung };
    491       1.19    dyoung 
    492  1.34.14.3      phil 
    493       1.19    dyoung /*
    494       1.19    dyoung  * Setup internal authenticators once; they are never unregistered.
    495       1.19    dyoung  */
    496  1.34.14.3      phil #if __FreeBSD__
    497  1.34.14.3      phil static void
    498  1.34.14.3      phil #elif __NetBSD__
    499  1.34.14.3      phil void
    500  1.34.14.3      phil #endif
    501       1.19    dyoung ieee80211_auth_setup(void)
    502       1.19    dyoung {
    503       1.19    dyoung 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
    504       1.19    dyoung 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
    505       1.19    dyoung 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
    506  1.34.14.3      phil #if __NetBSD__
    507  1.34.14.3      phil 	ieee80211_authenticator_register(IEEE80211_AUTH_8021X, &auth_xauth);
    508  1.34.14.3      phil 	ieee80211_authenticator_register(IEEE80211_AUTH_WPA, &auth_xauth);
    509  1.34.14.3      phil #endif
    510       1.19    dyoung }
    511  1.34.14.1      phil SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
    512       1.19    dyoung 
    513       1.19    dyoung const struct ieee80211_authenticator *
    514       1.19    dyoung ieee80211_authenticator_get(int auth)
    515       1.19    dyoung {
    516       1.19    dyoung 	if (auth >= IEEE80211_AUTH_MAX)
    517       1.19    dyoung 		return NULL;
    518  1.34.14.3      phil #if __FreeBSD__
    519       1.19    dyoung 	if (authenticators[auth] == NULL)
    520       1.19    dyoung 		ieee80211_load_module(auth_modnames[auth]);
    521  1.34.14.3      phil #endif
    522       1.19    dyoung 	return authenticators[auth];
    523       1.19    dyoung }
    524       1.19    dyoung 
    525       1.19    dyoung void
    526       1.19    dyoung ieee80211_authenticator_register(int type,
    527       1.19    dyoung 	const struct ieee80211_authenticator *auth)
    528       1.19    dyoung {
    529       1.19    dyoung 	if (type >= IEEE80211_AUTH_MAX)
    530       1.19    dyoung 		return;
    531       1.19    dyoung 	authenticators[type] = auth;
    532       1.19    dyoung }
    533       1.19    dyoung 
    534       1.19    dyoung void
    535       1.19    dyoung ieee80211_authenticator_unregister(int type)
    536       1.19    dyoung {
    537       1.19    dyoung 
    538       1.19    dyoung 	if (type >= IEEE80211_AUTH_MAX)
    539       1.19    dyoung 		return;
    540       1.19    dyoung 	authenticators[type] = NULL;
    541       1.19    dyoung }
    542       1.19    dyoung 
    543       1.19    dyoung /*
    544       1.19    dyoung  * Very simple-minded ACL module support.
    545       1.19    dyoung  */
    546       1.19    dyoung /* XXX just one for now */
    547       1.19    dyoung static	const struct ieee80211_aclator *acl = NULL;
    548       1.19    dyoung 
    549       1.19    dyoung void
    550       1.19    dyoung ieee80211_aclator_register(const struct ieee80211_aclator *iac)
    551       1.19    dyoung {
    552       1.19    dyoung 	printf("wlan: %s acl policy registered\n", iac->iac_name);
    553       1.19    dyoung 	acl = iac;
    554       1.19    dyoung }
    555       1.19    dyoung 
    556       1.19    dyoung void
    557       1.19    dyoung ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
    558       1.19    dyoung {
    559       1.19    dyoung 	if (acl == iac)
    560       1.19    dyoung 		acl = NULL;
    561       1.19    dyoung 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
    562       1.19    dyoung }
    563       1.19    dyoung 
    564       1.19    dyoung const struct ieee80211_aclator *
    565       1.19    dyoung ieee80211_aclator_get(const char *name)
    566       1.19    dyoung {
    567  1.34.14.3      phil #if __FreeBSD__
    568       1.19    dyoung 	if (acl == NULL)
    569       1.19    dyoung 		ieee80211_load_module("wlan_acl");
    570  1.34.14.3      phil #endif
    571       1.19    dyoung 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
    572        1.1    dyoung }
    573        1.1    dyoung 
    574        1.1    dyoung void
    575  1.34.14.1      phil ieee80211_print_essid(const uint8_t *essid, int len)
    576        1.1    dyoung {
    577  1.34.14.1      phil 	const uint8_t *p;
    578        1.1    dyoung 	int i;
    579        1.1    dyoung 
    580        1.1    dyoung 	if (len > IEEE80211_NWID_LEN)
    581        1.1    dyoung 		len = IEEE80211_NWID_LEN;
    582        1.1    dyoung 	/* determine printable or not */
    583        1.1    dyoung 	for (i = 0, p = essid; i < len; i++, p++) {
    584        1.1    dyoung 		if (*p < ' ' || *p > 0x7e)
    585        1.1    dyoung 			break;
    586        1.1    dyoung 	}
    587        1.1    dyoung 	if (i == len) {
    588        1.1    dyoung 		printf("\"");
    589        1.1    dyoung 		for (i = 0, p = essid; i < len; i++, p++)
    590        1.1    dyoung 			printf("%c", *p);
    591        1.1    dyoung 		printf("\"");
    592        1.1    dyoung 	} else {
    593        1.1    dyoung 		printf("0x");
    594        1.1    dyoung 		for (i = 0, p = essid; i < len; i++, p++)
    595        1.1    dyoung 			printf("%02x", *p);
    596        1.1    dyoung 	}
    597        1.1    dyoung }
    598        1.1    dyoung 
    599        1.1    dyoung void
    600  1.34.14.1      phil ieee80211_dump_pkt(struct ieee80211com *ic,
    601  1.34.14.1      phil 	const uint8_t *buf, int len, int rate, int rssi)
    602        1.1    dyoung {
    603       1.19    dyoung 	const struct ieee80211_frame *wh;
    604        1.1    dyoung 	int i;
    605        1.1    dyoung 
    606       1.19    dyoung 	wh = (const struct ieee80211_frame *)buf;
    607        1.1    dyoung 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    608        1.1    dyoung 	case IEEE80211_FC1_DIR_NODS:
    609        1.1    dyoung 		printf("NODS %s", ether_sprintf(wh->i_addr2));
    610        1.1    dyoung 		printf("->%s", ether_sprintf(wh->i_addr1));
    611        1.1    dyoung 		printf("(%s)", ether_sprintf(wh->i_addr3));
    612        1.1    dyoung 		break;
    613        1.1    dyoung 	case IEEE80211_FC1_DIR_TODS:
    614        1.1    dyoung 		printf("TODS %s", ether_sprintf(wh->i_addr2));
    615        1.1    dyoung 		printf("->%s", ether_sprintf(wh->i_addr3));
    616        1.1    dyoung 		printf("(%s)", ether_sprintf(wh->i_addr1));
    617        1.1    dyoung 		break;
    618        1.1    dyoung 	case IEEE80211_FC1_DIR_FROMDS:
    619        1.1    dyoung 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
    620        1.1    dyoung 		printf("->%s", ether_sprintf(wh->i_addr1));
    621        1.1    dyoung 		printf("(%s)", ether_sprintf(wh->i_addr2));
    622        1.1    dyoung 		break;
    623        1.1    dyoung 	case IEEE80211_FC1_DIR_DSTODS:
    624  1.34.14.1      phil 		printf("DSDS %s", ether_sprintf((const uint8_t *)&wh[1]));
    625        1.1    dyoung 		printf("->%s", ether_sprintf(wh->i_addr3));
    626        1.1    dyoung 		printf("(%s", ether_sprintf(wh->i_addr2));
    627        1.1    dyoung 		printf("->%s)", ether_sprintf(wh->i_addr1));
    628        1.1    dyoung 		break;
    629        1.1    dyoung 	}
    630        1.1    dyoung 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
    631        1.1    dyoung 	case IEEE80211_FC0_TYPE_DATA:
    632        1.1    dyoung 		printf(" data");
    633        1.1    dyoung 		break;
    634        1.1    dyoung 	case IEEE80211_FC0_TYPE_MGT:
    635  1.34.14.1      phil 		printf(" %s", ieee80211_mgt_subtype_name(wh->i_fc[0]));
    636        1.1    dyoung 		break;
    637        1.1    dyoung 	default:
    638        1.1    dyoung 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
    639        1.1    dyoung 		break;
    640        1.1    dyoung 	}
    641  1.34.14.1      phil 	if (IEEE80211_QOS_HAS_SEQ(wh)) {
    642  1.34.14.1      phil 		const struct ieee80211_qosframe *qwh =
    643  1.34.14.1      phil 			(const struct ieee80211_qosframe *)buf;
    644  1.34.14.1      phil 		printf(" QoS [TID %u%s]", qwh->i_qos[0] & IEEE80211_QOS_TID,
    645  1.34.14.1      phil 			qwh->i_qos[0] & IEEE80211_QOS_ACKPOLICY ? " ACM" : "");
    646  1.34.14.1      phil 	}
    647  1.34.14.1      phil 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
    648  1.34.14.1      phil 		int off;
    649  1.34.14.1      phil 
    650  1.34.14.1      phil 		off = ieee80211_anyhdrspace(ic, wh);
    651  1.34.14.1      phil 		printf(" WEP [IV %.02x %.02x %.02x",
    652  1.34.14.1      phil 			buf[off+0], buf[off+1], buf[off+2]);
    653  1.34.14.1      phil 		if (buf[off+IEEE80211_WEP_IVLEN] & IEEE80211_WEP_EXTIV)
    654  1.34.14.1      phil 			printf(" %.02x %.02x %.02x",
    655  1.34.14.1      phil 				buf[off+4], buf[off+5], buf[off+6]);
    656  1.34.14.1      phil 		printf(" KID %u]", buf[off+IEEE80211_WEP_IVLEN] >> 6);
    657       1.19    dyoung 	}
    658        1.1    dyoung 	if (rate >= 0)
    659        1.1    dyoung 		printf(" %dM", rate / 2);
    660        1.1    dyoung 	if (rssi >= 0)
    661        1.1    dyoung 		printf(" +%d", rssi);
    662        1.1    dyoung 	printf("\n");
    663        1.1    dyoung 	if (len > 0) {
    664        1.1    dyoung 		for (i = 0; i < len; i++) {
    665        1.1    dyoung 			if ((i & 1) == 0)
    666        1.1    dyoung 				printf(" ");
    667        1.1    dyoung 			printf("%02x", buf[i]);
    668        1.1    dyoung 		}
    669        1.1    dyoung 		printf("\n");
    670        1.1    dyoung 	}
    671        1.1    dyoung }
    672        1.1    dyoung 
    673  1.34.14.1      phil static __inline int
    674  1.34.14.1      phil findrix(const struct ieee80211_rateset *rs, int r)
    675  1.34.14.1      phil {
    676  1.34.14.1      phil 	int i;
    677  1.34.14.1      phil 
    678  1.34.14.1      phil 	for (i = 0; i < rs->rs_nrates; i++)
    679  1.34.14.1      phil 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == r)
    680  1.34.14.1      phil 			return i;
    681  1.34.14.1      phil 	return -1;
    682  1.34.14.1      phil }
    683  1.34.14.1      phil 
    684        1.1    dyoung int
    685  1.34.14.1      phil ieee80211_fix_rate(struct ieee80211_node *ni,
    686  1.34.14.1      phil 	struct ieee80211_rateset *nrs, int flags)
    687        1.1    dyoung {
    688  1.34.14.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    689       1.23     skrll 	struct ieee80211com *ic = ni->ni_ic;
    690  1.34.14.1      phil 	int i, j, rix, error;
    691  1.34.14.1      phil 	int okrate, badrate, fixedrate, ucastrate;
    692  1.34.14.1      phil 	const struct ieee80211_rateset *srs;
    693  1.34.14.1      phil 	uint8_t r;
    694        1.1    dyoung 
    695        1.1    dyoung 	error = 0;
    696  1.34.14.1      phil 	okrate = badrate = 0;
    697  1.34.14.1      phil 	ucastrate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].ucastrate;
    698  1.34.14.1      phil 	if (ucastrate != IEEE80211_FIXED_RATE_NONE) {
    699  1.34.14.1      phil 		/*
    700  1.34.14.1      phil 		 * Workaround awkwardness with fixed rate.  We are called
    701  1.34.14.1      phil 		 * to check both the legacy rate set and the HT rate set
    702  1.34.14.1      phil 		 * but we must apply any legacy fixed rate check only to the
    703  1.34.14.1      phil 		 * legacy rate set and vice versa.  We cannot tell what type
    704  1.34.14.1      phil 		 * of rate set we've been given (legacy or HT) but we can
    705  1.34.14.1      phil 		 * distinguish the fixed rate type (MCS have 0x80 set).
    706  1.34.14.1      phil 		 * So to deal with this the caller communicates whether to
    707  1.34.14.1      phil 		 * check MCS or legacy rate using the flags and we use the
    708  1.34.14.1      phil 		 * type of any fixed rate to avoid applying an MCS to a
    709  1.34.14.1      phil 		 * legacy rate and vice versa.
    710  1.34.14.1      phil 		 */
    711  1.34.14.1      phil 		if (ucastrate & 0x80) {
    712  1.34.14.1      phil 			if (flags & IEEE80211_F_DOFRATE)
    713  1.34.14.1      phil 				flags &= ~IEEE80211_F_DOFRATE;
    714  1.34.14.1      phil 		} else if ((ucastrate & 0x80) == 0) {
    715  1.34.14.1      phil 			if (flags & IEEE80211_F_DOFMCS)
    716  1.34.14.1      phil 				flags &= ~IEEE80211_F_DOFMCS;
    717  1.34.14.1      phil 		}
    718  1.34.14.1      phil 		/* NB: required to make MCS match below work */
    719  1.34.14.1      phil 		ucastrate &= IEEE80211_RATE_VAL;
    720  1.34.14.1      phil 	}
    721  1.34.14.1      phil 	fixedrate = IEEE80211_FIXED_RATE_NONE;
    722  1.34.14.1      phil 	/*
    723  1.34.14.1      phil 	 * XXX we are called to process both MCS and legacy rates;
    724  1.34.14.1      phil 	 * we must use the appropriate basic rate set or chaos will
    725  1.34.14.1      phil 	 * ensue; for now callers that want MCS must supply
    726  1.34.14.1      phil 	 * IEEE80211_F_DOBRS; at some point we'll need to split this
    727  1.34.14.1      phil 	 * function so there are two variants, one for MCS and one
    728  1.34.14.1      phil 	 * for legacy rates.
    729  1.34.14.1      phil 	 */
    730  1.34.14.1      phil 	if (flags & IEEE80211_F_DOBRS)
    731  1.34.14.1      phil 		srs = (const struct ieee80211_rateset *)
    732  1.34.14.1      phil 		    ieee80211_get_suphtrates(ic, ni->ni_chan);
    733  1.34.14.1      phil 	else
    734  1.34.14.1      phil 		srs = ieee80211_get_suprates(ic, ni->ni_chan);
    735        1.6    dyoung 	for (i = 0; i < nrs->rs_nrates; ) {
    736  1.34.14.1      phil 		if (flags & IEEE80211_F_DOSORT) {
    737        1.1    dyoung 			/*
    738        1.1    dyoung 			 * Sort rates.
    739        1.1    dyoung 			 */
    740        1.1    dyoung 			for (j = i + 1; j < nrs->rs_nrates; j++) {
    741  1.34.14.1      phil 				if (IEEE80211_RV(nrs->rs_rates[i]) >
    742  1.34.14.1      phil 				    IEEE80211_RV(nrs->rs_rates[j])) {
    743        1.1    dyoung 					r = nrs->rs_rates[i];
    744        1.1    dyoung 					nrs->rs_rates[i] = nrs->rs_rates[j];
    745        1.1    dyoung 					nrs->rs_rates[j] = r;
    746        1.1    dyoung 				}
    747        1.1    dyoung 			}
    748        1.1    dyoung 		}
    749        1.1    dyoung 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
    750        1.1    dyoung 		badrate = r;
    751  1.34.14.1      phil 		/*
    752  1.34.14.1      phil 		 * Check for fixed rate.
    753  1.34.14.1      phil 		 */
    754  1.34.14.1      phil 		if (r == ucastrate)
    755  1.34.14.1      phil 			fixedrate = r;
    756  1.34.14.1      phil 		/*
    757  1.34.14.1      phil 		 * Check against supported rates.
    758  1.34.14.1      phil 		 */
    759  1.34.14.1      phil 		rix = findrix(srs, r);
    760  1.34.14.1      phil 		if (flags & IEEE80211_F_DONEGO) {
    761  1.34.14.1      phil 			if (rix < 0) {
    762        1.6    dyoung 				/*
    763        1.6    dyoung 				 * A rate in the node's rate set is not
    764        1.6    dyoung 				 * supported.  If this is a basic rate and we
    765  1.34.14.1      phil 				 * are operating as a STA then this is an error.
    766        1.6    dyoung 				 * Otherwise we just discard/ignore the rate.
    767        1.6    dyoung 				 */
    768  1.34.14.1      phil 				if ((flags & IEEE80211_F_JOIN) &&
    769        1.6    dyoung 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
    770        1.1    dyoung 					error++;
    771  1.34.14.1      phil 			} else if ((flags & IEEE80211_F_JOIN) == 0) {
    772  1.34.14.1      phil 				/*
    773  1.34.14.1      phil 				 * Overwrite with the supported rate
    774  1.34.14.1      phil 				 * value so any basic rate bit is set.
    775  1.34.14.1      phil 				 */
    776  1.34.14.1      phil 				nrs->rs_rates[i] = srs->rs_rates[rix];
    777        1.1    dyoung 			}
    778        1.1    dyoung 		}
    779  1.34.14.1      phil 		if ((flags & IEEE80211_F_DODEL) && rix < 0) {
    780        1.1    dyoung 			/*
    781        1.1    dyoung 			 * Delete unacceptable rates.
    782        1.1    dyoung 			 */
    783  1.34.14.1      phil 			nrs->rs_nrates--;
    784  1.34.14.1      phil 			for (j = i; j < nrs->rs_nrates; j++)
    785  1.34.14.1      phil 				nrs->rs_rates[j] = nrs->rs_rates[j + 1];
    786  1.34.14.1      phil 			nrs->rs_rates[j] = 0;
    787  1.34.14.1      phil 			continue;
    788        1.1    dyoung 		}
    789  1.34.14.1      phil 		if (rix >= 0)
    790        1.1    dyoung 			okrate = nrs->rs_rates[i];
    791        1.1    dyoung 		i++;
    792        1.1    dyoung 	}
    793       1.19    dyoung 	if (okrate == 0 || error != 0 ||
    794  1.34.14.1      phil 	    ((flags & (IEEE80211_F_DOFRATE|IEEE80211_F_DOFMCS)) &&
    795  1.34.14.1      phil 	     fixedrate != ucastrate)) {
    796  1.34.14.1      phil 		IEEE80211_NOTE(vap, IEEE80211_MSG_XRATE | IEEE80211_MSG_11N, ni,
    797  1.34.14.1      phil 		    "%s: flags 0x%x okrate %d error %d fixedrate 0x%x "
    798  1.34.14.1      phil 		    "ucastrate %x\n", __func__, fixedrate, ucastrate, flags);
    799        1.1    dyoung 		return badrate | IEEE80211_RATE_BASIC;
    800  1.34.14.1      phil 	} else
    801  1.34.14.1      phil 		return IEEE80211_RV(okrate);
    802        1.1    dyoung }
    803        1.1    dyoung 
    804       1.19    dyoung /*
    805       1.19    dyoung  * Reset 11g-related state.
    806       1.19    dyoung  */
    807       1.19    dyoung void
    808       1.19    dyoung ieee80211_reset_erp(struct ieee80211com *ic)
    809       1.19    dyoung {
    810       1.19    dyoung 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
    811       1.19    dyoung 	ic->ic_nonerpsta = 0;
    812       1.19    dyoung 	ic->ic_longslotsta = 0;
    813       1.19    dyoung 	/*
    814       1.19    dyoung 	 * Short slot time is enabled only when operating in 11g
    815       1.19    dyoung 	 * and not in an IBSS.  We must also honor whether or not
    816       1.19    dyoung 	 * the driver is capable of doing it.
    817       1.19    dyoung 	 */
    818       1.19    dyoung 	ieee80211_set_shortslottime(ic,
    819  1.34.14.1      phil 		IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
    820  1.34.14.1      phil 		IEEE80211_IS_CHAN_HT(ic->ic_curchan) ||
    821  1.34.14.1      phil 		(IEEE80211_IS_CHAN_ANYG(ic->ic_curchan) &&
    822       1.19    dyoung 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
    823       1.19    dyoung 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
    824       1.19    dyoung 	/*
    825       1.19    dyoung 	 * Set short preamble and ERP barker-preamble flags.
    826       1.19    dyoung 	 */
    827  1.34.14.1      phil 	if (IEEE80211_IS_CHAN_A(ic->ic_curchan) ||
    828       1.19    dyoung 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
    829       1.19    dyoung 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    830       1.19    dyoung 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
    831       1.19    dyoung 	} else {
    832       1.19    dyoung 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
    833       1.19    dyoung 		ic->ic_flags |= IEEE80211_F_USEBARKER;
    834       1.19    dyoung 	}
    835       1.19    dyoung }
    836       1.19    dyoung 
    837       1.19    dyoung /*
    838       1.19    dyoung  * Set the short slot time state and notify the driver.
    839       1.19    dyoung  */
    840       1.19    dyoung void
    841       1.19    dyoung ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
    842       1.19    dyoung {
    843       1.19    dyoung 	if (onoff)
    844       1.19    dyoung 		ic->ic_flags |= IEEE80211_F_SHSLOT;
    845       1.19    dyoung 	else
    846       1.19    dyoung 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
    847       1.19    dyoung 	/* notify driver */
    848       1.19    dyoung 	if (ic->ic_updateslot != NULL)
    849  1.34.14.1      phil 		ic->ic_updateslot(ic);
    850       1.19    dyoung }
    851       1.19    dyoung 
    852       1.19    dyoung /*
    853       1.19    dyoung  * Check if the specified rate set supports ERP.
    854       1.19    dyoung  * NB: the rate set is assumed to be sorted.
    855       1.19    dyoung  */
    856       1.19    dyoung int
    857  1.34.14.1      phil ieee80211_iserp_rateset(const struct ieee80211_rateset *rs)
    858       1.19    dyoung {
    859       1.19    dyoung 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
    860       1.19    dyoung 	int i, j;
    861       1.19    dyoung 
    862  1.34.14.1      phil 	if (rs->rs_nrates < nitems(rates))
    863       1.19    dyoung 		return 0;
    864  1.34.14.1      phil 	for (i = 0; i < nitems(rates); i++) {
    865       1.19    dyoung 		for (j = 0; j < rs->rs_nrates; j++) {
    866       1.19    dyoung 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
    867       1.19    dyoung 			if (rates[i] == r)
    868       1.19    dyoung 				goto next;
    869       1.19    dyoung 			if (r > rates[i])
    870       1.19    dyoung 				return 0;
    871       1.19    dyoung 		}
    872       1.19    dyoung 		return 0;
    873       1.19    dyoung 	next:
    874       1.19    dyoung 		;
    875       1.19    dyoung 	}
    876       1.19    dyoung 	return 1;
    877       1.19    dyoung }
    878       1.19    dyoung 
    879       1.19    dyoung /*
    880  1.34.14.1      phil  * Mark the basic rates for the rate table based on the
    881       1.19    dyoung  * operating mode.  For real 11g we mark all the 11b rates
    882       1.19    dyoung  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    883       1.19    dyoung  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    884       1.19    dyoung  * the basic OFDM rates.
    885       1.19    dyoung  */
    886  1.34.14.1      phil static void
    887  1.34.14.1      phil setbasicrates(struct ieee80211_rateset *rs,
    888  1.34.14.1      phil     enum ieee80211_phymode mode, int add)
    889       1.19    dyoung {
    890  1.34.14.1      phil 	static const struct ieee80211_rateset basic[IEEE80211_MODE_MAX] = {
    891  1.34.14.1      phil 	    [IEEE80211_MODE_11A]	= { 3, { 12, 24, 48 } },
    892  1.34.14.1      phil 	    [IEEE80211_MODE_11B]	= { 2, { 2, 4 } },
    893  1.34.14.1      phil 					    /* NB: mixed b/g */
    894  1.34.14.1      phil 	    [IEEE80211_MODE_11G]	= { 4, { 2, 4, 11, 22 } },
    895  1.34.14.1      phil 	    [IEEE80211_MODE_TURBO_A]	= { 3, { 12, 24, 48 } },
    896  1.34.14.1      phil 	    [IEEE80211_MODE_TURBO_G]	= { 4, { 2, 4, 11, 22 } },
    897  1.34.14.1      phil 	    [IEEE80211_MODE_STURBO_A]	= { 3, { 12, 24, 48 } },
    898  1.34.14.1      phil 	    [IEEE80211_MODE_HALF]	= { 3, { 6, 12, 24 } },
    899  1.34.14.1      phil 	    [IEEE80211_MODE_QUARTER]	= { 3, { 3, 6, 12 } },
    900  1.34.14.1      phil 	    [IEEE80211_MODE_11NA]	= { 3, { 12, 24, 48 } },
    901  1.34.14.1      phil 					    /* NB: mixed b/g */
    902  1.34.14.1      phil 	    [IEEE80211_MODE_11NG]	= { 4, { 2, 4, 11, 22 } },
    903  1.34.14.1      phil 					    /* NB: mixed b/g */
    904  1.34.14.1      phil 	    [IEEE80211_MODE_VHT_2GHZ]	= { 4, { 2, 4, 11, 22 } },
    905  1.34.14.1      phil 	    [IEEE80211_MODE_VHT_5GHZ]	= { 3, { 12, 24, 48 } },
    906       1.19    dyoung 	};
    907       1.19    dyoung 	int i, j;
    908       1.19    dyoung 
    909       1.19    dyoung 	for (i = 0; i < rs->rs_nrates; i++) {
    910  1.34.14.1      phil 		if (!add)
    911  1.34.14.1      phil 			rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    912       1.19    dyoung 		for (j = 0; j < basic[mode].rs_nrates; j++)
    913       1.19    dyoung 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    914       1.19    dyoung 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    915       1.19    dyoung 				break;
    916       1.19    dyoung 			}
    917       1.19    dyoung 	}
    918       1.19    dyoung }
    919       1.19    dyoung 
    920       1.19    dyoung /*
    921  1.34.14.1      phil  * Set the basic rates in a rate set.
    922  1.34.14.1      phil  */
    923  1.34.14.1      phil void
    924  1.34.14.1      phil ieee80211_setbasicrates(struct ieee80211_rateset *rs,
    925  1.34.14.1      phil     enum ieee80211_phymode mode)
    926  1.34.14.1      phil {
    927  1.34.14.1      phil 	setbasicrates(rs, mode, 0);
    928  1.34.14.1      phil }
    929  1.34.14.1      phil 
    930  1.34.14.1      phil /*
    931  1.34.14.1      phil  * Add basic rates to a rate set.
    932  1.34.14.1      phil  */
    933  1.34.14.1      phil void
    934  1.34.14.1      phil ieee80211_addbasicrates(struct ieee80211_rateset *rs,
    935  1.34.14.1      phil     enum ieee80211_phymode mode)
    936  1.34.14.1      phil {
    937  1.34.14.1      phil 	setbasicrates(rs, mode, 1);
    938  1.34.14.1      phil }
    939  1.34.14.1      phil 
    940  1.34.14.1      phil /*
    941  1.34.14.1      phil  * WME protocol support.
    942  1.34.14.1      phil  *
    943  1.34.14.1      phil  * The default 11a/b/g/n parameters come from the WiFi Alliance WMM
    944  1.34.14.1      phil  * System Interopability Test Plan (v1.4, Appendix F) and the 802.11n
    945  1.34.14.1      phil  * Draft 2.0 Test Plan (Appendix D).
    946  1.34.14.1      phil  *
    947  1.34.14.1      phil  * Static/Dynamic Turbo mode settings come from Atheros.
    948       1.19    dyoung  */
    949       1.19    dyoung typedef struct phyParamType {
    950  1.34.14.1      phil 	uint8_t		aifsn;
    951  1.34.14.1      phil 	uint8_t		logcwmin;
    952  1.34.14.1      phil 	uint8_t		logcwmax;
    953  1.34.14.1      phil 	uint16_t	txopLimit;
    954  1.34.14.1      phil 	uint8_t 	acm;
    955       1.19    dyoung } paramType;
    956       1.19    dyoung 
    957       1.19    dyoung static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
    958  1.34.14.1      phil 	[IEEE80211_MODE_AUTO]	= { 3, 4,  6,  0, 0 },
    959  1.34.14.1      phil 	[IEEE80211_MODE_11A]	= { 3, 4,  6,  0, 0 },
    960  1.34.14.1      phil 	[IEEE80211_MODE_11B]	= { 3, 4,  6,  0, 0 },
    961  1.34.14.1      phil 	[IEEE80211_MODE_11G]	= { 3, 4,  6,  0, 0 },
    962  1.34.14.1      phil 	[IEEE80211_MODE_FH]	= { 3, 4,  6,  0, 0 },
    963  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_A]= { 2, 3,  5,  0, 0 },
    964  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_G]= { 2, 3,  5,  0, 0 },
    965  1.34.14.1      phil 	[IEEE80211_MODE_STURBO_A]={ 2, 3,  5,  0, 0 },
    966  1.34.14.1      phil 	[IEEE80211_MODE_HALF]	= { 3, 4,  6,  0, 0 },
    967  1.34.14.1      phil 	[IEEE80211_MODE_QUARTER]= { 3, 4,  6,  0, 0 },
    968  1.34.14.1      phil 	[IEEE80211_MODE_11NA]	= { 3, 4,  6,  0, 0 },
    969  1.34.14.1      phil 	[IEEE80211_MODE_11NG]	= { 3, 4,  6,  0, 0 },
    970  1.34.14.1      phil 	[IEEE80211_MODE_VHT_2GHZ]	= { 3, 4,  6,  0, 0 },
    971  1.34.14.1      phil 	[IEEE80211_MODE_VHT_5GHZ]	= { 3, 4,  6,  0, 0 },
    972       1.19    dyoung };
    973       1.19    dyoung static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
    974  1.34.14.1      phil 	[IEEE80211_MODE_AUTO]	= { 7, 4, 10,  0, 0 },
    975  1.34.14.1      phil 	[IEEE80211_MODE_11A]	= { 7, 4, 10,  0, 0 },
    976  1.34.14.1      phil 	[IEEE80211_MODE_11B]	= { 7, 4, 10,  0, 0 },
    977  1.34.14.1      phil 	[IEEE80211_MODE_11G]	= { 7, 4, 10,  0, 0 },
    978  1.34.14.1      phil 	[IEEE80211_MODE_FH]	= { 7, 4, 10,  0, 0 },
    979  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_A]= { 7, 3, 10,  0, 0 },
    980  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_G]= { 7, 3, 10,  0, 0 },
    981  1.34.14.1      phil 	[IEEE80211_MODE_STURBO_A]={ 7, 3, 10,  0, 0 },
    982  1.34.14.1      phil 	[IEEE80211_MODE_HALF]	= { 7, 4, 10,  0, 0 },
    983  1.34.14.1      phil 	[IEEE80211_MODE_QUARTER]= { 7, 4, 10,  0, 0 },
    984  1.34.14.1      phil 	[IEEE80211_MODE_11NA]	= { 7, 4, 10,  0, 0 },
    985  1.34.14.1      phil 	[IEEE80211_MODE_11NG]	= { 7, 4, 10,  0, 0 },
    986  1.34.14.1      phil 	[IEEE80211_MODE_VHT_2GHZ]	= { 7, 4, 10,  0, 0 },
    987  1.34.14.1      phil 	[IEEE80211_MODE_VHT_5GHZ]	= { 7, 4, 10,  0, 0 },
    988       1.19    dyoung };
    989       1.19    dyoung static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
    990  1.34.14.1      phil 	[IEEE80211_MODE_AUTO]	= { 1, 3, 4,  94, 0 },
    991  1.34.14.1      phil 	[IEEE80211_MODE_11A]	= { 1, 3, 4,  94, 0 },
    992  1.34.14.1      phil 	[IEEE80211_MODE_11B]	= { 1, 3, 4, 188, 0 },
    993  1.34.14.1      phil 	[IEEE80211_MODE_11G]	= { 1, 3, 4,  94, 0 },
    994  1.34.14.1      phil 	[IEEE80211_MODE_FH]	= { 1, 3, 4, 188, 0 },
    995  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 3,  94, 0 },
    996  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 3,  94, 0 },
    997  1.34.14.1      phil 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 3,  94, 0 },
    998  1.34.14.1      phil 	[IEEE80211_MODE_HALF]	= { 1, 3, 4,  94, 0 },
    999  1.34.14.1      phil 	[IEEE80211_MODE_QUARTER]= { 1, 3, 4,  94, 0 },
   1000  1.34.14.1      phil 	[IEEE80211_MODE_11NA]	= { 1, 3, 4,  94, 0 },
   1001  1.34.14.1      phil 	[IEEE80211_MODE_11NG]	= { 1, 3, 4,  94, 0 },
   1002  1.34.14.1      phil 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 3, 4,  94, 0 },
   1003  1.34.14.1      phil 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 3, 4,  94, 0 },
   1004       1.19    dyoung };
   1005       1.19    dyoung static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
   1006  1.34.14.1      phil 	[IEEE80211_MODE_AUTO]	= { 1, 2, 3,  47, 0 },
   1007  1.34.14.1      phil 	[IEEE80211_MODE_11A]	= { 1, 2, 3,  47, 0 },
   1008  1.34.14.1      phil 	[IEEE80211_MODE_11B]	= { 1, 2, 3, 102, 0 },
   1009  1.34.14.1      phil 	[IEEE80211_MODE_11G]	= { 1, 2, 3,  47, 0 },
   1010  1.34.14.1      phil 	[IEEE80211_MODE_FH]	= { 1, 2, 3, 102, 0 },
   1011  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
   1012  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
   1013  1.34.14.1      phil 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
   1014  1.34.14.1      phil 	[IEEE80211_MODE_HALF]	= { 1, 2, 3,  47, 0 },
   1015  1.34.14.1      phil 	[IEEE80211_MODE_QUARTER]= { 1, 2, 3,  47, 0 },
   1016  1.34.14.1      phil 	[IEEE80211_MODE_11NA]	= { 1, 2, 3,  47, 0 },
   1017  1.34.14.1      phil 	[IEEE80211_MODE_11NG]	= { 1, 2, 3,  47, 0 },
   1018  1.34.14.1      phil 	[IEEE80211_MODE_VHT_2GHZ]	= { 1, 2, 3,  47, 0 },
   1019  1.34.14.1      phil 	[IEEE80211_MODE_VHT_5GHZ]	= { 1, 2, 3,  47, 0 },
   1020       1.19    dyoung };
   1021       1.19    dyoung 
   1022       1.19    dyoung static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
   1023  1.34.14.1      phil 	[IEEE80211_MODE_AUTO]	= { 3, 4, 10,  0, 0 },
   1024  1.34.14.1      phil 	[IEEE80211_MODE_11A]	= { 3, 4, 10,  0, 0 },
   1025  1.34.14.1      phil 	[IEEE80211_MODE_11B]	= { 3, 4, 10,  0, 0 },
   1026  1.34.14.1      phil 	[IEEE80211_MODE_11G]	= { 3, 4, 10,  0, 0 },
   1027  1.34.14.1      phil 	[IEEE80211_MODE_FH]	= { 3, 4, 10,  0, 0 },
   1028  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_A]= { 2, 3, 10,  0, 0 },
   1029  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_G]= { 2, 3, 10,  0, 0 },
   1030  1.34.14.1      phil 	[IEEE80211_MODE_STURBO_A]={ 2, 3, 10,  0, 0 },
   1031  1.34.14.1      phil 	[IEEE80211_MODE_HALF]	= { 3, 4, 10,  0, 0 },
   1032  1.34.14.1      phil 	[IEEE80211_MODE_QUARTER]= { 3, 4, 10,  0, 0 },
   1033  1.34.14.1      phil 	[IEEE80211_MODE_11NA]	= { 3, 4, 10,  0, 0 },
   1034  1.34.14.1      phil 	[IEEE80211_MODE_11NG]	= { 3, 4, 10,  0, 0 },
   1035       1.19    dyoung };
   1036       1.19    dyoung static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
   1037  1.34.14.1      phil 	[IEEE80211_MODE_AUTO]	= { 2, 3, 4,  94, 0 },
   1038  1.34.14.1      phil 	[IEEE80211_MODE_11A]	= { 2, 3, 4,  94, 0 },
   1039  1.34.14.1      phil 	[IEEE80211_MODE_11B]	= { 2, 3, 4, 188, 0 },
   1040  1.34.14.1      phil 	[IEEE80211_MODE_11G]	= { 2, 3, 4,  94, 0 },
   1041  1.34.14.1      phil 	[IEEE80211_MODE_FH]	= { 2, 3, 4, 188, 0 },
   1042  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_A]= { 2, 2, 3,  94, 0 },
   1043  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_G]= { 2, 2, 3,  94, 0 },
   1044  1.34.14.1      phil 	[IEEE80211_MODE_STURBO_A]={ 2, 2, 3,  94, 0 },
   1045  1.34.14.1      phil 	[IEEE80211_MODE_HALF]	= { 2, 3, 4,  94, 0 },
   1046  1.34.14.1      phil 	[IEEE80211_MODE_QUARTER]= { 2, 3, 4,  94, 0 },
   1047  1.34.14.1      phil 	[IEEE80211_MODE_11NA]	= { 2, 3, 4,  94, 0 },
   1048  1.34.14.1      phil 	[IEEE80211_MODE_11NG]	= { 2, 3, 4,  94, 0 },
   1049       1.19    dyoung };
   1050       1.19    dyoung static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
   1051  1.34.14.1      phil 	[IEEE80211_MODE_AUTO]	= { 2, 2, 3,  47, 0 },
   1052  1.34.14.1      phil 	[IEEE80211_MODE_11A]	= { 2, 2, 3,  47, 0 },
   1053  1.34.14.1      phil 	[IEEE80211_MODE_11B]	= { 2, 2, 3, 102, 0 },
   1054  1.34.14.1      phil 	[IEEE80211_MODE_11G]	= { 2, 2, 3,  47, 0 },
   1055  1.34.14.1      phil 	[IEEE80211_MODE_FH]	= { 2, 2, 3, 102, 0 },
   1056  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_A]= { 1, 2, 2,  47, 0 },
   1057  1.34.14.1      phil 	[IEEE80211_MODE_TURBO_G]= { 1, 2, 2,  47, 0 },
   1058  1.34.14.1      phil 	[IEEE80211_MODE_STURBO_A]={ 1, 2, 2,  47, 0 },
   1059  1.34.14.1      phil 	[IEEE80211_MODE_HALF]	= { 2, 2, 3,  47, 0 },
   1060  1.34.14.1      phil 	[IEEE80211_MODE_QUARTER]= { 2, 2, 3,  47, 0 },
   1061  1.34.14.1      phil 	[IEEE80211_MODE_11NA]	= { 2, 2, 3,  47, 0 },
   1062  1.34.14.1      phil 	[IEEE80211_MODE_11NG]	= { 2, 2, 3,  47, 0 },
   1063       1.19    dyoung };
   1064       1.19    dyoung 
   1065  1.34.14.1      phil static void
   1066  1.34.14.1      phil _setifsparams(struct wmeParams *wmep, const paramType *phy)
   1067  1.34.14.1      phil {
   1068  1.34.14.1      phil 	wmep->wmep_aifsn = phy->aifsn;
   1069  1.34.14.1      phil 	wmep->wmep_logcwmin = phy->logcwmin;
   1070  1.34.14.1      phil 	wmep->wmep_logcwmax = phy->logcwmax;
   1071  1.34.14.1      phil 	wmep->wmep_txopLimit = phy->txopLimit;
   1072  1.34.14.1      phil }
   1073  1.34.14.1      phil 
   1074  1.34.14.1      phil static void
   1075  1.34.14.1      phil setwmeparams(struct ieee80211vap *vap, const char *type, int ac,
   1076  1.34.14.1      phil 	struct wmeParams *wmep, const paramType *phy)
   1077       1.19    dyoung {
   1078  1.34.14.1      phil 	wmep->wmep_acm = phy->acm;
   1079  1.34.14.1      phil 	_setifsparams(wmep, phy);
   1080  1.34.14.1      phil 
   1081  1.34.14.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1082  1.34.14.1      phil 	    "set %s (%s) [acm %u aifsn %u logcwmin %u logcwmax %u txop %u]\n",
   1083  1.34.14.1      phil 	    ieee80211_wme_acnames[ac], type,
   1084  1.34.14.1      phil 	    wmep->wmep_acm, wmep->wmep_aifsn, wmep->wmep_logcwmin,
   1085  1.34.14.1      phil 	    wmep->wmep_logcwmax, wmep->wmep_txopLimit);
   1086  1.34.14.1      phil }
   1087  1.34.14.1      phil 
   1088  1.34.14.1      phil static void
   1089  1.34.14.1      phil ieee80211_wme_initparams_locked(struct ieee80211vap *vap)
   1090  1.34.14.1      phil {
   1091  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1092       1.19    dyoung 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1093       1.19    dyoung 	const paramType *pPhyParam, *pBssPhyParam;
   1094       1.19    dyoung 	struct wmeParams *wmep;
   1095  1.34.14.1      phil 	enum ieee80211_phymode mode;
   1096       1.19    dyoung 	int i;
   1097       1.19    dyoung 
   1098  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1099  1.34.14.1      phil 
   1100  1.34.14.1      phil 	if ((ic->ic_caps & IEEE80211_C_WME) == 0 || ic->ic_nrunning > 1)
   1101       1.19    dyoung 		return;
   1102       1.19    dyoung 
   1103  1.34.14.1      phil 	/*
   1104  1.34.14.1      phil 	 * Clear the wme cap_info field so a qoscount from a previous
   1105  1.34.14.1      phil 	 * vap doesn't confuse later code which only parses the beacon
   1106  1.34.14.1      phil 	 * field and updates hardware when said field changes.
   1107  1.34.14.1      phil 	 * Otherwise the hardware is programmed with defaults, not what
   1108  1.34.14.1      phil 	 * the beacon actually announces.
   1109  1.34.14.1      phil 	 */
   1110  1.34.14.1      phil 	wme->wme_wmeChanParams.cap_info = 0;
   1111  1.34.14.1      phil 
   1112  1.34.14.1      phil 	/*
   1113  1.34.14.1      phil 	 * Select mode; we can be called early in which case we
   1114  1.34.14.1      phil 	 * always use auto mode.  We know we'll be called when
   1115  1.34.14.1      phil 	 * entering the RUN state with bsschan setup properly
   1116  1.34.14.1      phil 	 * so state will eventually get set correctly
   1117  1.34.14.1      phil 	 */
   1118  1.34.14.1      phil 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
   1119  1.34.14.1      phil 		mode = ieee80211_chan2mode(ic->ic_bsschan);
   1120  1.34.14.1      phil 	else
   1121  1.34.14.1      phil 		mode = IEEE80211_MODE_AUTO;
   1122       1.19    dyoung 	for (i = 0; i < WME_NUM_AC; i++) {
   1123       1.19    dyoung 		switch (i) {
   1124       1.19    dyoung 		case WME_AC_BK:
   1125  1.34.14.1      phil 			pPhyParam = &phyParamForAC_BK[mode];
   1126  1.34.14.1      phil 			pBssPhyParam = &phyParamForAC_BK[mode];
   1127       1.19    dyoung 			break;
   1128       1.19    dyoung 		case WME_AC_VI:
   1129  1.34.14.1      phil 			pPhyParam = &phyParamForAC_VI[mode];
   1130  1.34.14.1      phil 			pBssPhyParam = &bssPhyParamForAC_VI[mode];
   1131       1.19    dyoung 			break;
   1132       1.19    dyoung 		case WME_AC_VO:
   1133  1.34.14.1      phil 			pPhyParam = &phyParamForAC_VO[mode];
   1134  1.34.14.1      phil 			pBssPhyParam = &bssPhyParamForAC_VO[mode];
   1135       1.19    dyoung 			break;
   1136       1.19    dyoung 		case WME_AC_BE:
   1137       1.19    dyoung 		default:
   1138  1.34.14.1      phil 			pPhyParam = &phyParamForAC_BE[mode];
   1139  1.34.14.1      phil 			pBssPhyParam = &bssPhyParamForAC_BE[mode];
   1140       1.19    dyoung 			break;
   1141       1.19    dyoung 		}
   1142       1.19    dyoung 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
   1143       1.19    dyoung 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
   1144  1.34.14.1      phil 			setwmeparams(vap, "chan", i, wmep, pPhyParam);
   1145       1.19    dyoung 		} else {
   1146  1.34.14.1      phil 			setwmeparams(vap, "chan", i, wmep, pBssPhyParam);
   1147       1.19    dyoung 		}
   1148       1.19    dyoung 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
   1149  1.34.14.1      phil 		setwmeparams(vap, "bss ", i, wmep, pBssPhyParam);
   1150       1.19    dyoung 	}
   1151       1.19    dyoung 	/* NB: check ic_bss to avoid NULL deref on initial attach */
   1152  1.34.14.1      phil 	if (vap->iv_bss != NULL) {
   1153       1.19    dyoung 		/*
   1154  1.34.14.1      phil 		 * Calculate aggressive mode switching threshold based
   1155       1.19    dyoung 		 * on beacon interval.  This doesn't need locking since
   1156       1.19    dyoung 		 * we're only called before entering the RUN state at
   1157       1.19    dyoung 		 * which point we start sending beacon frames.
   1158       1.19    dyoung 		 */
   1159       1.19    dyoung 		wme->wme_hipri_switch_thresh =
   1160  1.34.14.1      phil 			(HIGH_PRI_SWITCH_THRESH * vap->iv_bss->ni_intval) / 100;
   1161  1.34.14.1      phil 		wme->wme_flags &= ~WME_F_AGGRMODE;
   1162  1.34.14.6      phil 		ieee80211_wme_updateparams_locked(vap); // BUG ???
   1163       1.19    dyoung 	}
   1164       1.19    dyoung }
   1165       1.19    dyoung 
   1166  1.34.14.1      phil void
   1167  1.34.14.1      phil ieee80211_wme_initparams(struct ieee80211vap *vap)
   1168  1.34.14.1      phil {
   1169  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1170  1.34.14.1      phil 
   1171  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1172  1.34.14.1      phil 	ieee80211_wme_initparams_locked(vap);
   1173  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1174  1.34.14.1      phil }
   1175  1.34.14.1      phil 
   1176       1.19    dyoung /*
   1177       1.19    dyoung  * Update WME parameters for ourself and the BSS.
   1178       1.19    dyoung  */
   1179       1.19    dyoung void
   1180  1.34.14.1      phil ieee80211_wme_updateparams_locked(struct ieee80211vap *vap)
   1181       1.19    dyoung {
   1182  1.34.14.1      phil 	static const paramType aggrParam[IEEE80211_MODE_MAX] = {
   1183  1.34.14.1      phil 	    [IEEE80211_MODE_AUTO]	= { 2, 4, 10, 64, 0 },
   1184  1.34.14.1      phil 	    [IEEE80211_MODE_11A]	= { 2, 4, 10, 64, 0 },
   1185  1.34.14.1      phil 	    [IEEE80211_MODE_11B]	= { 2, 5, 10, 64, 0 },
   1186  1.34.14.1      phil 	    [IEEE80211_MODE_11G]	= { 2, 4, 10, 64, 0 },
   1187  1.34.14.1      phil 	    [IEEE80211_MODE_FH]		= { 2, 5, 10, 64, 0 },
   1188  1.34.14.1      phil 	    [IEEE80211_MODE_TURBO_A]	= { 1, 3, 10, 64, 0 },
   1189  1.34.14.1      phil 	    [IEEE80211_MODE_TURBO_G]	= { 1, 3, 10, 64, 0 },
   1190  1.34.14.1      phil 	    [IEEE80211_MODE_STURBO_A]	= { 1, 3, 10, 64, 0 },
   1191  1.34.14.1      phil 	    [IEEE80211_MODE_HALF]	= { 2, 4, 10, 64, 0 },
   1192  1.34.14.1      phil 	    [IEEE80211_MODE_QUARTER]	= { 2, 4, 10, 64, 0 },
   1193  1.34.14.1      phil 	    [IEEE80211_MODE_11NA]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1194  1.34.14.1      phil 	    [IEEE80211_MODE_11NG]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1195  1.34.14.1      phil 	    [IEEE80211_MODE_VHT_2GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1196  1.34.14.1      phil 	    [IEEE80211_MODE_VHT_5GHZ]	= { 2, 4, 10, 64, 0 },	/* XXXcheck*/
   1197       1.19    dyoung 	};
   1198  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1199       1.19    dyoung 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1200       1.19    dyoung 	const struct wmeParams *wmep;
   1201       1.19    dyoung 	struct wmeParams *chanp, *bssp;
   1202  1.34.14.1      phil 	enum ieee80211_phymode mode;
   1203       1.19    dyoung 	int i;
   1204  1.34.14.1      phil 	int do_aggrmode = 0;
   1205       1.19    dyoung 
   1206  1.34.14.1      phil        	/*
   1207  1.34.14.1      phil 	 * Set up the channel access parameters for the physical
   1208  1.34.14.1      phil 	 * device.  First populate the configured settings.
   1209  1.34.14.1      phil 	 */
   1210       1.19    dyoung 	for (i = 0; i < WME_NUM_AC; i++) {
   1211       1.19    dyoung 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
   1212       1.19    dyoung 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
   1213       1.19    dyoung 		chanp->wmep_aifsn = wmep->wmep_aifsn;
   1214       1.19    dyoung 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
   1215       1.19    dyoung 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
   1216       1.19    dyoung 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
   1217       1.19    dyoung 
   1218       1.19    dyoung 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
   1219       1.19    dyoung 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
   1220       1.19    dyoung 		chanp->wmep_aifsn = wmep->wmep_aifsn;
   1221       1.19    dyoung 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
   1222       1.19    dyoung 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
   1223       1.19    dyoung 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
   1224       1.19    dyoung 	}
   1225       1.19    dyoung 
   1226       1.19    dyoung 	/*
   1227  1.34.14.1      phil 	 * Select mode; we can be called early in which case we
   1228  1.34.14.1      phil 	 * always use auto mode.  We know we'll be called when
   1229  1.34.14.1      phil 	 * entering the RUN state with bsschan setup properly
   1230  1.34.14.1      phil 	 * so state will eventually get set correctly
   1231  1.34.14.1      phil 	 */
   1232  1.34.14.1      phil 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
   1233  1.34.14.1      phil 		mode = ieee80211_chan2mode(ic->ic_bsschan);
   1234  1.34.14.1      phil 	else
   1235  1.34.14.1      phil 		mode = IEEE80211_MODE_AUTO;
   1236  1.34.14.1      phil 
   1237  1.34.14.1      phil 	/*
   1238  1.34.14.1      phil 	 * This implements aggressive mode as found in certain
   1239       1.19    dyoung 	 * vendors' AP's.  When there is significant high
   1240       1.19    dyoung 	 * priority (VI/VO) traffic in the BSS throttle back BE
   1241       1.19    dyoung 	 * traffic by using conservative parameters.  Otherwise
   1242  1.34.14.1      phil 	 * BE uses aggressive params to optimize performance of
   1243       1.19    dyoung 	 * legacy/non-QoS traffic.
   1244       1.19    dyoung 	 */
   1245  1.34.14.1      phil 
   1246  1.34.14.1      phil 	/* Hostap? Only if aggressive mode is enabled */
   1247  1.34.14.1      phil         if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   1248  1.34.14.1      phil 	     (wme->wme_flags & WME_F_AGGRMODE) != 0)
   1249  1.34.14.1      phil 		do_aggrmode = 1;
   1250  1.34.14.1      phil 
   1251  1.34.14.1      phil 	/*
   1252  1.34.14.1      phil 	 * Station? Only if we're in a non-QoS BSS.
   1253  1.34.14.1      phil 	 */
   1254  1.34.14.1      phil 	else if ((vap->iv_opmode == IEEE80211_M_STA &&
   1255  1.34.14.1      phil 	     (vap->iv_bss->ni_flags & IEEE80211_NODE_QOS) == 0))
   1256  1.34.14.1      phil 		do_aggrmode = 1;
   1257  1.34.14.1      phil 
   1258  1.34.14.1      phil 	/*
   1259  1.34.14.1      phil 	 * IBSS? Only if we we have WME enabled.
   1260  1.34.14.1      phil 	 */
   1261  1.34.14.1      phil 	else if ((vap->iv_opmode == IEEE80211_M_IBSS) &&
   1262  1.34.14.1      phil 	    (vap->iv_flags & IEEE80211_F_WME))
   1263  1.34.14.1      phil 		do_aggrmode = 1;
   1264  1.34.14.1      phil 
   1265  1.34.14.1      phil 	/*
   1266  1.34.14.1      phil 	 * If WME is disabled on this VAP, default to aggressive mode
   1267  1.34.14.1      phil 	 * regardless of the configuration.
   1268  1.34.14.1      phil 	 */
   1269  1.34.14.1      phil 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
   1270  1.34.14.1      phil 		do_aggrmode = 1;
   1271  1.34.14.1      phil 
   1272  1.34.14.1      phil 	/* XXX WDS? */
   1273  1.34.14.1      phil 
   1274  1.34.14.1      phil 	/* XXX MBSS? */
   1275  1.34.14.1      phil 
   1276  1.34.14.1      phil 	if (do_aggrmode) {
   1277       1.19    dyoung 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
   1278       1.19    dyoung 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
   1279       1.19    dyoung 
   1280  1.34.14.1      phil 		chanp->wmep_aifsn = bssp->wmep_aifsn = aggrParam[mode].aifsn;
   1281       1.19    dyoung 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
   1282  1.34.14.1      phil 		    aggrParam[mode].logcwmin;
   1283       1.19    dyoung 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
   1284  1.34.14.1      phil 		    aggrParam[mode].logcwmax;
   1285       1.19    dyoung 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
   1286  1.34.14.1      phil 		    (vap->iv_flags & IEEE80211_F_BURST) ?
   1287  1.34.14.1      phil 			aggrParam[mode].txopLimit : 0;
   1288  1.34.14.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1289  1.34.14.1      phil 		    "update %s (chan+bss) [acm %u aifsn %u logcwmin %u "
   1290  1.34.14.1      phil 		    "logcwmax %u txop %u]\n", ieee80211_wme_acnames[WME_AC_BE],
   1291  1.34.14.1      phil 		    chanp->wmep_acm, chanp->wmep_aifsn, chanp->wmep_logcwmin,
   1292  1.34.14.1      phil 		    chanp->wmep_logcwmax, chanp->wmep_txopLimit);
   1293       1.19    dyoung 	}
   1294  1.34.14.1      phil 
   1295  1.34.14.1      phil 
   1296  1.34.14.1      phil 	/*
   1297  1.34.14.1      phil 	 * Change the contention window based on the number of associated
   1298  1.34.14.1      phil 	 * stations.  If the number of associated stations is 1 and
   1299  1.34.14.1      phil 	 * aggressive mode is enabled, lower the contention window even
   1300  1.34.14.1      phil 	 * further.
   1301  1.34.14.1      phil 	 */
   1302  1.34.14.1      phil 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   1303  1.34.14.1      phil 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) != 0) {
   1304  1.34.14.1      phil 		static const uint8_t logCwMin[IEEE80211_MODE_MAX] = {
   1305  1.34.14.1      phil 		    [IEEE80211_MODE_AUTO]	= 3,
   1306  1.34.14.1      phil 		    [IEEE80211_MODE_11A]	= 3,
   1307  1.34.14.1      phil 		    [IEEE80211_MODE_11B]	= 4,
   1308  1.34.14.1      phil 		    [IEEE80211_MODE_11G]	= 3,
   1309  1.34.14.1      phil 		    [IEEE80211_MODE_FH]		= 4,
   1310  1.34.14.1      phil 		    [IEEE80211_MODE_TURBO_A]	= 3,
   1311  1.34.14.1      phil 		    [IEEE80211_MODE_TURBO_G]	= 3,
   1312  1.34.14.1      phil 		    [IEEE80211_MODE_STURBO_A]	= 3,
   1313  1.34.14.1      phil 		    [IEEE80211_MODE_HALF]	= 3,
   1314  1.34.14.1      phil 		    [IEEE80211_MODE_QUARTER]	= 3,
   1315  1.34.14.1      phil 		    [IEEE80211_MODE_11NA]	= 3,
   1316  1.34.14.1      phil 		    [IEEE80211_MODE_11NG]	= 3,
   1317  1.34.14.1      phil 		    [IEEE80211_MODE_VHT_2GHZ]	= 3,
   1318  1.34.14.1      phil 		    [IEEE80211_MODE_VHT_5GHZ]	= 3,
   1319       1.19    dyoung 		};
   1320       1.19    dyoung 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
   1321       1.19    dyoung 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
   1322       1.19    dyoung 
   1323  1.34.14.1      phil 		chanp->wmep_logcwmin = bssp->wmep_logcwmin = logCwMin[mode];
   1324  1.34.14.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1325  1.34.14.1      phil 		    "update %s (chan+bss) logcwmin %u\n",
   1326  1.34.14.1      phil 		    ieee80211_wme_acnames[WME_AC_BE], chanp->wmep_logcwmin);
   1327  1.34.14.1      phil 	}
   1328  1.34.14.1      phil 
   1329  1.34.14.1      phil 	/*
   1330  1.34.14.1      phil 	 * Arrange for the beacon update.
   1331  1.34.14.1      phil 	 *
   1332  1.34.14.1      phil 	 * XXX what about MBSS, WDS?
   1333  1.34.14.1      phil 	 */
   1334  1.34.14.1      phil 	if (vap->iv_opmode == IEEE80211_M_HOSTAP
   1335  1.34.14.1      phil 	    || vap->iv_opmode == IEEE80211_M_IBSS) {
   1336       1.19    dyoung 		/*
   1337       1.19    dyoung 		 * Arrange for a beacon update and bump the parameter
   1338       1.19    dyoung 		 * set number so associated stations load the new values.
   1339       1.19    dyoung 		 */
   1340       1.19    dyoung 		wme->wme_bssChanParams.cap_info =
   1341       1.19    dyoung 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
   1342  1.34.14.1      phil 		ieee80211_beacon_notify(vap, IEEE80211_BEACON_WME);
   1343       1.19    dyoung 	}
   1344       1.19    dyoung 
   1345  1.34.14.1      phil 	/* schedule the deferred WME update */
   1346  1.34.14.1      phil 	ieee80211_runtask(ic, &vap->iv_wme_task);
   1347       1.19    dyoung 
   1348  1.34.14.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_WME,
   1349  1.34.14.1      phil 	    "%s: WME params updated, cap_info 0x%x\n", __func__,
   1350  1.34.14.1      phil 	    vap->iv_opmode == IEEE80211_M_STA ?
   1351  1.34.14.1      phil 		wme->wme_wmeChanParams.cap_info :
   1352  1.34.14.1      phil 		wme->wme_bssChanParams.cap_info);
   1353       1.19    dyoung }
   1354       1.19    dyoung 
   1355       1.19    dyoung void
   1356  1.34.14.1      phil ieee80211_wme_updateparams(struct ieee80211vap *vap)
   1357       1.19    dyoung {
   1358  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1359       1.19    dyoung 
   1360       1.19    dyoung 	if (ic->ic_caps & IEEE80211_C_WME) {
   1361  1.34.14.1      phil 		IEEE80211_LOCK(ic);
   1362  1.34.14.1      phil 		ieee80211_wme_updateparams_locked(vap);
   1363  1.34.14.1      phil 		IEEE80211_UNLOCK(ic);
   1364       1.19    dyoung 	}
   1365       1.19    dyoung }
   1366       1.19    dyoung 
   1367  1.34.14.1      phil /*
   1368  1.34.14.1      phil  * Fetch the WME parameters for the given VAP.
   1369  1.34.14.1      phil  *
   1370  1.34.14.1      phil  * When net80211 grows p2p, etc support, this may return different
   1371  1.34.14.1      phil  * parameters for each VAP.
   1372  1.34.14.1      phil  */
   1373  1.34.14.1      phil void
   1374  1.34.14.1      phil ieee80211_wme_vap_getparams(struct ieee80211vap *vap, struct chanAccParams *wp)
   1375  1.34.14.1      phil {
   1376  1.34.14.1      phil 
   1377  1.34.14.1      phil 	memcpy(wp, &vap->iv_ic->ic_wme.wme_chanParams, sizeof(*wp));
   1378  1.34.14.1      phil }
   1379  1.34.14.1      phil 
   1380  1.34.14.1      phil /*
   1381  1.34.14.1      phil  * For NICs which only support one set of WME paramaters (ie, softmac NICs)
   1382  1.34.14.1      phil  * there may be different VAP WME parameters but only one is "active".
   1383  1.34.14.1      phil  * This returns the "NIC" WME parameters for the currently active
   1384  1.34.14.1      phil  * context.
   1385  1.34.14.1      phil  */
   1386  1.34.14.1      phil void
   1387  1.34.14.1      phil ieee80211_wme_ic_getparams(struct ieee80211com *ic, struct chanAccParams *wp)
   1388  1.34.14.1      phil {
   1389  1.34.14.1      phil 
   1390  1.34.14.1      phil 	memcpy(wp, &ic->ic_wme.wme_chanParams, sizeof(*wp));
   1391  1.34.14.1      phil }
   1392  1.34.14.1      phil 
   1393  1.34.14.1      phil /*
   1394  1.34.14.1      phil  * Return whether to use QoS on a given WME queue.
   1395  1.34.14.1      phil  *
   1396  1.34.14.1      phil  * This is intended to be called from the transmit path of softmac drivers
   1397  1.34.14.1      phil  * which are setting NoAck bits in transmit descriptors.
   1398  1.34.14.1      phil  *
   1399  1.34.14.1      phil  * Ideally this would be set in some transmit field before the packet is
   1400  1.34.14.1      phil  * queued to the driver but net80211 isn't quite there yet.
   1401  1.34.14.1      phil  */
   1402  1.34.14.1      phil int
   1403  1.34.14.1      phil ieee80211_wme_vap_ac_is_noack(struct ieee80211vap *vap, int ac)
   1404  1.34.14.1      phil {
   1405  1.34.14.1      phil 	/* Bounds/sanity check */
   1406  1.34.14.1      phil 	if (ac < 0 || ac >= WME_NUM_AC)
   1407  1.34.14.1      phil 		return (0);
   1408  1.34.14.1      phil 
   1409  1.34.14.1      phil 	/* Again, there's only one global context for now */
   1410  1.34.14.1      phil 	return (!! vap->iv_ic->ic_wme.wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy);
   1411  1.34.14.1      phil }
   1412  1.34.14.1      phil 
   1413       1.21    dyoung static void
   1414  1.34.14.1      phil parent_updown(void *arg, int npending)
   1415       1.21    dyoung {
   1416       1.21    dyoung 	struct ieee80211com *ic = arg;
   1417       1.21    dyoung 
   1418  1.34.14.5      phil 	printf ("parent_updown called on %s!\n", ic->ic_name);
   1419  1.34.14.5      phil 	// ic->ic_parent(ic);
   1420       1.21    dyoung }
   1421       1.24    dyoung 
   1422  1.34.14.1      phil static void
   1423  1.34.14.1      phil update_mcast(void *arg, int npending)
   1424       1.24    dyoung {
   1425  1.34.14.1      phil 	struct ieee80211com *ic = arg;
   1426       1.24    dyoung 
   1427  1.34.14.1      phil 	ic->ic_update_mcast(ic);
   1428  1.34.14.1      phil }
   1429  1.34.14.1      phil 
   1430  1.34.14.1      phil static void
   1431  1.34.14.1      phil update_promisc(void *arg, int npending)
   1432  1.34.14.1      phil {
   1433  1.34.14.1      phil 	struct ieee80211com *ic = arg;
   1434  1.34.14.1      phil 
   1435  1.34.14.1      phil 	ic->ic_update_promisc(ic);
   1436  1.34.14.1      phil }
   1437  1.34.14.1      phil 
   1438  1.34.14.1      phil static void
   1439  1.34.14.1      phil update_channel(void *arg, int npending)
   1440  1.34.14.1      phil {
   1441  1.34.14.1      phil 	struct ieee80211com *ic = arg;
   1442  1.34.14.1      phil 
   1443  1.34.14.1      phil 	ic->ic_set_channel(ic);
   1444  1.34.14.1      phil 	ieee80211_radiotap_chan_change(ic);
   1445  1.34.14.1      phil }
   1446  1.34.14.1      phil 
   1447  1.34.14.1      phil static void
   1448  1.34.14.1      phil update_chw(void *arg, int npending)
   1449  1.34.14.1      phil {
   1450  1.34.14.1      phil 	struct ieee80211com *ic = arg;
   1451       1.24    dyoung 
   1452       1.24    dyoung 	/*
   1453  1.34.14.1      phil 	 * XXX should we defer the channel width _config_ update until now?
   1454       1.24    dyoung 	 */
   1455  1.34.14.1      phil 	ic->ic_update_chw(ic);
   1456  1.34.14.1      phil }
   1457       1.21    dyoung 
   1458  1.34.14.1      phil /*
   1459  1.34.14.1      phil  * Deferred WME update.
   1460  1.34.14.1      phil  *
   1461  1.34.14.1      phil  * In preparation for per-VAP WME configuration, call the VAP
   1462  1.34.14.1      phil  * method if the VAP requires it.  Otherwise, just call the
   1463  1.34.14.1      phil  * older global method.  There isn't a per-VAP WME configuration
   1464  1.34.14.1      phil  * just yet so for now just use the global configuration.
   1465  1.34.14.1      phil  */
   1466  1.34.14.1      phil static void
   1467  1.34.14.1      phil vap_update_wme(void *arg, int npending)
   1468  1.34.14.1      phil {
   1469  1.34.14.1      phil 	struct ieee80211vap *vap = arg;
   1470  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1471  1.34.14.1      phil 
   1472  1.34.14.1      phil 	if (vap->iv_wme_update != NULL)
   1473  1.34.14.1      phil 		vap->iv_wme_update(vap,
   1474  1.34.14.1      phil 		    ic->ic_wme.wme_chanParams.cap_wmeParams);
   1475  1.34.14.1      phil 	else
   1476  1.34.14.1      phil 		ic->ic_wme.wme_update(ic);
   1477       1.24    dyoung }
   1478       1.24    dyoung 
   1479       1.21    dyoung static void
   1480  1.34.14.1      phil restart_vaps(void *arg, int npending)
   1481       1.21    dyoung {
   1482       1.21    dyoung 	struct ieee80211com *ic = arg;
   1483       1.21    dyoung 
   1484  1.34.14.1      phil 	ieee80211_suspend_all(ic);
   1485  1.34.14.1      phil 	ieee80211_resume_all(ic);
   1486  1.34.14.1      phil }
   1487  1.34.14.1      phil 
   1488  1.34.14.1      phil /*
   1489  1.34.14.1      phil  * Block until the parent is in a known state.  This is
   1490  1.34.14.1      phil  * used after any operations that dispatch a task (e.g.
   1491  1.34.14.1      phil  * to auto-configure the parent device up/down).
   1492  1.34.14.1      phil  */
   1493  1.34.14.1      phil void
   1494  1.34.14.1      phil ieee80211_waitfor_parent(struct ieee80211com *ic)
   1495  1.34.14.1      phil {
   1496  1.34.14.1      phil 	taskqueue_block(ic->ic_tq);
   1497  1.34.14.1      phil 	ieee80211_draintask(ic, &ic->ic_parent_task);
   1498  1.34.14.1      phil 	ieee80211_draintask(ic, &ic->ic_mcast_task);
   1499  1.34.14.1      phil 	ieee80211_draintask(ic, &ic->ic_promisc_task);
   1500  1.34.14.1      phil 	ieee80211_draintask(ic, &ic->ic_chan_task);
   1501  1.34.14.1      phil 	ieee80211_draintask(ic, &ic->ic_bmiss_task);
   1502  1.34.14.1      phil 	ieee80211_draintask(ic, &ic->ic_chw_task);
   1503  1.34.14.1      phil 	taskqueue_unblock(ic->ic_tq);
   1504       1.21    dyoung }
   1505       1.21    dyoung 
   1506  1.34.14.1      phil /*
   1507  1.34.14.1      phil  * Check to see whether the current channel needs reset.
   1508  1.34.14.1      phil  *
   1509  1.34.14.1      phil  * Some devices don't handle being given an invalid channel
   1510  1.34.14.1      phil  * in their operating mode very well (eg wpi(4) will throw a
   1511  1.34.14.1      phil  * firmware exception.)
   1512  1.34.14.1      phil  *
   1513  1.34.14.1      phil  * Return 0 if we're ok, 1 if the channel needs to be reset.
   1514  1.34.14.1      phil  *
   1515  1.34.14.1      phil  * See PR kern/202502.
   1516  1.34.14.1      phil  */
   1517        1.1    dyoung static int
   1518  1.34.14.1      phil ieee80211_start_check_reset_chan(struct ieee80211vap *vap)
   1519        1.1    dyoung {
   1520  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1521        1.1    dyoung 
   1522  1.34.14.1      phil 	if ((vap->iv_opmode == IEEE80211_M_IBSS &&
   1523  1.34.14.1      phil 	     IEEE80211_IS_CHAN_NOADHOC(ic->ic_curchan)) ||
   1524  1.34.14.1      phil 	    (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   1525  1.34.14.1      phil 	     IEEE80211_IS_CHAN_NOHOSTAP(ic->ic_curchan)))
   1526  1.34.14.1      phil 		return (1);
   1527  1.34.14.1      phil 	return (0);
   1528  1.34.14.1      phil }
   1529       1.34    nonaka 
   1530  1.34.14.1      phil /*
   1531  1.34.14.1      phil  * Reset the curchan to a known good state.
   1532  1.34.14.1      phil  */
   1533  1.34.14.1      phil static void
   1534  1.34.14.1      phil ieee80211_start_reset_chan(struct ieee80211vap *vap)
   1535  1.34.14.1      phil {
   1536  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1537  1.34.14.1      phil 
   1538  1.34.14.1      phil 	ic->ic_curchan = &ic->ic_channels[0];
   1539  1.34.14.1      phil }
   1540  1.34.14.1      phil 
   1541  1.34.14.1      phil /*
   1542  1.34.14.1      phil  * Start a vap running.  If this is the first vap to be
   1543  1.34.14.1      phil  * set running on the underlying device then we
   1544  1.34.14.1      phil  * automatically bring the device up.
   1545  1.34.14.1      phil  */
   1546  1.34.14.1      phil void
   1547  1.34.14.1      phil ieee80211_start_locked(struct ieee80211vap *vap)
   1548  1.34.14.1      phil {
   1549  1.34.14.7      phil #if __FreeBSD__
   1550  1.34.14.1      phil 	struct ifnet *ifp = vap->iv_ifp;
   1551  1.34.14.7      phil #endif
   1552  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1553  1.34.14.1      phil 
   1554  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1555  1.34.14.1      phil 
   1556  1.34.14.1      phil 	IEEE80211_DPRINTF(vap,
   1557  1.34.14.1      phil 		IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1558  1.34.14.1      phil 		"start running, %d vaps running\n", ic->ic_nrunning);
   1559  1.34.14.7      phil #if __NetBSD__
   1560  1.34.14.7      phil 	/* NNN may need to change/upgrade this once more than one vap/device. */
   1561  1.34.14.7      phil 	if (ic->ic_nrunning++ == 0) {
   1562  1.34.14.7      phil 		/* reset the channel to a known good channel */
   1563  1.34.14.7      phil 		if (ieee80211_start_check_reset_chan(vap))
   1564  1.34.14.7      phil 			ieee80211_start_reset_chan(vap);
   1565  1.34.14.7      phil 	}
   1566  1.34.14.7      phil #endif
   1567  1.34.14.4      phil 
   1568  1.34.14.2      phil #if __FreeBSD__
   1569  1.34.14.1      phil 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
   1570  1.34.14.1      phil 		/*
   1571  1.34.14.1      phil 		 * Mark us running.  Note that it's ok to do this first;
   1572  1.34.14.1      phil 		 * if we need to bring the parent device up we defer that
   1573  1.34.14.1      phil 		 * to avoid dropping the com lock.  We expect the device
   1574  1.34.14.1      phil 		 * to respond to being marked up by calling back into us
   1575  1.34.14.1      phil 		 * through ieee80211_start_all at which point we'll come
   1576  1.34.14.1      phil 		 * back in here and complete the work.
   1577  1.34.14.1      phil 		 */
   1578  1.34.14.7      phil 
   1579  1.34.14.1      phil 		ifp->if_drv_flags |= IFF_DRV_RUNNING;
   1580  1.34.14.7      phil 
   1581  1.34.14.1      phil 		/*
   1582  1.34.14.1      phil 		 * We are not running; if this we are the first vap
   1583  1.34.14.1      phil 		 * to be brought up auto-up the parent if necessary.
   1584  1.34.14.1      phil 		 */
   1585  1.34.14.1      phil 		if (ic->ic_nrunning++ == 0) {
   1586  1.34.14.1      phil 			/* reset the channel to a known good channel */
   1587  1.34.14.1      phil 			if (ieee80211_start_check_reset_chan(vap))
   1588  1.34.14.1      phil 				ieee80211_start_reset_chan(vap);
   1589  1.34.14.1      phil 
   1590  1.34.14.1      phil 			IEEE80211_DPRINTF(vap,
   1591  1.34.14.1      phil 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1592  1.34.14.1      phil 			    "%s: up parent %s\n", __func__, ic->ic_name);
   1593  1.34.14.1      phil 			ieee80211_runtask(ic, &ic->ic_parent_task);
   1594  1.34.14.1      phil 			return;
   1595        1.1    dyoung 		}
   1596  1.34.14.1      phil 	}
   1597  1.34.14.1      phil 	/*
   1598  1.34.14.1      phil 	 * If the parent is up and running, then kick the
   1599  1.34.14.1      phil 	 * 802.11 state machine as appropriate.
   1600  1.34.14.1      phil 	 */
   1601  1.34.14.1      phil 	if (vap->iv_roaming != IEEE80211_ROAMING_MANUAL) {
   1602  1.34.14.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA) {
   1603  1.34.14.1      phil #if 0
   1604  1.34.14.1      phil 			/* XXX bypasses scan too easily; disable for now */
   1605  1.34.14.1      phil 			/*
   1606  1.34.14.1      phil 			 * Try to be intelligent about clocking the state
   1607  1.34.14.1      phil 			 * machine.  If we're currently in RUN state then
   1608  1.34.14.1      phil 			 * we should be able to apply any new state/parameters
   1609  1.34.14.1      phil 			 * simply by re-associating.  Otherwise we need to
   1610  1.34.14.1      phil 			 * re-scan to select an appropriate ap.
   1611  1.34.14.1      phil 			 */
   1612  1.34.14.1      phil 			if (vap->iv_state >= IEEE80211_S_RUN)
   1613  1.34.14.1      phil 				ieee80211_new_state_locked(vap,
   1614  1.34.14.1      phil 				    IEEE80211_S_ASSOC, 1);
   1615  1.34.14.1      phil 			else
   1616  1.34.14.1      phil #endif
   1617  1.34.14.1      phil 				ieee80211_new_state_locked(vap,
   1618  1.34.14.1      phil 				    IEEE80211_S_SCAN, 0);
   1619  1.34.14.1      phil 		} else {
   1620  1.34.14.4      phil 			printf ("   first vap??? \n");
   1621  1.34.14.1      phil 			/*
   1622  1.34.14.1      phil 			 * For monitor+wds mode there's nothing to do but
   1623  1.34.14.1      phil 			 * start running.  Otherwise if this is the first
   1624  1.34.14.1      phil 			 * vap to be brought up, start a scan which may be
   1625  1.34.14.1      phil 			 * preempted if the station is locked to a particular
   1626  1.34.14.1      phil 			 * channel.
   1627  1.34.14.1      phil 			 */
   1628  1.34.14.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_REINIT;
   1629  1.34.14.1      phil 			if (vap->iv_opmode == IEEE80211_M_MONITOR ||
   1630  1.34.14.1      phil 			    vap->iv_opmode == IEEE80211_M_WDS)
   1631  1.34.14.1      phil 				ieee80211_new_state_locked(vap,
   1632  1.34.14.1      phil 				    IEEE80211_S_RUN, -1);
   1633  1.34.14.1      phil 			else
   1634  1.34.14.1      phil 				ieee80211_new_state_locked(vap,
   1635  1.34.14.1      phil 				    IEEE80211_S_SCAN, 0);
   1636        1.1    dyoung 		}
   1637  1.34.14.1      phil 	}
   1638  1.34.14.7      phil #endif
   1639  1.34.14.1      phil }
   1640  1.34.14.1      phil 
   1641  1.34.14.1      phil /*
   1642  1.34.14.1      phil  * Start a single vap.
   1643  1.34.14.1      phil  */
   1644  1.34.14.3      phil #if __FreeBSD__
   1645  1.34.14.1      phil void
   1646  1.34.14.1      phil ieee80211_init(void *arg)
   1647  1.34.14.1      phil {
   1648  1.34.14.1      phil 	struct ieee80211vap *vap = arg;
   1649  1.34.14.1      phil 
   1650  1.34.14.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1651  1.34.14.1      phil 	    "%s\n", __func__);
   1652  1.34.14.1      phil 
   1653  1.34.14.1      phil 	IEEE80211_LOCK(vap->iv_ic);
   1654  1.34.14.1      phil 	ieee80211_start_locked(vap);
   1655  1.34.14.1      phil 	IEEE80211_UNLOCK(vap->iv_ic);
   1656  1.34.14.1      phil }
   1657  1.34.14.3      phil #elif __NetBSD__
   1658  1.34.14.3      phil int
   1659  1.34.14.3      phil ieee80211_init(struct ifnet *ifp)
   1660  1.34.14.3      phil {
   1661  1.34.14.3      phil 	struct ieee80211vap *vap = ifp->if_softc;
   1662  1.34.14.4      phil 	static ONCE_DECL(ieee80211_init_once);
   1663  1.34.14.3      phil 
   1664  1.34.14.3      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1665  1.34.14.3      phil 	    "%s\n", __func__);
   1666  1.34.14.6      phil 	printf ("ieee80211_init called.\n"); // NNN
   1667  1.34.14.3      phil 
   1668  1.34.14.4      phil 	RUN_ONCE(&ieee80211_init_once, ieee80211_init0);
   1669  1.34.14.4      phil 
   1670  1.34.14.3      phil 	IEEE80211_LOCK(vap->iv_ic);
   1671  1.34.14.3      phil 	ieee80211_start_locked(vap);
   1672  1.34.14.3      phil 	IEEE80211_UNLOCK(vap->iv_ic);
   1673  1.34.14.3      phil 	return 0;
   1674  1.34.14.3      phil }
   1675  1.34.14.3      phil #endif
   1676  1.34.14.1      phil 
   1677  1.34.14.1      phil /*
   1678  1.34.14.1      phil  * Start all runnable vap's on a device.
   1679  1.34.14.1      phil  */
   1680  1.34.14.1      phil void
   1681  1.34.14.1      phil ieee80211_start_all(struct ieee80211com *ic)
   1682  1.34.14.1      phil {
   1683  1.34.14.1      phil 	struct ieee80211vap *vap;
   1684  1.34.14.1      phil 
   1685  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1686  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1687  1.34.14.1      phil 		struct ifnet *ifp = vap->iv_ifp;
   1688  1.34.14.1      phil 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
   1689  1.34.14.1      phil 			ieee80211_start_locked(vap);
   1690  1.34.14.1      phil 	}
   1691  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1692  1.34.14.1      phil }
   1693  1.34.14.1      phil 
   1694  1.34.14.1      phil /*
   1695  1.34.14.1      phil  * Stop a vap.  We force it down using the state machine
   1696  1.34.14.1      phil  * then mark it's ifnet not running.  If this is the last
   1697  1.34.14.1      phil  * vap running on the underlying device then we close it
   1698  1.34.14.1      phil  * too to insure it will be properly initialized when the
   1699  1.34.14.1      phil  * next vap is brought up.
   1700  1.34.14.1      phil  */
   1701  1.34.14.1      phil void
   1702  1.34.14.1      phil ieee80211_stop_locked(struct ieee80211vap *vap)
   1703  1.34.14.1      phil {
   1704  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1705  1.34.14.1      phil 	struct ifnet *ifp = vap->iv_ifp;
   1706  1.34.14.1      phil 
   1707  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1708  1.34.14.1      phil 
   1709  1.34.14.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1710  1.34.14.1      phil 	    "stop running, %d vaps running\n", ic->ic_nrunning);
   1711  1.34.14.1      phil 
   1712  1.34.14.1      phil 	ieee80211_new_state_locked(vap, IEEE80211_S_INIT, -1);
   1713  1.34.14.2      phil #if __FreeBSD__
   1714  1.34.14.1      phil 	if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
   1715  1.34.14.1      phil 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;	/* mark us stopped */
   1716  1.34.14.2      phil #elif __NetBSD__
   1717  1.34.14.2      phil 	if (ifp->if_flags & IFF_RUNNING) {
   1718  1.34.14.2      phil 		ifp->if_flags &= ~IFF_RUNNING;	/* mark us stopped */
   1719  1.34.14.2      phil #endif
   1720  1.34.14.1      phil 		if (--ic->ic_nrunning == 0) {
   1721  1.34.14.1      phil 			IEEE80211_DPRINTF(vap,
   1722  1.34.14.1      phil 			    IEEE80211_MSG_STATE | IEEE80211_MSG_DEBUG,
   1723  1.34.14.1      phil 			    "down parent %s\n", ic->ic_name);
   1724  1.34.14.1      phil 			ieee80211_runtask(ic, &ic->ic_parent_task);
   1725        1.1    dyoung 		}
   1726  1.34.14.1      phil 	}
   1727  1.34.14.1      phil }
   1728  1.34.14.1      phil 
   1729  1.34.14.1      phil void
   1730  1.34.14.1      phil ieee80211_stop(struct ieee80211vap *vap)
   1731  1.34.14.1      phil {
   1732  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1733  1.34.14.1      phil 
   1734  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1735  1.34.14.1      phil 	ieee80211_stop_locked(vap);
   1736  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1737  1.34.14.1      phil }
   1738  1.34.14.1      phil 
   1739  1.34.14.1      phil /*
   1740  1.34.14.1      phil  * Stop all vap's running on a device.
   1741  1.34.14.1      phil  */
   1742  1.34.14.1      phil void
   1743  1.34.14.1      phil ieee80211_stop_all(struct ieee80211com *ic)
   1744  1.34.14.1      phil {
   1745  1.34.14.1      phil 	struct ieee80211vap *vap;
   1746  1.34.14.1      phil 
   1747  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1748  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1749  1.34.14.1      phil 		struct ifnet *ifp = vap->iv_ifp;
   1750  1.34.14.1      phil 		if (IFNET_IS_UP_RUNNING(ifp))	/* NB: avoid recursion */
   1751  1.34.14.1      phil 			ieee80211_stop_locked(vap);
   1752  1.34.14.1      phil 	}
   1753  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1754  1.34.14.1      phil 
   1755  1.34.14.1      phil 	ieee80211_waitfor_parent(ic);
   1756  1.34.14.1      phil }
   1757  1.34.14.1      phil 
   1758  1.34.14.1      phil /*
   1759  1.34.14.1      phil  * Stop all vap's running on a device and arrange
   1760  1.34.14.1      phil  * for those that were running to be resumed.
   1761  1.34.14.1      phil  */
   1762  1.34.14.1      phil void
   1763  1.34.14.1      phil ieee80211_suspend_all(struct ieee80211com *ic)
   1764  1.34.14.1      phil {
   1765  1.34.14.1      phil 	struct ieee80211vap *vap;
   1766  1.34.14.1      phil 
   1767  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1768  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1769  1.34.14.1      phil 		struct ifnet *ifp = vap->iv_ifp;
   1770  1.34.14.1      phil 		if (IFNET_IS_UP_RUNNING(ifp)) {	/* NB: avoid recursion */
   1771  1.34.14.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_RESUME;
   1772  1.34.14.1      phil 			ieee80211_stop_locked(vap);
   1773       1.19    dyoung 		}
   1774  1.34.14.1      phil 	}
   1775  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1776  1.34.14.1      phil 
   1777  1.34.14.1      phil 	ieee80211_waitfor_parent(ic);
   1778  1.34.14.1      phil }
   1779  1.34.14.1      phil 
   1780  1.34.14.1      phil /*
   1781  1.34.14.1      phil  * Start all vap's marked for resume.
   1782  1.34.14.1      phil  */
   1783  1.34.14.1      phil void
   1784  1.34.14.1      phil ieee80211_resume_all(struct ieee80211com *ic)
   1785  1.34.14.1      phil {
   1786  1.34.14.1      phil 	struct ieee80211vap *vap;
   1787  1.34.14.1      phil 
   1788  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1789  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1790  1.34.14.1      phil 		struct ifnet *ifp = vap->iv_ifp;
   1791  1.34.14.1      phil 		if (!IFNET_IS_UP_RUNNING(ifp) &&
   1792  1.34.14.1      phil 		    (vap->iv_flags_ext & IEEE80211_FEXT_RESUME)) {
   1793  1.34.14.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_RESUME;
   1794  1.34.14.1      phil 			ieee80211_start_locked(vap);
   1795        1.1    dyoung 		}
   1796  1.34.14.1      phil 	}
   1797  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1798  1.34.14.1      phil }
   1799  1.34.14.1      phil 
   1800  1.34.14.1      phil /*
   1801  1.34.14.1      phil  * Restart all vap's running on a device.
   1802  1.34.14.1      phil  */
   1803  1.34.14.1      phil void
   1804  1.34.14.1      phil ieee80211_restart_all(struct ieee80211com *ic)
   1805  1.34.14.1      phil {
   1806  1.34.14.1      phil 	/*
   1807  1.34.14.1      phil 	 * NB: do not use ieee80211_runtask here, we will
   1808  1.34.14.1      phil 	 * block & drain net80211 taskqueue.
   1809  1.34.14.1      phil 	 */
   1810  1.34.14.5      phil #if __FreeBSD__
   1811  1.34.14.1      phil 	taskqueue_enqueue(taskqueue_thread, &ic->ic_restart_task);
   1812  1.34.14.5      phil #elif __NetBSD__
   1813  1.34.14.5      phil 	printf ("ieee80211_restart_all called .... should add code.\n");
   1814  1.34.14.5      phil #endif
   1815  1.34.14.1      phil }
   1816  1.34.14.1      phil 
   1817  1.34.14.1      phil void
   1818  1.34.14.1      phil ieee80211_beacon_miss(struct ieee80211com *ic)
   1819  1.34.14.1      phil {
   1820  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1821  1.34.14.1      phil 	if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) {
   1822  1.34.14.1      phil 		/* Process in a taskq, the handler may reenter the driver */
   1823  1.34.14.1      phil 		ieee80211_runtask(ic, &ic->ic_bmiss_task);
   1824  1.34.14.1      phil 	}
   1825  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1826  1.34.14.1      phil }
   1827  1.34.14.1      phil 
   1828  1.34.14.1      phil static void
   1829  1.34.14.1      phil beacon_miss(void *arg, int npending)
   1830  1.34.14.1      phil {
   1831  1.34.14.1      phil 	struct ieee80211com *ic = arg;
   1832  1.34.14.1      phil 	struct ieee80211vap *vap;
   1833  1.34.14.1      phil 
   1834  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1835  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1836       1.19    dyoung 		/*
   1837  1.34.14.1      phil 		 * We only pass events through for sta vap's in RUN+ state;
   1838  1.34.14.1      phil 		 * may be too restrictive but for now this saves all the
   1839  1.34.14.1      phil 		 * handlers duplicating these checks.
   1840       1.19    dyoung 		 */
   1841  1.34.14.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA &&
   1842  1.34.14.1      phil 		    vap->iv_state >= IEEE80211_S_RUN &&
   1843  1.34.14.1      phil 		    vap->iv_bmiss != NULL)
   1844  1.34.14.1      phil 			vap->iv_bmiss(vap);
   1845  1.34.14.1      phil 	}
   1846  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1847  1.34.14.1      phil }
   1848  1.34.14.1      phil 
   1849  1.34.14.1      phil static void
   1850  1.34.14.1      phil beacon_swmiss(void *arg, int npending)
   1851  1.34.14.1      phil {
   1852  1.34.14.1      phil 	struct ieee80211vap *vap = arg;
   1853  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1854  1.34.14.1      phil 
   1855  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   1856  1.34.14.1      phil 	if (vap->iv_state >= IEEE80211_S_RUN) {
   1857  1.34.14.1      phil 		/* XXX Call multiple times if npending > zero? */
   1858  1.34.14.1      phil 		vap->iv_bmiss(vap);
   1859  1.34.14.1      phil 	}
   1860  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   1861  1.34.14.1      phil }
   1862  1.34.14.1      phil 
   1863  1.34.14.1      phil /*
   1864  1.34.14.1      phil  * Software beacon miss handling.  Check if any beacons
   1865  1.34.14.1      phil  * were received in the last period.  If not post a
   1866  1.34.14.1      phil  * beacon miss; otherwise reset the counter.
   1867  1.34.14.1      phil  */
   1868  1.34.14.1      phil void
   1869  1.34.14.1      phil ieee80211_swbmiss(void *arg)
   1870  1.34.14.1      phil {
   1871  1.34.14.1      phil 	struct ieee80211vap *vap = arg;
   1872  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1873  1.34.14.1      phil 
   1874  1.34.14.6      phil #if __FreeBSD__
   1875  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1876  1.34.14.6      phil #elif __NetBSD__
   1877  1.34.14.6      phil 	IEEE80211_LOCK(ic);
   1878  1.34.14.7      phil 	if (vap->iv_state < IEEE80211_S_RUN) {  /* NNN should stop it */
   1879  1.34.14.7      phil 		IEEE80211_UNLOCK(ic);
   1880  1.34.14.7      phil 		return;
   1881  1.34.14.7      phil 	}
   1882  1.34.14.6      phil #endif
   1883  1.34.14.1      phil 
   1884  1.34.14.1      phil 	KASSERT(vap->iv_state >= IEEE80211_S_RUN,
   1885  1.34.14.1      phil 	    ("wrong state %d", vap->iv_state));
   1886  1.34.14.1      phil 
   1887  1.34.14.1      phil 	if (ic->ic_flags & IEEE80211_F_SCAN) {
   1888  1.34.14.1      phil 		/*
   1889  1.34.14.1      phil 		 * If scanning just ignore and reset state.  If we get a
   1890  1.34.14.1      phil 		 * bmiss after coming out of scan because we haven't had
   1891  1.34.14.1      phil 		 * time to receive a beacon then we should probe the AP
   1892  1.34.14.1      phil 		 * before posting a real bmiss (unless iv_bmiss_max has
   1893  1.34.14.1      phil 		 * been artifiically lowered).  A cleaner solution might
   1894  1.34.14.1      phil 		 * be to disable the timer on scan start/end but to handle
   1895  1.34.14.1      phil 		 * case of multiple sta vap's we'd need to disable the
   1896  1.34.14.1      phil 		 * timers of all affected vap's.
   1897  1.34.14.1      phil 		 */
   1898  1.34.14.1      phil 		vap->iv_swbmiss_count = 0;
   1899  1.34.14.1      phil 	} else if (vap->iv_swbmiss_count == 0) {
   1900  1.34.14.1      phil 		if (vap->iv_bmiss != NULL)
   1901  1.34.14.1      phil 			ieee80211_runtask(ic, &vap->iv_swbmiss_task);
   1902  1.34.14.1      phil 	} else
   1903  1.34.14.1      phil 		vap->iv_swbmiss_count = 0;
   1904  1.34.14.1      phil 	callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
   1905  1.34.14.1      phil 		ieee80211_swbmiss, vap);
   1906  1.34.14.6      phil 
   1907  1.34.14.6      phil #if __NetBSD__
   1908  1.34.14.6      phil 	IEEE80211_UNLOCK(ic);
   1909  1.34.14.6      phil #endif
   1910  1.34.14.1      phil }
   1911  1.34.14.1      phil 
   1912  1.34.14.1      phil /*
   1913  1.34.14.1      phil  * Start an 802.11h channel switch.  We record the parameters,
   1914  1.34.14.1      phil  * mark the operation pending, notify each vap through the
   1915  1.34.14.1      phil  * beacon update mechanism so it can update the beacon frame
   1916  1.34.14.1      phil  * contents, and then switch vap's to CSA state to block outbound
   1917  1.34.14.1      phil  * traffic.  Devices that handle CSA directly can use the state
   1918  1.34.14.1      phil  * switch to do the right thing so long as they call
   1919  1.34.14.1      phil  * ieee80211_csa_completeswitch when it's time to complete the
   1920  1.34.14.1      phil  * channel change.  Devices that depend on the net80211 layer can
   1921  1.34.14.1      phil  * use ieee80211_beacon_update to handle the countdown and the
   1922  1.34.14.1      phil  * channel switch.
   1923  1.34.14.1      phil  */
   1924  1.34.14.1      phil void
   1925  1.34.14.1      phil ieee80211_csa_startswitch(struct ieee80211com *ic,
   1926  1.34.14.1      phil 	struct ieee80211_channel *c, int mode, int count)
   1927  1.34.14.1      phil {
   1928  1.34.14.1      phil 	struct ieee80211vap *vap;
   1929  1.34.14.1      phil 
   1930  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1931  1.34.14.1      phil 
   1932  1.34.14.1      phil 	ic->ic_csa_newchan = c;
   1933  1.34.14.1      phil 	ic->ic_csa_mode = mode;
   1934  1.34.14.1      phil 	ic->ic_csa_count = count;
   1935  1.34.14.1      phil 	ic->ic_flags |= IEEE80211_F_CSAPENDING;
   1936  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   1937  1.34.14.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
   1938  1.34.14.1      phil 		    vap->iv_opmode == IEEE80211_M_IBSS ||
   1939  1.34.14.1      phil 		    vap->iv_opmode == IEEE80211_M_MBSS)
   1940  1.34.14.1      phil 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_CSA);
   1941  1.34.14.1      phil 		/* switch to CSA state to block outbound traffic */
   1942  1.34.14.1      phil 		if (vap->iv_state == IEEE80211_S_RUN)
   1943  1.34.14.1      phil 			ieee80211_new_state_locked(vap, IEEE80211_S_CSA, 0);
   1944  1.34.14.1      phil 	}
   1945  1.34.14.1      phil 	ieee80211_notify_csa(ic, c, mode, count);
   1946  1.34.14.1      phil }
   1947  1.34.14.1      phil 
   1948  1.34.14.1      phil /*
   1949  1.34.14.1      phil  * Complete the channel switch by transitioning all CSA VAPs to RUN.
   1950  1.34.14.1      phil  * This is called by both the completion and cancellation functions
   1951  1.34.14.1      phil  * so each VAP is placed back in the RUN state and can thus transmit.
   1952  1.34.14.1      phil  */
   1953  1.34.14.1      phil static void
   1954  1.34.14.1      phil csa_completeswitch(struct ieee80211com *ic)
   1955  1.34.14.1      phil {
   1956  1.34.14.1      phil 	struct ieee80211vap *vap;
   1957  1.34.14.1      phil 
   1958  1.34.14.1      phil 	ic->ic_csa_newchan = NULL;
   1959  1.34.14.1      phil 	ic->ic_flags &= ~IEEE80211_F_CSAPENDING;
   1960  1.34.14.1      phil 
   1961  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   1962  1.34.14.1      phil 		if (vap->iv_state == IEEE80211_S_CSA)
   1963  1.34.14.1      phil 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
   1964  1.34.14.1      phil }
   1965  1.34.14.1      phil 
   1966  1.34.14.1      phil /*
   1967  1.34.14.1      phil  * Complete an 802.11h channel switch started by ieee80211_csa_startswitch.
   1968  1.34.14.1      phil  * We clear state and move all vap's in CSA state to RUN state
   1969  1.34.14.1      phil  * so they can again transmit.
   1970  1.34.14.1      phil  *
   1971  1.34.14.1      phil  * Although this may not be completely correct, update the BSS channel
   1972  1.34.14.1      phil  * for each VAP to the newly configured channel. The setcurchan sets
   1973  1.34.14.1      phil  * the current operating channel for the interface (so the radio does
   1974  1.34.14.1      phil  * switch over) but the VAP BSS isn't updated, leading to incorrectly
   1975  1.34.14.1      phil  * reported information via ioctl.
   1976  1.34.14.1      phil  */
   1977  1.34.14.1      phil void
   1978  1.34.14.1      phil ieee80211_csa_completeswitch(struct ieee80211com *ic)
   1979  1.34.14.1      phil {
   1980  1.34.14.1      phil 	struct ieee80211vap *vap;
   1981  1.34.14.1      phil 
   1982  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   1983  1.34.14.1      phil 
   1984  1.34.14.1      phil 	KASSERT(ic->ic_flags & IEEE80211_F_CSAPENDING, ("csa not pending"));
   1985  1.34.14.1      phil 
   1986  1.34.14.1      phil 	ieee80211_setcurchan(ic, ic->ic_csa_newchan);
   1987  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   1988  1.34.14.1      phil 		if (vap->iv_state == IEEE80211_S_CSA)
   1989  1.34.14.1      phil 			vap->iv_bss->ni_chan = ic->ic_curchan;
   1990  1.34.14.1      phil 
   1991  1.34.14.1      phil 	csa_completeswitch(ic);
   1992  1.34.14.1      phil }
   1993  1.34.14.1      phil 
   1994  1.34.14.1      phil /*
   1995  1.34.14.1      phil  * Cancel an 802.11h channel switch started by ieee80211_csa_startswitch.
   1996  1.34.14.1      phil  * We clear state and move all vap's in CSA state to RUN state
   1997  1.34.14.1      phil  * so they can again transmit.
   1998  1.34.14.1      phil  */
   1999  1.34.14.1      phil void
   2000  1.34.14.1      phil ieee80211_csa_cancelswitch(struct ieee80211com *ic)
   2001  1.34.14.1      phil {
   2002  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2003  1.34.14.1      phil 
   2004  1.34.14.1      phil 	csa_completeswitch(ic);
   2005  1.34.14.1      phil }
   2006  1.34.14.1      phil 
   2007  1.34.14.1      phil /*
   2008  1.34.14.1      phil  * Complete a DFS CAC started by ieee80211_dfs_cac_start.
   2009  1.34.14.1      phil  * We clear state and move all vap's in CAC state to RUN state.
   2010  1.34.14.1      phil  */
   2011  1.34.14.1      phil void
   2012  1.34.14.1      phil ieee80211_cac_completeswitch(struct ieee80211vap *vap0)
   2013  1.34.14.1      phil {
   2014  1.34.14.1      phil 	struct ieee80211com *ic = vap0->iv_ic;
   2015  1.34.14.1      phil 	struct ieee80211vap *vap;
   2016  1.34.14.1      phil 
   2017  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   2018  1.34.14.1      phil 	/*
   2019  1.34.14.1      phil 	 * Complete CAC state change for lead vap first; then
   2020  1.34.14.1      phil 	 * clock all the other vap's waiting.
   2021  1.34.14.1      phil 	 */
   2022  1.34.14.1      phil 	KASSERT(vap0->iv_state == IEEE80211_S_CAC,
   2023  1.34.14.1      phil 	    ("wrong state %d", vap0->iv_state));
   2024  1.34.14.1      phil 	ieee80211_new_state_locked(vap0, IEEE80211_S_RUN, 0);
   2025  1.34.14.1      phil 
   2026  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
   2027  1.34.14.1      phil 		if (vap->iv_state == IEEE80211_S_CAC && vap != vap0)
   2028  1.34.14.1      phil 			ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
   2029  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   2030  1.34.14.1      phil }
   2031  1.34.14.1      phil 
   2032  1.34.14.1      phil /*
   2033  1.34.14.1      phil  * Force all vap's other than the specified vap to the INIT state
   2034  1.34.14.1      phil  * and mark them as waiting for a scan to complete.  These vaps
   2035  1.34.14.1      phil  * will be brought up when the scan completes and the scanning vap
   2036  1.34.14.1      phil  * reaches RUN state by wakeupwaiting.
   2037  1.34.14.1      phil  */
   2038  1.34.14.1      phil static void
   2039  1.34.14.1      phil markwaiting(struct ieee80211vap *vap0)
   2040  1.34.14.1      phil {
   2041  1.34.14.1      phil 	struct ieee80211com *ic = vap0->iv_ic;
   2042  1.34.14.1      phil 	struct ieee80211vap *vap;
   2043  1.34.14.1      phil 
   2044  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2045  1.34.14.1      phil 
   2046  1.34.14.1      phil 	/*
   2047  1.34.14.1      phil 	 * A vap list entry can not disappear since we are running on the
   2048  1.34.14.1      phil 	 * taskqueue and a vap destroy will queue and drain another state
   2049  1.34.14.1      phil 	 * change task.
   2050  1.34.14.1      phil 	 */
   2051  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   2052  1.34.14.1      phil 		if (vap == vap0)
   2053  1.34.14.1      phil 			continue;
   2054  1.34.14.1      phil 		if (vap->iv_state != IEEE80211_S_INIT) {
   2055  1.34.14.1      phil 			/* NB: iv_newstate may drop the lock */
   2056  1.34.14.1      phil 			vap->iv_newstate(vap, IEEE80211_S_INIT, 0);
   2057  1.34.14.1      phil 			IEEE80211_LOCK_ASSERT(ic);
   2058  1.34.14.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
   2059  1.34.14.1      phil 		}
   2060  1.34.14.1      phil 	}
   2061  1.34.14.1      phil }
   2062  1.34.14.1      phil 
   2063  1.34.14.1      phil /*
   2064  1.34.14.1      phil  * Wakeup all vap's waiting for a scan to complete.  This is the
   2065  1.34.14.1      phil  * companion to markwaiting (above) and is used to coordinate
   2066  1.34.14.1      phil  * multiple vaps scanning.
   2067  1.34.14.1      phil  * This is called from the state taskqueue.
   2068  1.34.14.1      phil  */
   2069  1.34.14.1      phil static void
   2070  1.34.14.1      phil wakeupwaiting(struct ieee80211vap *vap0)
   2071  1.34.14.1      phil {
   2072  1.34.14.1      phil 	struct ieee80211com *ic = vap0->iv_ic;
   2073  1.34.14.1      phil 	struct ieee80211vap *vap;
   2074  1.34.14.1      phil 
   2075  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2076  1.34.14.1      phil 
   2077  1.34.14.1      phil 	/*
   2078  1.34.14.1      phil 	 * A vap list entry can not disappear since we are running on the
   2079  1.34.14.1      phil 	 * taskqueue and a vap destroy will queue and drain another state
   2080  1.34.14.1      phil 	 * change task.
   2081  1.34.14.1      phil 	 */
   2082  1.34.14.1      phil 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next) {
   2083  1.34.14.1      phil 		if (vap == vap0)
   2084  1.34.14.1      phil 			continue;
   2085  1.34.14.1      phil 		if (vap->iv_flags_ext & IEEE80211_FEXT_SCANWAIT) {
   2086  1.34.14.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
   2087  1.34.14.1      phil 			/* NB: sta's cannot go INIT->RUN */
   2088  1.34.14.1      phil 			/* NB: iv_newstate may drop the lock */
   2089  1.34.14.1      phil 			vap->iv_newstate(vap,
   2090  1.34.14.1      phil 			    vap->iv_opmode == IEEE80211_M_STA ?
   2091  1.34.14.1      phil 			        IEEE80211_S_SCAN : IEEE80211_S_RUN, 0);
   2092  1.34.14.1      phil 			IEEE80211_LOCK_ASSERT(ic);
   2093       1.19    dyoung 		}
   2094  1.34.14.1      phil 	}
   2095  1.34.14.1      phil }
   2096  1.34.14.1      phil 
   2097  1.34.14.1      phil /*
   2098  1.34.14.1      phil  * Handle post state change work common to all operating modes.
   2099  1.34.14.1      phil  */
   2100  1.34.14.1      phil static void
   2101  1.34.14.1      phil ieee80211_newstate_cb(void *xvap, int npending)
   2102  1.34.14.1      phil {
   2103  1.34.14.1      phil 	struct ieee80211vap *vap = xvap;
   2104  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2105  1.34.14.1      phil 	enum ieee80211_state nstate, ostate;
   2106  1.34.14.1      phil 	int arg, rc;
   2107  1.34.14.1      phil 
   2108  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   2109  1.34.14.1      phil 	nstate = vap->iv_nstate;
   2110  1.34.14.1      phil 	arg = vap->iv_nstate_arg;
   2111  1.34.14.1      phil 
   2112  1.34.14.1      phil 	if (vap->iv_flags_ext & IEEE80211_FEXT_REINIT) {
   2113       1.19    dyoung 		/*
   2114  1.34.14.1      phil 		 * We have been requested to drop back to the INIT before
   2115  1.34.14.1      phil 		 * proceeding to the new state.
   2116       1.19    dyoung 		 */
   2117  1.34.14.1      phil 		/* Deny any state changes while we are here. */
   2118  1.34.14.1      phil 		vap->iv_nstate = IEEE80211_S_INIT;
   2119  1.34.14.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2120  1.34.14.1      phil 		    "%s: %s -> %s arg %d\n", __func__,
   2121  1.34.14.1      phil 		    ieee80211_state_name[vap->iv_state],
   2122  1.34.14.1      phil 		    ieee80211_state_name[vap->iv_nstate], arg);
   2123  1.34.14.1      phil 		vap->iv_newstate(vap, vap->iv_nstate, 0);
   2124  1.34.14.1      phil 		IEEE80211_LOCK_ASSERT(ic);
   2125  1.34.14.1      phil 		vap->iv_flags_ext &= ~(IEEE80211_FEXT_REINIT |
   2126  1.34.14.1      phil 		    IEEE80211_FEXT_STATEWAIT);
   2127  1.34.14.1      phil 		/* enqueue new state transition after cancel_scan() task */
   2128  1.34.14.1      phil 		ieee80211_new_state_locked(vap, nstate, arg);
   2129  1.34.14.1      phil 		goto done;
   2130  1.34.14.1      phil 	}
   2131  1.34.14.1      phil 
   2132  1.34.14.1      phil 	ostate = vap->iv_state;
   2133  1.34.14.1      phil 	if (nstate == IEEE80211_S_SCAN && ostate != IEEE80211_S_INIT) {
   2134       1.19    dyoung 		/*
   2135  1.34.14.1      phil 		 * SCAN was forced; e.g. on beacon miss.  Force other running
   2136  1.34.14.1      phil 		 * vap's to INIT state and mark them as waiting for the scan to
   2137  1.34.14.1      phil 		 * complete.  This insures they don't interfere with our
   2138  1.34.14.1      phil 		 * scanning.  Since we are single threaded the vaps can not
   2139  1.34.14.1      phil 		 * transition again while we are executing.
   2140  1.34.14.1      phil 		 *
   2141  1.34.14.1      phil 		 * XXX not always right, assumes ap follows sta
   2142       1.19    dyoung 		 */
   2143  1.34.14.1      phil 		markwaiting(vap);
   2144  1.34.14.1      phil 	}
   2145  1.34.14.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2146  1.34.14.1      phil 	    "%s: %s -> %s arg %d\n", __func__,
   2147  1.34.14.1      phil 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate], arg);
   2148  1.34.14.1      phil 
   2149  1.34.14.1      phil 	rc = vap->iv_newstate(vap, nstate, arg);
   2150  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2151  1.34.14.1      phil 	vap->iv_flags_ext &= ~IEEE80211_FEXT_STATEWAIT;
   2152  1.34.14.1      phil 	if (rc != 0) {
   2153  1.34.14.1      phil 		/* State transition failed */
   2154  1.34.14.1      phil 		KASSERT(rc != EINPROGRESS, ("iv_newstate was deferred"));
   2155  1.34.14.1      phil 		KASSERT(nstate != IEEE80211_S_INIT,
   2156  1.34.14.1      phil 		    ("INIT state change failed"));
   2157  1.34.14.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2158  1.34.14.1      phil 		    "%s: %s returned error %d\n", __func__,
   2159  1.34.14.1      phil 		    ieee80211_state_name[nstate], rc);
   2160  1.34.14.1      phil 		goto done;
   2161  1.34.14.1      phil 	}
   2162  1.34.14.1      phil 
   2163  1.34.14.1      phil 	/* No actual transition, skip post processing */
   2164  1.34.14.1      phil 	if (ostate == nstate)
   2165  1.34.14.1      phil 		goto done;
   2166  1.34.14.1      phil 
   2167  1.34.14.1      phil 	if (nstate == IEEE80211_S_RUN) {
   2168  1.34.14.1      phil 		/*
   2169  1.34.14.1      phil 		 * OACTIVE may be set on the vap if the upper layer
   2170  1.34.14.1      phil 		 * tried to transmit (e.g. IPv6 NDP) before we reach
   2171  1.34.14.1      phil 		 * RUN state.  Clear it and restart xmit.
   2172  1.34.14.1      phil 		 *
   2173  1.34.14.1      phil 		 * Note this can also happen as a result of SLEEP->RUN
   2174  1.34.14.1      phil 		 * (i.e. coming out of power save mode).
   2175  1.34.14.1      phil 		 */
   2176  1.34.14.2      phil #if __FreeBSD__
   2177  1.34.14.1      phil 		vap->iv_ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
   2178  1.34.14.2      phil #elif __NetBSD__
   2179  1.34.14.2      phil 		vap->iv_ifp->if_flags &= ~IFF_OACTIVE;
   2180  1.34.14.2      phil #endif
   2181  1.34.14.1      phil 
   2182  1.34.14.1      phil 		/*
   2183  1.34.14.1      phil 		 * XXX TODO Kick-start a VAP queue - this should be a method!
   2184  1.34.14.1      phil 		 */
   2185  1.34.14.1      phil 
   2186  1.34.14.1      phil 		/* bring up any vaps waiting on us */
   2187  1.34.14.1      phil 		wakeupwaiting(vap);
   2188  1.34.14.1      phil 	} else if (nstate == IEEE80211_S_INIT) {
   2189  1.34.14.1      phil 		/*
   2190  1.34.14.1      phil 		 * Flush the scan cache if we did the last scan (XXX?)
   2191  1.34.14.1      phil 		 * and flush any frames on send queues from this vap.
   2192  1.34.14.1      phil 		 * Note the mgt q is used only for legacy drivers and
   2193  1.34.14.1      phil 		 * will go away shortly.
   2194  1.34.14.1      phil 		 */
   2195  1.34.14.1      phil 		ieee80211_scan_flush(vap);
   2196  1.34.14.1      phil 
   2197  1.34.14.1      phil 		/*
   2198  1.34.14.1      phil 		 * XXX TODO: ic/vap queue flush
   2199  1.34.14.1      phil 		 */
   2200  1.34.14.1      phil 	}
   2201  1.34.14.1      phil done:
   2202  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   2203  1.34.14.1      phil }
   2204  1.34.14.1      phil 
   2205  1.34.14.1      phil /*
   2206  1.34.14.1      phil  * Public interface for initiating a state machine change.
   2207  1.34.14.1      phil  * This routine single-threads the request and coordinates
   2208  1.34.14.1      phil  * the scheduling of multiple vaps for the purpose of selecting
   2209  1.34.14.1      phil  * an operating channel.  Specifically the following scenarios
   2210  1.34.14.1      phil  * are handled:
   2211  1.34.14.1      phil  * o only one vap can be selecting a channel so on transition to
   2212  1.34.14.1      phil  *   SCAN state if another vap is already scanning then
   2213  1.34.14.1      phil  *   mark the caller for later processing and return without
   2214  1.34.14.1      phil  *   doing anything (XXX? expectations by caller of synchronous operation)
   2215  1.34.14.1      phil  * o only one vap can be doing CAC of a channel so on transition to
   2216  1.34.14.1      phil  *   CAC state if another vap is already scanning for radar then
   2217  1.34.14.1      phil  *   mark the caller for later processing and return without
   2218  1.34.14.1      phil  *   doing anything (XXX? expectations by caller of synchronous operation)
   2219  1.34.14.1      phil  * o if another vap is already running when a request is made
   2220  1.34.14.1      phil  *   to SCAN then an operating channel has been chosen; bypass
   2221  1.34.14.1      phil  *   the scan and just join the channel
   2222  1.34.14.1      phil  *
   2223  1.34.14.1      phil  * Note that the state change call is done through the iv_newstate
   2224  1.34.14.1      phil  * method pointer so any driver routine gets invoked.  The driver
   2225  1.34.14.1      phil  * will normally call back into operating mode-specific
   2226  1.34.14.1      phil  * ieee80211_newstate routines (below) unless it needs to completely
   2227  1.34.14.1      phil  * bypass the state machine (e.g. because the firmware has it's
   2228  1.34.14.1      phil  * own idea how things should work).  Bypassing the net80211 layer
   2229  1.34.14.1      phil  * is usually a mistake and indicates lack of proper integration
   2230  1.34.14.1      phil  * with the net80211 layer.
   2231  1.34.14.1      phil  */
   2232  1.34.14.1      phil int
   2233  1.34.14.1      phil ieee80211_new_state_locked(struct ieee80211vap *vap,
   2234  1.34.14.1      phil 	enum ieee80211_state nstate, int arg)
   2235  1.34.14.1      phil {
   2236  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2237  1.34.14.1      phil 	struct ieee80211vap *vp;
   2238  1.34.14.1      phil 	enum ieee80211_state ostate;
   2239  1.34.14.1      phil 	int nrunning, nscanning;
   2240  1.34.14.1      phil 
   2241  1.34.14.1      phil 	IEEE80211_LOCK_ASSERT(ic);
   2242  1.34.14.1      phil 
   2243  1.34.14.1      phil 	if (vap->iv_flags_ext & IEEE80211_FEXT_STATEWAIT) {
   2244  1.34.14.1      phil 		if (vap->iv_nstate == IEEE80211_S_INIT ||
   2245  1.34.14.1      phil 		    ((vap->iv_state == IEEE80211_S_INIT ||
   2246  1.34.14.1      phil 		    (vap->iv_flags_ext & IEEE80211_FEXT_REINIT)) &&
   2247  1.34.14.1      phil 		    vap->iv_nstate == IEEE80211_S_SCAN &&
   2248  1.34.14.1      phil 		    nstate > IEEE80211_S_SCAN)) {
   2249  1.34.14.1      phil 			/*
   2250  1.34.14.1      phil 			 * XXX The vap is being stopped/started,
   2251  1.34.14.1      phil 			 * do not allow any other state changes
   2252  1.34.14.1      phil 			 * until this is completed.
   2253  1.34.14.1      phil 			 */
   2254  1.34.14.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2255  1.34.14.1      phil 			    "%s: %s -> %s (%s) transition discarded\n",
   2256  1.34.14.1      phil 			    __func__,
   2257  1.34.14.1      phil 			    ieee80211_state_name[vap->iv_state],
   2258  1.34.14.1      phil 			    ieee80211_state_name[nstate],
   2259  1.34.14.1      phil 			    ieee80211_state_name[vap->iv_nstate]);
   2260  1.34.14.1      phil 			return -1;
   2261  1.34.14.1      phil 		} else if (vap->iv_state != vap->iv_nstate) {
   2262  1.34.14.1      phil #if 0
   2263  1.34.14.1      phil 			/* Warn if the previous state hasn't completed. */
   2264  1.34.14.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2265  1.34.14.1      phil 			    "%s: pending %s -> %s transition lost\n", __func__,
   2266  1.34.14.1      phil 			    ieee80211_state_name[vap->iv_state],
   2267  1.34.14.1      phil 			    ieee80211_state_name[vap->iv_nstate]);
   2268  1.34.14.1      phil #else
   2269  1.34.14.1      phil 			/* XXX temporarily enable to identify issues */
   2270  1.34.14.1      phil 			if_printf(vap->iv_ifp,
   2271  1.34.14.1      phil 			    "%s: pending %s -> %s transition lost\n",
   2272  1.34.14.1      phil 			    __func__, ieee80211_state_name[vap->iv_state],
   2273  1.34.14.1      phil 			    ieee80211_state_name[vap->iv_nstate]);
   2274  1.34.14.1      phil #endif
   2275  1.34.14.1      phil 		}
   2276  1.34.14.1      phil 	}
   2277  1.34.14.1      phil 
   2278  1.34.14.1      phil 	nrunning = nscanning = 0;
   2279  1.34.14.1      phil 	/* XXX can track this state instead of calculating */
   2280  1.34.14.1      phil 	TAILQ_FOREACH(vp, &ic->ic_vaps, iv_next) {
   2281  1.34.14.1      phil 		if (vp != vap) {
   2282  1.34.14.1      phil 			if (vp->iv_state >= IEEE80211_S_RUN)
   2283  1.34.14.1      phil 				nrunning++;
   2284  1.34.14.1      phil 			/* XXX doesn't handle bg scan */
   2285  1.34.14.1      phil 			/* NB: CAC+AUTH+ASSOC treated like SCAN */
   2286  1.34.14.1      phil 			else if (vp->iv_state > IEEE80211_S_INIT)
   2287  1.34.14.1      phil 				nscanning++;
   2288  1.34.14.1      phil 		}
   2289  1.34.14.1      phil 	}
   2290  1.34.14.1      phil 	ostate = vap->iv_state;
   2291  1.34.14.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2292  1.34.14.1      phil 	    "%s: %s -> %s (nrunning %d nscanning %d)\n", __func__,
   2293  1.34.14.1      phil 	    ieee80211_state_name[ostate], ieee80211_state_name[nstate],
   2294  1.34.14.1      phil 	    nrunning, nscanning);
   2295  1.34.14.1      phil 	switch (nstate) {
   2296  1.34.14.1      phil 	case IEEE80211_S_SCAN:
   2297  1.34.14.1      phil 		if (ostate == IEEE80211_S_INIT) {
   2298  1.34.14.1      phil 			/*
   2299  1.34.14.1      phil 			 * INIT -> SCAN happens on initial bringup.
   2300  1.34.14.1      phil 			 */
   2301  1.34.14.1      phil 			KASSERT(!(nscanning && nrunning),
   2302  1.34.14.1      phil 			    ("%d scanning and %d running", nscanning, nrunning));
   2303  1.34.14.1      phil 			if (nscanning) {
   2304  1.34.14.1      phil 				/*
   2305  1.34.14.1      phil 				 * Someone is scanning, defer our state
   2306  1.34.14.1      phil 				 * change until the work has completed.
   2307  1.34.14.1      phil 				 */
   2308  1.34.14.1      phil 				IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2309  1.34.14.1      phil 				    "%s: defer %s -> %s\n",
   2310  1.34.14.1      phil 				    __func__, ieee80211_state_name[ostate],
   2311  1.34.14.1      phil 				    ieee80211_state_name[nstate]);
   2312  1.34.14.1      phil 				vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
   2313  1.34.14.1      phil 				return 0;
   2314  1.34.14.1      phil 			}
   2315  1.34.14.1      phil 			if (nrunning) {
   2316  1.34.14.1      phil 				/*
   2317  1.34.14.1      phil 				 * Someone is operating; just join the channel
   2318  1.34.14.1      phil 				 * they have chosen.
   2319  1.34.14.1      phil 				 */
   2320  1.34.14.1      phil 				/* XXX kill arg? */
   2321  1.34.14.1      phil 				/* XXX check each opmode, adhoc? */
   2322  1.34.14.1      phil 				if (vap->iv_opmode == IEEE80211_M_STA)
   2323  1.34.14.1      phil 					nstate = IEEE80211_S_SCAN;
   2324  1.34.14.1      phil 				else
   2325  1.34.14.1      phil 					nstate = IEEE80211_S_RUN;
   2326  1.34.14.1      phil #ifdef IEEE80211_DEBUG
   2327  1.34.14.1      phil 				if (nstate != IEEE80211_S_SCAN) {
   2328  1.34.14.1      phil 					IEEE80211_DPRINTF(vap,
   2329  1.34.14.1      phil 					    IEEE80211_MSG_STATE,
   2330  1.34.14.1      phil 					    "%s: override, now %s -> %s\n",
   2331  1.34.14.1      phil 					    __func__,
   2332  1.34.14.1      phil 					    ieee80211_state_name[ostate],
   2333  1.34.14.1      phil 					    ieee80211_state_name[nstate]);
   2334  1.34.14.1      phil 				}
   2335  1.34.14.1      phil #endif
   2336  1.34.14.1      phil 			}
   2337  1.34.14.1      phil 		}
   2338  1.34.14.1      phil 		break;
   2339  1.34.14.1      phil 	case IEEE80211_S_RUN:
   2340  1.34.14.1      phil 		if (vap->iv_opmode == IEEE80211_M_WDS &&
   2341  1.34.14.1      phil 		    (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY) &&
   2342  1.34.14.1      phil 		    nscanning) {
   2343  1.34.14.1      phil 			/*
   2344  1.34.14.1      phil 			 * Legacy WDS with someone else scanning; don't
   2345  1.34.14.1      phil 			 * go online until that completes as we should
   2346  1.34.14.1      phil 			 * follow the other vap to the channel they choose.
   2347  1.34.14.1      phil 			 */
   2348  1.34.14.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2349  1.34.14.1      phil 			     "%s: defer %s -> %s (legacy WDS)\n", __func__,
   2350  1.34.14.1      phil 			     ieee80211_state_name[ostate],
   2351  1.34.14.1      phil 			     ieee80211_state_name[nstate]);
   2352  1.34.14.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_SCANWAIT;
   2353  1.34.14.1      phil 			return 0;
   2354  1.34.14.1      phil 		}
   2355  1.34.14.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
   2356  1.34.14.1      phil 		    IEEE80211_IS_CHAN_DFS(ic->ic_bsschan) &&
   2357  1.34.14.1      phil 		    (vap->iv_flags_ext & IEEE80211_FEXT_DFS) &&
   2358  1.34.14.1      phil 		    !IEEE80211_IS_CHAN_CACDONE(ic->ic_bsschan)) {
   2359  1.34.14.1      phil 			/*
   2360  1.34.14.1      phil 			 * This is a DFS channel, transition to CAC state
   2361  1.34.14.1      phil 			 * instead of RUN.  This allows us to initiate
   2362  1.34.14.1      phil 			 * Channel Availability Check (CAC) as specified
   2363  1.34.14.1      phil 			 * by 11h/DFS.
   2364  1.34.14.1      phil 			 */
   2365  1.34.14.1      phil 			nstate = IEEE80211_S_CAC;
   2366  1.34.14.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE,
   2367  1.34.14.1      phil 			     "%s: override %s -> %s (DFS)\n", __func__,
   2368  1.34.14.1      phil 			     ieee80211_state_name[ostate],
   2369  1.34.14.1      phil 			     ieee80211_state_name[nstate]);
   2370  1.34.14.1      phil 		}
   2371  1.34.14.1      phil 		break;
   2372  1.34.14.1      phil 	case IEEE80211_S_INIT:
   2373  1.34.14.1      phil 		/* cancel any scan in progress */
   2374  1.34.14.1      phil 		ieee80211_cancel_scan(vap);
   2375  1.34.14.1      phil 		if (ostate == IEEE80211_S_INIT ) {
   2376  1.34.14.1      phil 			/* XXX don't believe this */
   2377  1.34.14.1      phil 			/* INIT -> INIT. nothing to do */
   2378  1.34.14.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANWAIT;
   2379  1.34.14.1      phil 		}
   2380  1.34.14.1      phil 		/* fall thru... */
   2381  1.34.14.1      phil 	default:
   2382        1.1    dyoung 		break;
   2383        1.1    dyoung 	}
   2384  1.34.14.1      phil 	/* defer the state change to a thread */
   2385  1.34.14.1      phil 	vap->iv_nstate = nstate;
   2386  1.34.14.1      phil 	vap->iv_nstate_arg = arg;
   2387  1.34.14.1      phil 	vap->iv_flags_ext |= IEEE80211_FEXT_STATEWAIT;
   2388  1.34.14.1      phil 	ieee80211_runtask(ic, &vap->iv_nstate_task);
   2389  1.34.14.1      phil 	return EINPROGRESS;
   2390  1.34.14.1      phil }
   2391  1.34.14.1      phil 
   2392  1.34.14.1      phil int
   2393  1.34.14.1      phil ieee80211_new_state(struct ieee80211vap *vap,
   2394  1.34.14.1      phil 	enum ieee80211_state nstate, int arg)
   2395  1.34.14.1      phil {
   2396  1.34.14.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2397  1.34.14.1      phil 	int rc;
   2398  1.34.14.1      phil 
   2399  1.34.14.1      phil 	IEEE80211_LOCK(ic);
   2400  1.34.14.1      phil 	rc = ieee80211_new_state_locked(vap, nstate, arg);
   2401  1.34.14.1      phil 	IEEE80211_UNLOCK(ic);
   2402  1.34.14.1      phil 	return rc;
   2403        1.1    dyoung }
   2404