Home | History | Annotate | Line # | Download | only in net80211
ieee80211_proto.c revision 1.1.1.6
      1      1.1  dyoung /*-
      2      1.1  dyoung  * Copyright (c) 2001 Atsushi Onoe
      3  1.1.1.4  dyoung  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      4      1.1  dyoung  * All rights reserved.
      5      1.1  dyoung  *
      6      1.1  dyoung  * Redistribution and use in source and binary forms, with or without
      7      1.1  dyoung  * modification, are permitted provided that the following conditions
      8      1.1  dyoung  * are met:
      9      1.1  dyoung  * 1. Redistributions of source code must retain the above copyright
     10      1.1  dyoung  *    notice, this list of conditions and the following disclaimer.
     11      1.1  dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     12      1.1  dyoung  *    notice, this list of conditions and the following disclaimer in the
     13      1.1  dyoung  *    documentation and/or other materials provided with the distribution.
     14      1.1  dyoung  * 3. The name of the author may not be used to endorse or promote products
     15      1.1  dyoung  *    derived from this software without specific prior written permission.
     16      1.1  dyoung  *
     17      1.1  dyoung  * Alternatively, this software may be distributed under the terms of the
     18      1.1  dyoung  * GNU General Public License ("GPL") version 2 as published by the Free
     19      1.1  dyoung  * Software Foundation.
     20      1.1  dyoung  *
     21      1.1  dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22      1.1  dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23      1.1  dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24      1.1  dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25      1.1  dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26      1.1  dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27      1.1  dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28      1.1  dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29      1.1  dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30      1.1  dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31      1.1  dyoung  */
     32      1.1  dyoung 
     33      1.1  dyoung #include <sys/cdefs.h>
     34  1.1.1.6   skrll __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_proto.c,v 1.23 2005/08/10 16:22:29 sam Exp $");
     35      1.1  dyoung 
     36      1.1  dyoung /*
     37      1.1  dyoung  * IEEE 802.11 protocol support.
     38      1.1  dyoung  */
     39      1.1  dyoung 
     40      1.1  dyoung #include "opt_inet.h"
     41      1.1  dyoung 
     42      1.1  dyoung #include <sys/param.h>
     43      1.1  dyoung #include <sys/kernel.h>
     44  1.1.1.4  dyoung #include <sys/systm.h>
     45  1.1.1.4  dyoung 
     46      1.1  dyoung #include <sys/socket.h>
     47      1.1  dyoung 
     48      1.1  dyoung #include <net/if.h>
     49      1.1  dyoung #include <net/if_media.h>
     50  1.1.1.4  dyoung #include <net/ethernet.h>		/* XXX for ether_sprintf */
     51      1.1  dyoung 
     52      1.1  dyoung #include <net80211/ieee80211_var.h>
     53      1.1  dyoung 
     54  1.1.1.4  dyoung /* XXX tunables */
     55  1.1.1.4  dyoung #define	AGGRESSIVE_MODE_SWITCH_HYSTERESIS	3	/* pkts / 100ms */
     56  1.1.1.4  dyoung #define	HIGH_PRI_SWITCH_THRESH			10	/* pkts / 100ms */
     57      1.1  dyoung 
     58      1.1  dyoung #define	IEEE80211_RATE2MBS(r)	(((r) & IEEE80211_RATE_VAL) / 2)
     59      1.1  dyoung 
     60      1.1  dyoung const char *ieee80211_mgt_subtype_name[] = {
     61      1.1  dyoung 	"assoc_req",	"assoc_resp",	"reassoc_req",	"reassoc_resp",
     62      1.1  dyoung 	"probe_req",	"probe_resp",	"reserved#6",	"reserved#7",
     63      1.1  dyoung 	"beacon",	"atim",		"disassoc",	"auth",
     64      1.1  dyoung 	"deauth",	"reserved#13",	"reserved#14",	"reserved#15"
     65      1.1  dyoung };
     66  1.1.1.4  dyoung const char *ieee80211_ctl_subtype_name[] = {
     67  1.1.1.4  dyoung 	"reserved#0",	"reserved#1",	"reserved#2",	"reserved#3",
     68  1.1.1.4  dyoung 	"reserved#3",	"reserved#5",	"reserved#6",	"reserved#7",
     69  1.1.1.4  dyoung 	"reserved#8",	"reserved#9",	"ps_poll",	"rts",
     70  1.1.1.4  dyoung 	"cts",		"ack",		"cf_end",	"cf_end_ack"
     71  1.1.1.4  dyoung };
     72      1.1  dyoung const char *ieee80211_state_name[IEEE80211_S_MAX] = {
     73      1.1  dyoung 	"INIT",		/* IEEE80211_S_INIT */
     74      1.1  dyoung 	"SCAN",		/* IEEE80211_S_SCAN */
     75      1.1  dyoung 	"AUTH",		/* IEEE80211_S_AUTH */
     76      1.1  dyoung 	"ASSOC",	/* IEEE80211_S_ASSOC */
     77      1.1  dyoung 	"RUN"		/* IEEE80211_S_RUN */
     78      1.1  dyoung };
     79  1.1.1.4  dyoung const char *ieee80211_wme_acnames[] = {
     80  1.1.1.4  dyoung 	"WME_AC_BE",
     81  1.1.1.4  dyoung 	"WME_AC_BK",
     82  1.1.1.4  dyoung 	"WME_AC_VI",
     83  1.1.1.4  dyoung 	"WME_AC_VO",
     84  1.1.1.4  dyoung 	"WME_UPSD",
     85  1.1.1.4  dyoung };
     86      1.1  dyoung 
     87      1.1  dyoung static int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
     88      1.1  dyoung 
     89      1.1  dyoung void
     90  1.1.1.4  dyoung ieee80211_proto_attach(struct ieee80211com *ic)
     91      1.1  dyoung {
     92  1.1.1.4  dyoung 	struct ifnet *ifp = ic->ic_ifp;
     93      1.1  dyoung 
     94  1.1.1.4  dyoung 	/* XXX room for crypto  */
     95  1.1.1.4  dyoung 	ifp->if_hdrlen = sizeof(struct ieee80211_qosframe_addr4);
     96      1.1  dyoung 
     97      1.1  dyoung 	ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
     98  1.1.1.6   skrll 	ic->ic_fragthreshold = IEEE80211_FRAG_DEFAULT;
     99  1.1.1.6   skrll 	ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
    100  1.1.1.3  dyoung 	ic->ic_protmode = IEEE80211_PROT_CTSONLY;
    101  1.1.1.4  dyoung 	ic->ic_roaming = IEEE80211_ROAMING_AUTO;
    102  1.1.1.4  dyoung 
    103  1.1.1.4  dyoung 	ic->ic_wme.wme_hipri_switch_hysteresis =
    104  1.1.1.4  dyoung 		AGGRESSIVE_MODE_SWITCH_HYSTERESIS;
    105      1.1  dyoung 
    106  1.1.1.2  dyoung 	mtx_init(&ic->ic_mgtq.ifq_mtx, ifp->if_xname, "mgmt send q", MTX_DEF);
    107      1.1  dyoung 
    108      1.1  dyoung 	/* protocol state change handler */
    109      1.1  dyoung 	ic->ic_newstate = ieee80211_newstate;
    110      1.1  dyoung 
    111      1.1  dyoung 	/* initialize management frame handlers */
    112      1.1  dyoung 	ic->ic_recv_mgmt = ieee80211_recv_mgmt;
    113      1.1  dyoung 	ic->ic_send_mgmt = ieee80211_send_mgmt;
    114      1.1  dyoung }
    115      1.1  dyoung 
    116      1.1  dyoung void
    117  1.1.1.4  dyoung ieee80211_proto_detach(struct ieee80211com *ic)
    118      1.1  dyoung {
    119  1.1.1.4  dyoung 
    120  1.1.1.4  dyoung 	/*
    121  1.1.1.4  dyoung 	 * This should not be needed as we detach when reseting
    122  1.1.1.4  dyoung 	 * the state but be conservative here since the
    123  1.1.1.4  dyoung 	 * authenticator may do things like spawn kernel threads.
    124  1.1.1.4  dyoung 	 */
    125  1.1.1.4  dyoung 	if (ic->ic_auth->ia_detach)
    126  1.1.1.4  dyoung 		ic->ic_auth->ia_detach(ic);
    127      1.1  dyoung 
    128      1.1  dyoung 	IF_DRAIN(&ic->ic_mgtq);
    129      1.1  dyoung 	mtx_destroy(&ic->ic_mgtq.ifq_mtx);
    130  1.1.1.4  dyoung 
    131  1.1.1.4  dyoung 	/*
    132  1.1.1.4  dyoung 	 * Detach any ACL'ator.
    133  1.1.1.4  dyoung 	 */
    134  1.1.1.4  dyoung 	if (ic->ic_acl != NULL)
    135  1.1.1.4  dyoung 		ic->ic_acl->iac_detach(ic);
    136  1.1.1.4  dyoung }
    137  1.1.1.4  dyoung 
    138  1.1.1.4  dyoung /*
    139  1.1.1.4  dyoung  * Simple-minded authenticator module support.
    140  1.1.1.4  dyoung  */
    141  1.1.1.4  dyoung 
    142  1.1.1.4  dyoung #define	IEEE80211_AUTH_MAX	(IEEE80211_AUTH_WPA+1)
    143  1.1.1.4  dyoung /* XXX well-known names */
    144  1.1.1.4  dyoung static const char *auth_modnames[IEEE80211_AUTH_MAX] = {
    145  1.1.1.4  dyoung 	"wlan_internal",	/* IEEE80211_AUTH_NONE */
    146  1.1.1.4  dyoung 	"wlan_internal",	/* IEEE80211_AUTH_OPEN */
    147  1.1.1.4  dyoung 	"wlan_internal",	/* IEEE80211_AUTH_SHARED */
    148  1.1.1.4  dyoung 	"wlan_xauth",		/* IEEE80211_AUTH_8021X	 */
    149  1.1.1.4  dyoung 	"wlan_internal",	/* IEEE80211_AUTH_AUTO */
    150  1.1.1.4  dyoung 	"wlan_xauth",		/* IEEE80211_AUTH_WPA */
    151  1.1.1.4  dyoung };
    152  1.1.1.4  dyoung static const struct ieee80211_authenticator *authenticators[IEEE80211_AUTH_MAX];
    153  1.1.1.4  dyoung 
    154  1.1.1.4  dyoung static const struct ieee80211_authenticator auth_internal = {
    155  1.1.1.4  dyoung 	.ia_name		= "wlan_internal",
    156  1.1.1.4  dyoung 	.ia_attach		= NULL,
    157  1.1.1.4  dyoung 	.ia_detach		= NULL,
    158  1.1.1.4  dyoung 	.ia_node_join		= NULL,
    159  1.1.1.4  dyoung 	.ia_node_leave		= NULL,
    160  1.1.1.4  dyoung };
    161  1.1.1.4  dyoung 
    162  1.1.1.4  dyoung /*
    163  1.1.1.4  dyoung  * Setup internal authenticators once; they are never unregistered.
    164  1.1.1.4  dyoung  */
    165  1.1.1.4  dyoung static void
    166  1.1.1.4  dyoung ieee80211_auth_setup(void)
    167  1.1.1.4  dyoung {
    168  1.1.1.4  dyoung 	ieee80211_authenticator_register(IEEE80211_AUTH_OPEN, &auth_internal);
    169  1.1.1.4  dyoung 	ieee80211_authenticator_register(IEEE80211_AUTH_SHARED, &auth_internal);
    170  1.1.1.4  dyoung 	ieee80211_authenticator_register(IEEE80211_AUTH_AUTO, &auth_internal);
    171  1.1.1.4  dyoung }
    172  1.1.1.4  dyoung SYSINIT(wlan_auth, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_auth_setup, NULL);
    173  1.1.1.4  dyoung 
    174  1.1.1.4  dyoung const struct ieee80211_authenticator *
    175  1.1.1.4  dyoung ieee80211_authenticator_get(int auth)
    176  1.1.1.4  dyoung {
    177  1.1.1.4  dyoung 	if (auth >= IEEE80211_AUTH_MAX)
    178  1.1.1.4  dyoung 		return NULL;
    179  1.1.1.4  dyoung 	if (authenticators[auth] == NULL)
    180  1.1.1.4  dyoung 		ieee80211_load_module(auth_modnames[auth]);
    181  1.1.1.4  dyoung 	return authenticators[auth];
    182  1.1.1.4  dyoung }
    183  1.1.1.4  dyoung 
    184  1.1.1.4  dyoung void
    185  1.1.1.4  dyoung ieee80211_authenticator_register(int type,
    186  1.1.1.4  dyoung 	const struct ieee80211_authenticator *auth)
    187  1.1.1.4  dyoung {
    188  1.1.1.4  dyoung 	if (type >= IEEE80211_AUTH_MAX)
    189  1.1.1.4  dyoung 		return;
    190  1.1.1.4  dyoung 	authenticators[type] = auth;
    191      1.1  dyoung }
    192      1.1  dyoung 
    193      1.1  dyoung void
    194  1.1.1.4  dyoung ieee80211_authenticator_unregister(int type)
    195      1.1  dyoung {
    196  1.1.1.4  dyoung 
    197  1.1.1.4  dyoung 	if (type >= IEEE80211_AUTH_MAX)
    198  1.1.1.4  dyoung 		return;
    199  1.1.1.4  dyoung 	authenticators[type] = NULL;
    200  1.1.1.4  dyoung }
    201  1.1.1.4  dyoung 
    202  1.1.1.4  dyoung /*
    203  1.1.1.4  dyoung  * Very simple-minded ACL module support.
    204  1.1.1.4  dyoung  */
    205  1.1.1.4  dyoung /* XXX just one for now */
    206  1.1.1.4  dyoung static	const struct ieee80211_aclator *acl = NULL;
    207  1.1.1.4  dyoung 
    208  1.1.1.4  dyoung void
    209  1.1.1.4  dyoung ieee80211_aclator_register(const struct ieee80211_aclator *iac)
    210  1.1.1.4  dyoung {
    211  1.1.1.4  dyoung 	printf("wlan: %s acl policy registered\n", iac->iac_name);
    212  1.1.1.4  dyoung 	acl = iac;
    213  1.1.1.4  dyoung }
    214  1.1.1.4  dyoung 
    215  1.1.1.4  dyoung void
    216  1.1.1.4  dyoung ieee80211_aclator_unregister(const struct ieee80211_aclator *iac)
    217  1.1.1.4  dyoung {
    218  1.1.1.4  dyoung 	if (acl == iac)
    219  1.1.1.4  dyoung 		acl = NULL;
    220  1.1.1.4  dyoung 	printf("wlan: %s acl policy unregistered\n", iac->iac_name);
    221  1.1.1.4  dyoung }
    222  1.1.1.4  dyoung 
    223  1.1.1.4  dyoung const struct ieee80211_aclator *
    224  1.1.1.4  dyoung ieee80211_aclator_get(const char *name)
    225  1.1.1.4  dyoung {
    226  1.1.1.4  dyoung 	if (acl == NULL)
    227  1.1.1.4  dyoung 		ieee80211_load_module("wlan_acl");
    228  1.1.1.4  dyoung 	return acl != NULL && strcmp(acl->iac_name, name) == 0 ? acl : NULL;
    229  1.1.1.4  dyoung }
    230  1.1.1.4  dyoung 
    231  1.1.1.4  dyoung void
    232  1.1.1.4  dyoung ieee80211_print_essid(const u_int8_t *essid, int len)
    233  1.1.1.4  dyoung {
    234  1.1.1.4  dyoung 	const u_int8_t *p;
    235      1.1  dyoung 	int i;
    236      1.1  dyoung 
    237      1.1  dyoung 	if (len > IEEE80211_NWID_LEN)
    238      1.1  dyoung 		len = IEEE80211_NWID_LEN;
    239      1.1  dyoung 	/* determine printable or not */
    240      1.1  dyoung 	for (i = 0, p = essid; i < len; i++, p++) {
    241      1.1  dyoung 		if (*p < ' ' || *p > 0x7e)
    242      1.1  dyoung 			break;
    243      1.1  dyoung 	}
    244      1.1  dyoung 	if (i == len) {
    245      1.1  dyoung 		printf("\"");
    246      1.1  dyoung 		for (i = 0, p = essid; i < len; i++, p++)
    247      1.1  dyoung 			printf("%c", *p);
    248      1.1  dyoung 		printf("\"");
    249      1.1  dyoung 	} else {
    250      1.1  dyoung 		printf("0x");
    251      1.1  dyoung 		for (i = 0, p = essid; i < len; i++, p++)
    252      1.1  dyoung 			printf("%02x", *p);
    253      1.1  dyoung 	}
    254      1.1  dyoung }
    255      1.1  dyoung 
    256      1.1  dyoung void
    257  1.1.1.4  dyoung ieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi)
    258      1.1  dyoung {
    259  1.1.1.4  dyoung 	const struct ieee80211_frame *wh;
    260      1.1  dyoung 	int i;
    261      1.1  dyoung 
    262  1.1.1.4  dyoung 	wh = (const struct ieee80211_frame *)buf;
    263      1.1  dyoung 	switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
    264      1.1  dyoung 	case IEEE80211_FC1_DIR_NODS:
    265      1.1  dyoung 		printf("NODS %s", ether_sprintf(wh->i_addr2));
    266      1.1  dyoung 		printf("->%s", ether_sprintf(wh->i_addr1));
    267      1.1  dyoung 		printf("(%s)", ether_sprintf(wh->i_addr3));
    268      1.1  dyoung 		break;
    269      1.1  dyoung 	case IEEE80211_FC1_DIR_TODS:
    270      1.1  dyoung 		printf("TODS %s", ether_sprintf(wh->i_addr2));
    271      1.1  dyoung 		printf("->%s", ether_sprintf(wh->i_addr3));
    272      1.1  dyoung 		printf("(%s)", ether_sprintf(wh->i_addr1));
    273      1.1  dyoung 		break;
    274      1.1  dyoung 	case IEEE80211_FC1_DIR_FROMDS:
    275      1.1  dyoung 		printf("FRDS %s", ether_sprintf(wh->i_addr3));
    276      1.1  dyoung 		printf("->%s", ether_sprintf(wh->i_addr1));
    277      1.1  dyoung 		printf("(%s)", ether_sprintf(wh->i_addr2));
    278      1.1  dyoung 		break;
    279      1.1  dyoung 	case IEEE80211_FC1_DIR_DSTODS:
    280  1.1.1.4  dyoung 		printf("DSDS %s", ether_sprintf((const u_int8_t *)&wh[1]));
    281      1.1  dyoung 		printf("->%s", ether_sprintf(wh->i_addr3));
    282      1.1  dyoung 		printf("(%s", ether_sprintf(wh->i_addr2));
    283      1.1  dyoung 		printf("->%s)", ether_sprintf(wh->i_addr1));
    284      1.1  dyoung 		break;
    285      1.1  dyoung 	}
    286      1.1  dyoung 	switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
    287      1.1  dyoung 	case IEEE80211_FC0_TYPE_DATA:
    288      1.1  dyoung 		printf(" data");
    289      1.1  dyoung 		break;
    290      1.1  dyoung 	case IEEE80211_FC0_TYPE_MGT:
    291      1.1  dyoung 		printf(" %s", ieee80211_mgt_subtype_name[
    292      1.1  dyoung 		    (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
    293      1.1  dyoung 		    >> IEEE80211_FC0_SUBTYPE_SHIFT]);
    294      1.1  dyoung 		break;
    295      1.1  dyoung 	default:
    296      1.1  dyoung 		printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
    297      1.1  dyoung 		break;
    298      1.1  dyoung 	}
    299  1.1.1.4  dyoung 	if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
    300  1.1.1.4  dyoung 		int i;
    301  1.1.1.4  dyoung 		printf(" WEP [IV");
    302  1.1.1.4  dyoung 		for (i = 0; i < IEEE80211_WEP_IVLEN; i++)
    303  1.1.1.4  dyoung 			printf(" %.02x", buf[sizeof(*wh)+i]);
    304  1.1.1.4  dyoung 		printf(" KID %u]", buf[sizeof(*wh)+i] >> 6);
    305  1.1.1.4  dyoung 	}
    306      1.1  dyoung 	if (rate >= 0)
    307      1.1  dyoung 		printf(" %dM", rate / 2);
    308      1.1  dyoung 	if (rssi >= 0)
    309      1.1  dyoung 		printf(" +%d", rssi);
    310      1.1  dyoung 	printf("\n");
    311      1.1  dyoung 	if (len > 0) {
    312      1.1  dyoung 		for (i = 0; i < len; i++) {
    313      1.1  dyoung 			if ((i & 1) == 0)
    314      1.1  dyoung 				printf(" ");
    315      1.1  dyoung 			printf("%02x", buf[i]);
    316      1.1  dyoung 		}
    317      1.1  dyoung 		printf("\n");
    318      1.1  dyoung 	}
    319      1.1  dyoung }
    320      1.1  dyoung 
    321      1.1  dyoung int
    322  1.1.1.6   skrll ieee80211_fix_rate(struct ieee80211_node *ni, int flags)
    323      1.1  dyoung {
    324      1.1  dyoung #define	RV(v)	((v) & IEEE80211_RATE_VAL)
    325  1.1.1.6   skrll 	struct ieee80211com *ic = ni->ni_ic;
    326      1.1  dyoung 	int i, j, ignore, error;
    327  1.1.1.4  dyoung 	int okrate, badrate, fixedrate;
    328      1.1  dyoung 	struct ieee80211_rateset *srs, *nrs;
    329      1.1  dyoung 	u_int8_t r;
    330      1.1  dyoung 
    331  1.1.1.4  dyoung 	/*
    332  1.1.1.4  dyoung 	 * If the fixed rate check was requested but no
    333  1.1.1.4  dyoung 	 * fixed has been defined then just remove it.
    334  1.1.1.4  dyoung 	 */
    335  1.1.1.6   skrll 	if ((flags & IEEE80211_F_DOFRATE) &&
    336  1.1.1.6   skrll 	    ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
    337  1.1.1.4  dyoung 		flags &= ~IEEE80211_F_DOFRATE;
    338      1.1  dyoung 	error = 0;
    339  1.1.1.4  dyoung 	okrate = badrate = fixedrate = 0;
    340      1.1  dyoung 	srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
    341      1.1  dyoung 	nrs = &ni->ni_rates;
    342  1.1.1.2  dyoung 	for (i = 0; i < nrs->rs_nrates; ) {
    343      1.1  dyoung 		ignore = 0;
    344      1.1  dyoung 		if (flags & IEEE80211_F_DOSORT) {
    345      1.1  dyoung 			/*
    346      1.1  dyoung 			 * Sort rates.
    347      1.1  dyoung 			 */
    348      1.1  dyoung 			for (j = i + 1; j < nrs->rs_nrates; j++) {
    349      1.1  dyoung 				if (RV(nrs->rs_rates[i]) > RV(nrs->rs_rates[j])) {
    350      1.1  dyoung 					r = nrs->rs_rates[i];
    351      1.1  dyoung 					nrs->rs_rates[i] = nrs->rs_rates[j];
    352      1.1  dyoung 					nrs->rs_rates[j] = r;
    353      1.1  dyoung 				}
    354      1.1  dyoung 			}
    355      1.1  dyoung 		}
    356      1.1  dyoung 		r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
    357      1.1  dyoung 		badrate = r;
    358      1.1  dyoung 		if (flags & IEEE80211_F_DOFRATE) {
    359      1.1  dyoung 			/*
    360  1.1.1.4  dyoung 			 * Check any fixed rate is included.
    361      1.1  dyoung 			 */
    362  1.1.1.4  dyoung 			if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
    363  1.1.1.4  dyoung 				fixedrate = r;
    364      1.1  dyoung 		}
    365      1.1  dyoung 		if (flags & IEEE80211_F_DONEGO) {
    366      1.1  dyoung 			/*
    367      1.1  dyoung 			 * Check against supported rates.
    368      1.1  dyoung 			 */
    369      1.1  dyoung 			for (j = 0; j < srs->rs_nrates; j++) {
    370  1.1.1.3  dyoung 				if (r == RV(srs->rs_rates[j])) {
    371  1.1.1.3  dyoung 					/*
    372  1.1.1.3  dyoung 					 * Overwrite with the supported rate
    373  1.1.1.3  dyoung 					 * value so any basic rate bit is set.
    374  1.1.1.3  dyoung 					 * This insures that response we send
    375  1.1.1.3  dyoung 					 * to stations have the necessary basic
    376  1.1.1.3  dyoung 					 * rate bit set.
    377  1.1.1.3  dyoung 					 */
    378  1.1.1.3  dyoung 					nrs->rs_rates[i] = srs->rs_rates[j];
    379      1.1  dyoung 					break;
    380  1.1.1.3  dyoung 				}
    381      1.1  dyoung 			}
    382      1.1  dyoung 			if (j == srs->rs_nrates) {
    383  1.1.1.2  dyoung 				/*
    384  1.1.1.2  dyoung 				 * A rate in the node's rate set is not
    385  1.1.1.2  dyoung 				 * supported.  If this is a basic rate and we
    386  1.1.1.2  dyoung 				 * are operating as an AP then this is an error.
    387  1.1.1.2  dyoung 				 * Otherwise we just discard/ignore the rate.
    388  1.1.1.2  dyoung 				 * Note that this is important for 11b stations
    389  1.1.1.2  dyoung 				 * when they want to associate with an 11g AP.
    390  1.1.1.2  dyoung 				 */
    391  1.1.1.2  dyoung 				if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    392  1.1.1.2  dyoung 				    (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
    393      1.1  dyoung 					error++;
    394      1.1  dyoung 				ignore++;
    395      1.1  dyoung 			}
    396      1.1  dyoung 		}
    397      1.1  dyoung 		if (flags & IEEE80211_F_DODEL) {
    398      1.1  dyoung 			/*
    399      1.1  dyoung 			 * Delete unacceptable rates.
    400      1.1  dyoung 			 */
    401      1.1  dyoung 			if (ignore) {
    402      1.1  dyoung 				nrs->rs_nrates--;
    403      1.1  dyoung 				for (j = i; j < nrs->rs_nrates; j++)
    404      1.1  dyoung 					nrs->rs_rates[j] = nrs->rs_rates[j + 1];
    405      1.1  dyoung 				nrs->rs_rates[j] = 0;
    406      1.1  dyoung 				continue;
    407      1.1  dyoung 			}
    408      1.1  dyoung 		}
    409      1.1  dyoung 		if (!ignore)
    410      1.1  dyoung 			okrate = nrs->rs_rates[i];
    411      1.1  dyoung 		i++;
    412      1.1  dyoung 	}
    413  1.1.1.4  dyoung 	if (okrate == 0 || error != 0 ||
    414  1.1.1.4  dyoung 	    ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
    415      1.1  dyoung 		return badrate | IEEE80211_RATE_BASIC;
    416      1.1  dyoung 	else
    417      1.1  dyoung 		return RV(okrate);
    418      1.1  dyoung #undef RV
    419      1.1  dyoung }
    420      1.1  dyoung 
    421  1.1.1.4  dyoung /*
    422  1.1.1.4  dyoung  * Reset 11g-related state.
    423  1.1.1.4  dyoung  */
    424  1.1.1.4  dyoung void
    425  1.1.1.4  dyoung ieee80211_reset_erp(struct ieee80211com *ic)
    426  1.1.1.4  dyoung {
    427  1.1.1.4  dyoung 	ic->ic_flags &= ~IEEE80211_F_USEPROT;
    428  1.1.1.4  dyoung 	ic->ic_nonerpsta = 0;
    429  1.1.1.4  dyoung 	ic->ic_longslotsta = 0;
    430  1.1.1.4  dyoung 	/*
    431  1.1.1.4  dyoung 	 * Short slot time is enabled only when operating in 11g
    432  1.1.1.4  dyoung 	 * and not in an IBSS.  We must also honor whether or not
    433  1.1.1.4  dyoung 	 * the driver is capable of doing it.
    434  1.1.1.4  dyoung 	 */
    435  1.1.1.4  dyoung 	ieee80211_set_shortslottime(ic,
    436  1.1.1.4  dyoung 		ic->ic_curmode == IEEE80211_MODE_11A ||
    437  1.1.1.4  dyoung 		(ic->ic_curmode == IEEE80211_MODE_11G &&
    438  1.1.1.4  dyoung 		ic->ic_opmode == IEEE80211_M_HOSTAP &&
    439  1.1.1.4  dyoung 		(ic->ic_caps & IEEE80211_C_SHSLOT)));
    440  1.1.1.4  dyoung 	/*
    441  1.1.1.4  dyoung 	 * Set short preamble and ERP barker-preamble flags.
    442  1.1.1.4  dyoung 	 */
    443  1.1.1.4  dyoung 	if (ic->ic_curmode == IEEE80211_MODE_11A ||
    444  1.1.1.4  dyoung 	    (ic->ic_caps & IEEE80211_C_SHPREAMBLE)) {
    445  1.1.1.4  dyoung 		ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
    446  1.1.1.4  dyoung 		ic->ic_flags &= ~IEEE80211_F_USEBARKER;
    447  1.1.1.4  dyoung 	} else {
    448  1.1.1.4  dyoung 		ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
    449  1.1.1.4  dyoung 		ic->ic_flags |= IEEE80211_F_USEBARKER;
    450  1.1.1.4  dyoung 	}
    451  1.1.1.4  dyoung }
    452  1.1.1.4  dyoung 
    453  1.1.1.4  dyoung /*
    454  1.1.1.4  dyoung  * Set the short slot time state and notify the driver.
    455  1.1.1.4  dyoung  */
    456  1.1.1.4  dyoung void
    457  1.1.1.4  dyoung ieee80211_set_shortslottime(struct ieee80211com *ic, int onoff)
    458  1.1.1.4  dyoung {
    459  1.1.1.4  dyoung 	if (onoff)
    460  1.1.1.4  dyoung 		ic->ic_flags |= IEEE80211_F_SHSLOT;
    461  1.1.1.4  dyoung 	else
    462  1.1.1.4  dyoung 		ic->ic_flags &= ~IEEE80211_F_SHSLOT;
    463  1.1.1.4  dyoung 	/* notify driver */
    464  1.1.1.4  dyoung 	if (ic->ic_updateslot != NULL)
    465  1.1.1.4  dyoung 		ic->ic_updateslot(ic->ic_ifp);
    466  1.1.1.4  dyoung }
    467  1.1.1.4  dyoung 
    468  1.1.1.4  dyoung /*
    469  1.1.1.4  dyoung  * Check if the specified rate set supports ERP.
    470  1.1.1.4  dyoung  * NB: the rate set is assumed to be sorted.
    471  1.1.1.4  dyoung  */
    472  1.1.1.4  dyoung int
    473  1.1.1.4  dyoung ieee80211_iserp_rateset(struct ieee80211com *ic, struct ieee80211_rateset *rs)
    474  1.1.1.4  dyoung {
    475  1.1.1.4  dyoung #define N(a)	(sizeof(a) / sizeof(a[0]))
    476  1.1.1.4  dyoung 	static const int rates[] = { 2, 4, 11, 22, 12, 24, 48 };
    477  1.1.1.4  dyoung 	int i, j;
    478  1.1.1.4  dyoung 
    479  1.1.1.4  dyoung 	if (rs->rs_nrates < N(rates))
    480  1.1.1.4  dyoung 		return 0;
    481  1.1.1.4  dyoung 	for (i = 0; i < N(rates); i++) {
    482  1.1.1.4  dyoung 		for (j = 0; j < rs->rs_nrates; j++) {
    483  1.1.1.4  dyoung 			int r = rs->rs_rates[j] & IEEE80211_RATE_VAL;
    484  1.1.1.4  dyoung 			if (rates[i] == r)
    485  1.1.1.4  dyoung 				goto next;
    486  1.1.1.4  dyoung 			if (r > rates[i])
    487  1.1.1.4  dyoung 				return 0;
    488  1.1.1.4  dyoung 		}
    489  1.1.1.4  dyoung 		return 0;
    490  1.1.1.4  dyoung 	next:
    491  1.1.1.4  dyoung 		;
    492  1.1.1.4  dyoung 	}
    493  1.1.1.4  dyoung 	return 1;
    494  1.1.1.4  dyoung #undef N
    495  1.1.1.4  dyoung }
    496  1.1.1.4  dyoung 
    497  1.1.1.4  dyoung /*
    498  1.1.1.4  dyoung  * Mark the basic rates for the 11g rate table based on the
    499  1.1.1.4  dyoung  * operating mode.  For real 11g we mark all the 11b rates
    500  1.1.1.4  dyoung  * and 6, 12, and 24 OFDM.  For 11b compatibility we mark only
    501  1.1.1.4  dyoung  * 11b rates.  There's also a pseudo 11a-mode used to mark only
    502  1.1.1.4  dyoung  * the basic OFDM rates.
    503  1.1.1.4  dyoung  */
    504  1.1.1.4  dyoung void
    505  1.1.1.4  dyoung ieee80211_set11gbasicrates(struct ieee80211_rateset *rs, enum ieee80211_phymode mode)
    506  1.1.1.4  dyoung {
    507  1.1.1.4  dyoung 	static const struct ieee80211_rateset basic[] = {
    508  1.1.1.4  dyoung 	    { 0 },			/* IEEE80211_MODE_AUTO */
    509  1.1.1.4  dyoung 	    { 3, { 12, 24, 48 } },	/* IEEE80211_MODE_11A */
    510  1.1.1.4  dyoung 	    { 2, { 2, 4 } },		/* IEEE80211_MODE_11B */
    511  1.1.1.4  dyoung 	    { 4, { 2, 4, 11, 22 } },	/* IEEE80211_MODE_11G (mixed b/g) */
    512  1.1.1.4  dyoung 	    { 0 },			/* IEEE80211_MODE_FH */
    513  1.1.1.4  dyoung 					/* IEEE80211_MODE_PUREG (not yet) */
    514  1.1.1.4  dyoung 	    { 7, { 2, 4, 11, 22, 12, 24, 48 } },
    515  1.1.1.4  dyoung 	};
    516  1.1.1.4  dyoung 	int i, j;
    517  1.1.1.4  dyoung 
    518  1.1.1.4  dyoung 	for (i = 0; i < rs->rs_nrates; i++) {
    519  1.1.1.4  dyoung 		rs->rs_rates[i] &= IEEE80211_RATE_VAL;
    520  1.1.1.4  dyoung 		for (j = 0; j < basic[mode].rs_nrates; j++)
    521  1.1.1.4  dyoung 			if (basic[mode].rs_rates[j] == rs->rs_rates[i]) {
    522  1.1.1.4  dyoung 				rs->rs_rates[i] |= IEEE80211_RATE_BASIC;
    523  1.1.1.4  dyoung 				break;
    524  1.1.1.4  dyoung 			}
    525  1.1.1.4  dyoung 	}
    526  1.1.1.4  dyoung }
    527  1.1.1.4  dyoung 
    528  1.1.1.4  dyoung /*
    529  1.1.1.4  dyoung  * WME protocol support.  The following parameters come from the spec.
    530  1.1.1.4  dyoung  */
    531  1.1.1.4  dyoung typedef struct phyParamType {
    532  1.1.1.4  dyoung 	u_int8_t aifsn;
    533  1.1.1.4  dyoung 	u_int8_t logcwmin;
    534  1.1.1.4  dyoung 	u_int8_t logcwmax;
    535  1.1.1.4  dyoung 	u_int16_t txopLimit;
    536  1.1.1.4  dyoung 	u_int8_t acm;
    537  1.1.1.4  dyoung } paramType;
    538  1.1.1.4  dyoung 
    539  1.1.1.4  dyoung static const struct phyParamType phyParamForAC_BE[IEEE80211_MODE_MAX] = {
    540  1.1.1.4  dyoung 	{ 3, 4, 6 },		/* IEEE80211_MODE_AUTO */
    541  1.1.1.4  dyoung 	{ 3, 4, 6 },		/* IEEE80211_MODE_11A */
    542  1.1.1.4  dyoung 	{ 3, 5, 7 },		/* IEEE80211_MODE_11B */
    543  1.1.1.4  dyoung 	{ 3, 4, 6 },		/* IEEE80211_MODE_11G */
    544  1.1.1.4  dyoung 	{ 3, 5, 7 },		/* IEEE80211_MODE_FH */
    545  1.1.1.4  dyoung 	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_A */
    546  1.1.1.4  dyoung 	{ 2, 3, 5 },		/* IEEE80211_MODE_TURBO_G */
    547  1.1.1.4  dyoung };
    548  1.1.1.4  dyoung static const struct phyParamType phyParamForAC_BK[IEEE80211_MODE_MAX] = {
    549  1.1.1.4  dyoung 	{ 7, 4, 10 },		/* IEEE80211_MODE_AUTO */
    550  1.1.1.4  dyoung 	{ 7, 4, 10 },		/* IEEE80211_MODE_11A */
    551  1.1.1.4  dyoung 	{ 7, 5, 10 },		/* IEEE80211_MODE_11B */
    552  1.1.1.4  dyoung 	{ 7, 4, 10 },		/* IEEE80211_MODE_11G */
    553  1.1.1.4  dyoung 	{ 7, 5, 10 },		/* IEEE80211_MODE_FH */
    554  1.1.1.4  dyoung 	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
    555  1.1.1.4  dyoung 	{ 7, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
    556  1.1.1.4  dyoung };
    557  1.1.1.4  dyoung static const struct phyParamType phyParamForAC_VI[IEEE80211_MODE_MAX] = {
    558  1.1.1.4  dyoung 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
    559  1.1.1.4  dyoung 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11A */
    560  1.1.1.4  dyoung 	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_11B */
    561  1.1.1.4  dyoung 	{ 1, 3, 4,  94 },	/* IEEE80211_MODE_11G */
    562  1.1.1.4  dyoung 	{ 1, 4, 5, 188 },	/* IEEE80211_MODE_FH */
    563  1.1.1.4  dyoung 	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
    564  1.1.1.4  dyoung 	{ 1, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
    565  1.1.1.4  dyoung };
    566  1.1.1.4  dyoung static const struct phyParamType phyParamForAC_VO[IEEE80211_MODE_MAX] = {
    567  1.1.1.4  dyoung 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
    568  1.1.1.4  dyoung 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11A */
    569  1.1.1.4  dyoung 	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_11B */
    570  1.1.1.4  dyoung 	{ 1, 2, 3,  47 },	/* IEEE80211_MODE_11G */
    571  1.1.1.4  dyoung 	{ 1, 3, 4, 102 },	/* IEEE80211_MODE_FH */
    572  1.1.1.4  dyoung 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
    573  1.1.1.4  dyoung 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
    574  1.1.1.4  dyoung };
    575  1.1.1.4  dyoung 
    576  1.1.1.4  dyoung static const struct phyParamType bssPhyParamForAC_BE[IEEE80211_MODE_MAX] = {
    577  1.1.1.4  dyoung 	{ 3, 4, 10 },		/* IEEE80211_MODE_AUTO */
    578  1.1.1.4  dyoung 	{ 3, 4, 10 },		/* IEEE80211_MODE_11A */
    579  1.1.1.4  dyoung 	{ 3, 5, 10 },		/* IEEE80211_MODE_11B */
    580  1.1.1.4  dyoung 	{ 3, 4, 10 },		/* IEEE80211_MODE_11G */
    581  1.1.1.4  dyoung 	{ 3, 5, 10 },		/* IEEE80211_MODE_FH */
    582  1.1.1.4  dyoung 	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_A */
    583  1.1.1.4  dyoung 	{ 2, 3, 10 },		/* IEEE80211_MODE_TURBO_G */
    584  1.1.1.4  dyoung };
    585  1.1.1.4  dyoung static const struct phyParamType bssPhyParamForAC_VI[IEEE80211_MODE_MAX] = {
    586  1.1.1.4  dyoung 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_AUTO */
    587  1.1.1.4  dyoung 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11A */
    588  1.1.1.4  dyoung 	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_11B */
    589  1.1.1.4  dyoung 	{ 2, 3, 4,  94 },	/* IEEE80211_MODE_11G */
    590  1.1.1.4  dyoung 	{ 2, 4, 5, 188 },	/* IEEE80211_MODE_FH */
    591  1.1.1.4  dyoung 	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_A */
    592  1.1.1.4  dyoung 	{ 2, 2, 3,  94 },	/* IEEE80211_MODE_TURBO_G */
    593  1.1.1.4  dyoung };
    594  1.1.1.4  dyoung static const struct phyParamType bssPhyParamForAC_VO[IEEE80211_MODE_MAX] = {
    595  1.1.1.4  dyoung 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_AUTO */
    596  1.1.1.4  dyoung 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11A */
    597  1.1.1.4  dyoung 	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_11B */
    598  1.1.1.4  dyoung 	{ 2, 2, 3,  47 },	/* IEEE80211_MODE_11G */
    599  1.1.1.4  dyoung 	{ 2, 3, 4, 102 },	/* IEEE80211_MODE_FH */
    600  1.1.1.4  dyoung 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_A */
    601  1.1.1.4  dyoung 	{ 1, 2, 2,  47 },	/* IEEE80211_MODE_TURBO_G */
    602  1.1.1.4  dyoung };
    603  1.1.1.4  dyoung 
    604  1.1.1.4  dyoung void
    605  1.1.1.4  dyoung ieee80211_wme_initparams(struct ieee80211com *ic)
    606  1.1.1.4  dyoung {
    607  1.1.1.4  dyoung 	struct ieee80211_wme_state *wme = &ic->ic_wme;
    608  1.1.1.4  dyoung 	const paramType *pPhyParam, *pBssPhyParam;
    609  1.1.1.4  dyoung 	struct wmeParams *wmep;
    610  1.1.1.4  dyoung 	int i;
    611  1.1.1.4  dyoung 
    612  1.1.1.4  dyoung 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
    613  1.1.1.4  dyoung 		return;
    614  1.1.1.4  dyoung 
    615  1.1.1.4  dyoung 	for (i = 0; i < WME_NUM_AC; i++) {
    616  1.1.1.4  dyoung 		switch (i) {
    617  1.1.1.4  dyoung 		case WME_AC_BK:
    618  1.1.1.4  dyoung 			pPhyParam = &phyParamForAC_BK[ic->ic_curmode];
    619  1.1.1.4  dyoung 			pBssPhyParam = &phyParamForAC_BK[ic->ic_curmode];
    620  1.1.1.4  dyoung 			break;
    621  1.1.1.4  dyoung 		case WME_AC_VI:
    622  1.1.1.4  dyoung 			pPhyParam = &phyParamForAC_VI[ic->ic_curmode];
    623  1.1.1.4  dyoung 			pBssPhyParam = &bssPhyParamForAC_VI[ic->ic_curmode];
    624  1.1.1.4  dyoung 			break;
    625  1.1.1.4  dyoung 		case WME_AC_VO:
    626  1.1.1.4  dyoung 			pPhyParam = &phyParamForAC_VO[ic->ic_curmode];
    627  1.1.1.4  dyoung 			pBssPhyParam = &bssPhyParamForAC_VO[ic->ic_curmode];
    628  1.1.1.4  dyoung 			break;
    629  1.1.1.4  dyoung 		case WME_AC_BE:
    630  1.1.1.4  dyoung 		default:
    631  1.1.1.4  dyoung 			pPhyParam = &phyParamForAC_BE[ic->ic_curmode];
    632  1.1.1.4  dyoung 			pBssPhyParam = &bssPhyParamForAC_BE[ic->ic_curmode];
    633  1.1.1.4  dyoung 			break;
    634  1.1.1.4  dyoung 		}
    635  1.1.1.4  dyoung 
    636  1.1.1.4  dyoung 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
    637  1.1.1.4  dyoung 		if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    638  1.1.1.4  dyoung 			wmep->wmep_acm = pPhyParam->acm;
    639  1.1.1.4  dyoung 			wmep->wmep_aifsn = pPhyParam->aifsn;
    640  1.1.1.4  dyoung 			wmep->wmep_logcwmin = pPhyParam->logcwmin;
    641  1.1.1.4  dyoung 			wmep->wmep_logcwmax = pPhyParam->logcwmax;
    642  1.1.1.4  dyoung 			wmep->wmep_txopLimit = pPhyParam->txopLimit;
    643  1.1.1.4  dyoung 		} else {
    644  1.1.1.4  dyoung 			wmep->wmep_acm = pBssPhyParam->acm;
    645  1.1.1.4  dyoung 			wmep->wmep_aifsn = pBssPhyParam->aifsn;
    646  1.1.1.4  dyoung 			wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
    647  1.1.1.4  dyoung 			wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
    648  1.1.1.4  dyoung 			wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
    649  1.1.1.4  dyoung 
    650  1.1.1.4  dyoung 		}
    651  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    652  1.1.1.4  dyoung 			"%s: %s chan [acm %u aifsn %u log2(cwmin) %u "
    653  1.1.1.4  dyoung 			"log2(cwmax) %u txpoLimit %u]\n", __func__
    654  1.1.1.4  dyoung 			, ieee80211_wme_acnames[i]
    655  1.1.1.4  dyoung 			, wmep->wmep_acm
    656  1.1.1.4  dyoung 			, wmep->wmep_aifsn
    657  1.1.1.4  dyoung 			, wmep->wmep_logcwmin
    658  1.1.1.4  dyoung 			, wmep->wmep_logcwmax
    659  1.1.1.4  dyoung 			, wmep->wmep_txopLimit
    660  1.1.1.4  dyoung 		);
    661  1.1.1.4  dyoung 
    662  1.1.1.4  dyoung 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
    663  1.1.1.4  dyoung 		wmep->wmep_acm = pBssPhyParam->acm;
    664  1.1.1.4  dyoung 		wmep->wmep_aifsn = pBssPhyParam->aifsn;
    665  1.1.1.4  dyoung 		wmep->wmep_logcwmin = pBssPhyParam->logcwmin;
    666  1.1.1.4  dyoung 		wmep->wmep_logcwmax = pBssPhyParam->logcwmax;
    667  1.1.1.4  dyoung 		wmep->wmep_txopLimit = pBssPhyParam->txopLimit;
    668  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    669  1.1.1.4  dyoung 			"%s: %s  bss [acm %u aifsn %u log2(cwmin) %u "
    670  1.1.1.4  dyoung 			"log2(cwmax) %u txpoLimit %u]\n", __func__
    671  1.1.1.4  dyoung 			, ieee80211_wme_acnames[i]
    672  1.1.1.4  dyoung 			, wmep->wmep_acm
    673  1.1.1.4  dyoung 			, wmep->wmep_aifsn
    674  1.1.1.4  dyoung 			, wmep->wmep_logcwmin
    675  1.1.1.4  dyoung 			, wmep->wmep_logcwmax
    676  1.1.1.4  dyoung 			, wmep->wmep_txopLimit
    677  1.1.1.4  dyoung 		);
    678  1.1.1.4  dyoung 	}
    679  1.1.1.4  dyoung 	/* NB: check ic_bss to avoid NULL deref on initial attach */
    680  1.1.1.4  dyoung 	if (ic->ic_bss != NULL) {
    681  1.1.1.4  dyoung 		/*
    682  1.1.1.4  dyoung 		 * Calculate agressive mode switching threshold based
    683  1.1.1.4  dyoung 		 * on beacon interval.  This doesn't need locking since
    684  1.1.1.4  dyoung 		 * we're only called before entering the RUN state at
    685  1.1.1.4  dyoung 		 * which point we start sending beacon frames.
    686  1.1.1.4  dyoung 		 */
    687  1.1.1.4  dyoung 		wme->wme_hipri_switch_thresh =
    688  1.1.1.4  dyoung 			(HIGH_PRI_SWITCH_THRESH * ic->ic_bss->ni_intval) / 100;
    689  1.1.1.4  dyoung 		ieee80211_wme_updateparams(ic);
    690  1.1.1.4  dyoung 	}
    691  1.1.1.4  dyoung }
    692  1.1.1.4  dyoung 
    693  1.1.1.4  dyoung /*
    694  1.1.1.4  dyoung  * Update WME parameters for ourself and the BSS.
    695  1.1.1.4  dyoung  */
    696  1.1.1.4  dyoung void
    697  1.1.1.4  dyoung ieee80211_wme_updateparams_locked(struct ieee80211com *ic)
    698  1.1.1.4  dyoung {
    699  1.1.1.4  dyoung 	static const paramType phyParam[IEEE80211_MODE_MAX] = {
    700  1.1.1.4  dyoung 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_AUTO */
    701  1.1.1.4  dyoung 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11A */
    702  1.1.1.4  dyoung 		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_11B */
    703  1.1.1.4  dyoung 		{ 2, 4, 10, 64 },	/* IEEE80211_MODE_11G */
    704  1.1.1.4  dyoung 		{ 2, 5, 10, 64 },	/* IEEE80211_MODE_FH */
    705  1.1.1.4  dyoung 		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_A */
    706  1.1.1.4  dyoung 		{ 1, 3, 10, 64 },	/* IEEE80211_MODE_TURBO_G */
    707  1.1.1.4  dyoung 	};
    708  1.1.1.4  dyoung 	struct ieee80211_wme_state *wme = &ic->ic_wme;
    709  1.1.1.4  dyoung 	const struct wmeParams *wmep;
    710  1.1.1.4  dyoung 	struct wmeParams *chanp, *bssp;
    711  1.1.1.4  dyoung 	int i;
    712  1.1.1.4  dyoung 
    713  1.1.1.4  dyoung        	/* set up the channel access parameters for the physical device */
    714  1.1.1.4  dyoung 	for (i = 0; i < WME_NUM_AC; i++) {
    715  1.1.1.4  dyoung 		chanp = &wme->wme_chanParams.cap_wmeParams[i];
    716  1.1.1.4  dyoung 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[i];
    717  1.1.1.4  dyoung 		chanp->wmep_aifsn = wmep->wmep_aifsn;
    718  1.1.1.4  dyoung 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
    719  1.1.1.4  dyoung 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
    720  1.1.1.4  dyoung 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
    721  1.1.1.4  dyoung 
    722  1.1.1.4  dyoung 		chanp = &wme->wme_bssChanParams.cap_wmeParams[i];
    723  1.1.1.4  dyoung 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[i];
    724  1.1.1.4  dyoung 		chanp->wmep_aifsn = wmep->wmep_aifsn;
    725  1.1.1.4  dyoung 		chanp->wmep_logcwmin = wmep->wmep_logcwmin;
    726  1.1.1.4  dyoung 		chanp->wmep_logcwmax = wmep->wmep_logcwmax;
    727  1.1.1.4  dyoung 		chanp->wmep_txopLimit = wmep->wmep_txopLimit;
    728  1.1.1.4  dyoung 	}
    729  1.1.1.4  dyoung 
    730  1.1.1.4  dyoung 	/*
    731  1.1.1.4  dyoung 	 * This implements agressive mode as found in certain
    732  1.1.1.4  dyoung 	 * vendors' AP's.  When there is significant high
    733  1.1.1.4  dyoung 	 * priority (VI/VO) traffic in the BSS throttle back BE
    734  1.1.1.4  dyoung 	 * traffic by using conservative parameters.  Otherwise
    735  1.1.1.4  dyoung 	 * BE uses agressive params to optimize performance of
    736  1.1.1.4  dyoung 	 * legacy/non-QoS traffic.
    737  1.1.1.4  dyoung 	 */
    738  1.1.1.4  dyoung         if ((ic->ic_opmode == IEEE80211_M_HOSTAP &&
    739  1.1.1.4  dyoung 	     (wme->wme_flags & WME_F_AGGRMODE) == 0) ||
    740  1.1.1.4  dyoung 	    (ic->ic_opmode != IEEE80211_M_HOSTAP &&
    741  1.1.1.4  dyoung 	     (ic->ic_bss->ni_flags & IEEE80211_NODE_QOS) == 0) ||
    742  1.1.1.4  dyoung 	    (ic->ic_flags & IEEE80211_F_WME) == 0) {
    743  1.1.1.4  dyoung 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
    744  1.1.1.4  dyoung 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
    745  1.1.1.4  dyoung 
    746  1.1.1.4  dyoung 		chanp->wmep_aifsn = bssp->wmep_aifsn =
    747  1.1.1.4  dyoung 			phyParam[ic->ic_curmode].aifsn;
    748  1.1.1.4  dyoung 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
    749  1.1.1.4  dyoung 			phyParam[ic->ic_curmode].logcwmin;
    750  1.1.1.4  dyoung 		chanp->wmep_logcwmax = bssp->wmep_logcwmax =
    751  1.1.1.4  dyoung 			phyParam[ic->ic_curmode].logcwmax;
    752  1.1.1.4  dyoung 		chanp->wmep_txopLimit = bssp->wmep_txopLimit =
    753  1.1.1.4  dyoung 			(ic->ic_caps & IEEE80211_C_BURST) ?
    754  1.1.1.4  dyoung 				phyParam[ic->ic_curmode].txopLimit : 0;
    755  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    756  1.1.1.4  dyoung 			"%s: %s [acm %u aifsn %u log2(cwmin) %u "
    757  1.1.1.4  dyoung 			"log2(cwmax) %u txpoLimit %u]\n", __func__
    758  1.1.1.4  dyoung 			, ieee80211_wme_acnames[WME_AC_BE]
    759  1.1.1.4  dyoung 			, chanp->wmep_acm
    760  1.1.1.4  dyoung 			, chanp->wmep_aifsn
    761  1.1.1.4  dyoung 			, chanp->wmep_logcwmin
    762  1.1.1.4  dyoung 			, chanp->wmep_logcwmax
    763  1.1.1.4  dyoung 			, chanp->wmep_txopLimit
    764  1.1.1.4  dyoung 		);
    765  1.1.1.4  dyoung 	}
    766  1.1.1.4  dyoung 
    767  1.1.1.4  dyoung 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
    768  1.1.1.4  dyoung 	    ic->ic_sta_assoc < 2 && (wme->wme_flags & WME_F_AGGRMODE) == 0) {
    769  1.1.1.4  dyoung         	static const u_int8_t logCwMin[IEEE80211_MODE_MAX] = {
    770  1.1.1.4  dyoung               		3,	/* IEEE80211_MODE_AUTO */
    771  1.1.1.4  dyoung               		3,	/* IEEE80211_MODE_11A */
    772  1.1.1.4  dyoung               		4,	/* IEEE80211_MODE_11B */
    773  1.1.1.4  dyoung               		3,	/* IEEE80211_MODE_11G */
    774  1.1.1.4  dyoung               		4,	/* IEEE80211_MODE_FH */
    775  1.1.1.4  dyoung               		3,	/* IEEE80211_MODE_TURBO_A */
    776  1.1.1.4  dyoung               		3,	/* IEEE80211_MODE_TURBO_G */
    777  1.1.1.4  dyoung 		};
    778  1.1.1.4  dyoung 		chanp = &wme->wme_chanParams.cap_wmeParams[WME_AC_BE];
    779  1.1.1.4  dyoung 		bssp = &wme->wme_bssChanParams.cap_wmeParams[WME_AC_BE];
    780  1.1.1.4  dyoung 
    781  1.1.1.4  dyoung 		chanp->wmep_logcwmin = bssp->wmep_logcwmin =
    782  1.1.1.4  dyoung 			logCwMin[ic->ic_curmode];
    783  1.1.1.4  dyoung 		IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    784  1.1.1.4  dyoung 			"%s: %s log2(cwmin) %u\n", __func__
    785  1.1.1.4  dyoung 			, ieee80211_wme_acnames[WME_AC_BE]
    786  1.1.1.4  dyoung 			, chanp->wmep_logcwmin
    787  1.1.1.4  dyoung 		);
    788  1.1.1.4  dyoung     	}
    789  1.1.1.4  dyoung 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {	/* XXX ibss? */
    790  1.1.1.4  dyoung 		/*
    791  1.1.1.4  dyoung 		 * Arrange for a beacon update and bump the parameter
    792  1.1.1.4  dyoung 		 * set number so associated stations load the new values.
    793  1.1.1.4  dyoung 		 */
    794  1.1.1.4  dyoung 		wme->wme_bssChanParams.cap_info =
    795  1.1.1.4  dyoung 			(wme->wme_bssChanParams.cap_info+1) & WME_QOSINFO_COUNT;
    796  1.1.1.4  dyoung 		ic->ic_flags |= IEEE80211_F_WMEUPDATE;
    797  1.1.1.4  dyoung 	}
    798  1.1.1.4  dyoung 
    799  1.1.1.4  dyoung 	wme->wme_update(ic);
    800  1.1.1.4  dyoung 
    801  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_WME,
    802  1.1.1.4  dyoung 		"%s: WME params updated, cap_info 0x%x\n", __func__,
    803  1.1.1.4  dyoung 		ic->ic_opmode == IEEE80211_M_STA ?
    804  1.1.1.4  dyoung 			wme->wme_wmeChanParams.cap_info :
    805  1.1.1.4  dyoung 			wme->wme_bssChanParams.cap_info);
    806  1.1.1.4  dyoung }
    807  1.1.1.4  dyoung 
    808  1.1.1.4  dyoung void
    809  1.1.1.4  dyoung ieee80211_wme_updateparams(struct ieee80211com *ic)
    810  1.1.1.4  dyoung {
    811  1.1.1.4  dyoung 
    812  1.1.1.4  dyoung 	if (ic->ic_caps & IEEE80211_C_WME) {
    813  1.1.1.4  dyoung 		IEEE80211_BEACON_LOCK(ic);
    814  1.1.1.4  dyoung 		ieee80211_wme_updateparams_locked(ic);
    815  1.1.1.4  dyoung 		IEEE80211_BEACON_UNLOCK(ic);
    816  1.1.1.4  dyoung 	}
    817  1.1.1.4  dyoung }
    818  1.1.1.4  dyoung 
    819  1.1.1.5  dyoung static void
    820  1.1.1.5  dyoung sta_disassoc(void *arg, struct ieee80211_node *ni)
    821  1.1.1.5  dyoung {
    822  1.1.1.5  dyoung 	struct ieee80211com *ic = arg;
    823  1.1.1.5  dyoung 
    824  1.1.1.5  dyoung 	if (ni->ni_associd != 0) {
    825  1.1.1.5  dyoung 		IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
    826  1.1.1.5  dyoung 			IEEE80211_REASON_ASSOC_LEAVE);
    827  1.1.1.5  dyoung 		ieee80211_node_leave(ic, ni);
    828  1.1.1.5  dyoung 	}
    829  1.1.1.5  dyoung }
    830  1.1.1.5  dyoung 
    831  1.1.1.5  dyoung static void
    832  1.1.1.5  dyoung sta_deauth(void *arg, struct ieee80211_node *ni)
    833  1.1.1.5  dyoung {
    834  1.1.1.5  dyoung 	struct ieee80211com *ic = arg;
    835  1.1.1.5  dyoung 
    836  1.1.1.5  dyoung 	IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
    837  1.1.1.5  dyoung 		IEEE80211_REASON_ASSOC_LEAVE);
    838  1.1.1.5  dyoung }
    839  1.1.1.5  dyoung 
    840      1.1  dyoung static int
    841  1.1.1.4  dyoung ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
    842      1.1  dyoung {
    843  1.1.1.4  dyoung 	struct ifnet *ifp = ic->ic_ifp;
    844      1.1  dyoung 	struct ieee80211_node *ni;
    845      1.1  dyoung 	enum ieee80211_state ostate;
    846      1.1  dyoung 
    847      1.1  dyoung 	ostate = ic->ic_state;
    848  1.1.1.4  dyoung 	IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE, "%s: %s -> %s\n", __func__,
    849  1.1.1.4  dyoung 		ieee80211_state_name[ostate], ieee80211_state_name[nstate]);
    850      1.1  dyoung 	ic->ic_state = nstate;			/* state transition */
    851      1.1  dyoung 	ni = ic->ic_bss;			/* NB: no reference held */
    852      1.1  dyoung 	switch (nstate) {
    853      1.1  dyoung 	case IEEE80211_S_INIT:
    854      1.1  dyoung 		switch (ostate) {
    855      1.1  dyoung 		case IEEE80211_S_INIT:
    856      1.1  dyoung 			break;
    857      1.1  dyoung 		case IEEE80211_S_RUN:
    858      1.1  dyoung 			switch (ic->ic_opmode) {
    859      1.1  dyoung 			case IEEE80211_M_STA:
    860      1.1  dyoung 				IEEE80211_SEND_MGMT(ic, ni,
    861      1.1  dyoung 				    IEEE80211_FC0_SUBTYPE_DISASSOC,
    862      1.1  dyoung 				    IEEE80211_REASON_ASSOC_LEAVE);
    863  1.1.1.4  dyoung 				ieee80211_sta_leave(ic, ni);
    864      1.1  dyoung 				break;
    865      1.1  dyoung 			case IEEE80211_M_HOSTAP:
    866  1.1.1.5  dyoung 				ieee80211_iterate_nodes(&ic->ic_sta,
    867  1.1.1.5  dyoung 					sta_disassoc, ic);
    868      1.1  dyoung 				break;
    869      1.1  dyoung 			default:
    870      1.1  dyoung 				break;
    871      1.1  dyoung 			}
    872  1.1.1.4  dyoung 			goto reset;
    873      1.1  dyoung 		case IEEE80211_S_ASSOC:
    874      1.1  dyoung 			switch (ic->ic_opmode) {
    875      1.1  dyoung 			case IEEE80211_M_STA:
    876      1.1  dyoung 				IEEE80211_SEND_MGMT(ic, ni,
    877      1.1  dyoung 				    IEEE80211_FC0_SUBTYPE_DEAUTH,
    878      1.1  dyoung 				    IEEE80211_REASON_AUTH_LEAVE);
    879      1.1  dyoung 				break;
    880      1.1  dyoung 			case IEEE80211_M_HOSTAP:
    881  1.1.1.5  dyoung 				ieee80211_iterate_nodes(&ic->ic_sta,
    882  1.1.1.5  dyoung 					sta_deauth, ic);
    883      1.1  dyoung 				break;
    884      1.1  dyoung 			default:
    885      1.1  dyoung 				break;
    886      1.1  dyoung 			}
    887  1.1.1.4  dyoung 			goto reset;
    888      1.1  dyoung 		case IEEE80211_S_SCAN:
    889  1.1.1.4  dyoung 			ieee80211_cancel_scan(ic);
    890  1.1.1.4  dyoung 			goto reset;
    891  1.1.1.4  dyoung 		case IEEE80211_S_AUTH:
    892  1.1.1.4  dyoung 		reset:
    893      1.1  dyoung 			ic->ic_mgt_timer = 0;
    894      1.1  dyoung 			IF_DRAIN(&ic->ic_mgtq);
    895  1.1.1.4  dyoung 			ieee80211_reset_bss(ic);
    896      1.1  dyoung 			break;
    897      1.1  dyoung 		}
    898  1.1.1.4  dyoung 		if (ic->ic_auth->ia_detach != NULL)
    899  1.1.1.4  dyoung 			ic->ic_auth->ia_detach(ic);
    900      1.1  dyoung 		break;
    901      1.1  dyoung 	case IEEE80211_S_SCAN:
    902      1.1  dyoung 		switch (ostate) {
    903      1.1  dyoung 		case IEEE80211_S_INIT:
    904  1.1.1.4  dyoung 			if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
    905  1.1.1.4  dyoung 			     ic->ic_opmode == IEEE80211_M_IBSS ||
    906  1.1.1.4  dyoung 			     ic->ic_opmode == IEEE80211_M_AHDEMO) &&
    907      1.1  dyoung 			    ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
    908      1.1  dyoung 				/*
    909      1.1  dyoung 				 * AP operation and we already have a channel;
    910      1.1  dyoung 				 * bypass the scan and startup immediately.
    911      1.1  dyoung 				 */
    912      1.1  dyoung 				ieee80211_create_ibss(ic, ic->ic_des_chan);
    913      1.1  dyoung 			} else {
    914  1.1.1.4  dyoung 				ieee80211_begin_scan(ic, arg);
    915      1.1  dyoung 			}
    916      1.1  dyoung 			break;
    917      1.1  dyoung 		case IEEE80211_S_SCAN:
    918  1.1.1.4  dyoung 			/*
    919  1.1.1.4  dyoung 			 * Scan next. If doing an active scan and the
    920  1.1.1.4  dyoung 			 * channel is not marked passive-only then send
    921  1.1.1.4  dyoung 			 * a probe request.  Otherwise just listen for
    922  1.1.1.4  dyoung 			 * beacons on the channel.
    923  1.1.1.4  dyoung 			 */
    924  1.1.1.4  dyoung 			if ((ic->ic_flags & IEEE80211_F_ASCAN) &&
    925  1.1.1.6   skrll 			    (ic->ic_curchan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) {
    926  1.1.1.6   skrll 				ieee80211_send_probereq(ni,
    927  1.1.1.6   skrll 					ic->ic_myaddr, ifp->if_broadcastaddr,
    928  1.1.1.6   skrll 					ifp->if_broadcastaddr,
    929  1.1.1.6   skrll 					ic->ic_des_essid, ic->ic_des_esslen,
    930  1.1.1.6   skrll 					ic->ic_opt_ie, ic->ic_opt_ie_len);
    931      1.1  dyoung 			}
    932      1.1  dyoung 			break;
    933      1.1  dyoung 		case IEEE80211_S_RUN:
    934      1.1  dyoung 			/* beacon miss */
    935  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_STATE,
    936  1.1.1.4  dyoung 				"no recent beacons from %s; rescanning\n",
    937  1.1.1.4  dyoung 				ether_sprintf(ic->ic_bss->ni_bssid));
    938  1.1.1.4  dyoung 			ieee80211_sta_leave(ic, ni);
    939  1.1.1.4  dyoung 			ic->ic_flags &= ~IEEE80211_F_SIBSS;	/* XXX */
    940      1.1  dyoung 			/* FALLTHRU */
    941      1.1  dyoung 		case IEEE80211_S_AUTH:
    942      1.1  dyoung 		case IEEE80211_S_ASSOC:
    943      1.1  dyoung 			/* timeout restart scan */
    944  1.1.1.4  dyoung 			ni = ieee80211_find_node(&ic->ic_scan,
    945  1.1.1.4  dyoung 				ic->ic_bss->ni_macaddr);
    946      1.1  dyoung 			if (ni != NULL) {
    947      1.1  dyoung 				ni->ni_fails++;
    948      1.1  dyoung 				ieee80211_unref_node(&ni);
    949      1.1  dyoung 			}
    950  1.1.1.5  dyoung 			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO)
    951  1.1.1.5  dyoung 				ieee80211_begin_scan(ic, arg);
    952      1.1  dyoung 			break;
    953      1.1  dyoung 		}
    954      1.1  dyoung 		break;
    955      1.1  dyoung 	case IEEE80211_S_AUTH:
    956      1.1  dyoung 		switch (ostate) {
    957      1.1  dyoung 		case IEEE80211_S_INIT:
    958      1.1  dyoung 		case IEEE80211_S_SCAN:
    959      1.1  dyoung 			IEEE80211_SEND_MGMT(ic, ni,
    960      1.1  dyoung 			    IEEE80211_FC0_SUBTYPE_AUTH, 1);
    961      1.1  dyoung 			break;
    962      1.1  dyoung 		case IEEE80211_S_AUTH:
    963      1.1  dyoung 		case IEEE80211_S_ASSOC:
    964  1.1.1.4  dyoung 			switch (arg) {
    965      1.1  dyoung 			case IEEE80211_FC0_SUBTYPE_AUTH:
    966      1.1  dyoung 				/* ??? */
    967      1.1  dyoung 				IEEE80211_SEND_MGMT(ic, ni,
    968      1.1  dyoung 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
    969      1.1  dyoung 				break;
    970      1.1  dyoung 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
    971      1.1  dyoung 				/* ignore and retry scan on timeout */
    972      1.1  dyoung 				break;
    973      1.1  dyoung 			}
    974      1.1  dyoung 			break;
    975      1.1  dyoung 		case IEEE80211_S_RUN:
    976  1.1.1.4  dyoung 			switch (arg) {
    977      1.1  dyoung 			case IEEE80211_FC0_SUBTYPE_AUTH:
    978      1.1  dyoung 				IEEE80211_SEND_MGMT(ic, ni,
    979      1.1  dyoung 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
    980      1.1  dyoung 				ic->ic_state = ostate;	/* stay RUN */
    981      1.1  dyoung 				break;
    982      1.1  dyoung 			case IEEE80211_FC0_SUBTYPE_DEAUTH:
    983  1.1.1.4  dyoung 				ieee80211_sta_leave(ic, ni);
    984  1.1.1.5  dyoung 				if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
    985  1.1.1.5  dyoung 					/* try to reauth */
    986  1.1.1.5  dyoung 					IEEE80211_SEND_MGMT(ic, ni,
    987  1.1.1.5  dyoung 					    IEEE80211_FC0_SUBTYPE_AUTH, 1);
    988  1.1.1.5  dyoung 				}
    989      1.1  dyoung 				break;
    990      1.1  dyoung 			}
    991      1.1  dyoung 			break;
    992      1.1  dyoung 		}
    993      1.1  dyoung 		break;
    994      1.1  dyoung 	case IEEE80211_S_ASSOC:
    995      1.1  dyoung 		switch (ostate) {
    996      1.1  dyoung 		case IEEE80211_S_INIT:
    997      1.1  dyoung 		case IEEE80211_S_SCAN:
    998      1.1  dyoung 		case IEEE80211_S_ASSOC:
    999  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
   1000  1.1.1.4  dyoung 				"%s: invalid transition\n", __func__);
   1001      1.1  dyoung 			break;
   1002      1.1  dyoung 		case IEEE80211_S_AUTH:
   1003      1.1  dyoung 			IEEE80211_SEND_MGMT(ic, ni,
   1004      1.1  dyoung 			    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
   1005      1.1  dyoung 			break;
   1006      1.1  dyoung 		case IEEE80211_S_RUN:
   1007  1.1.1.4  dyoung 			ieee80211_sta_leave(ic, ni);
   1008  1.1.1.5  dyoung 			if (ic->ic_roaming == IEEE80211_ROAMING_AUTO) {
   1009  1.1.1.5  dyoung 				IEEE80211_SEND_MGMT(ic, ni,
   1010  1.1.1.5  dyoung 				    IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
   1011  1.1.1.5  dyoung 			}
   1012      1.1  dyoung 			break;
   1013      1.1  dyoung 		}
   1014      1.1  dyoung 		break;
   1015      1.1  dyoung 	case IEEE80211_S_RUN:
   1016  1.1.1.4  dyoung 		if (ic->ic_flags & IEEE80211_F_WPA) {
   1017  1.1.1.4  dyoung 			/* XXX validate prerequisites */
   1018  1.1.1.4  dyoung 		}
   1019      1.1  dyoung 		switch (ostate) {
   1020      1.1  dyoung 		case IEEE80211_S_INIT:
   1021  1.1.1.4  dyoung 			if (ic->ic_opmode == IEEE80211_M_MONITOR)
   1022  1.1.1.4  dyoung 				break;
   1023  1.1.1.4  dyoung 			/* fall thru... */
   1024      1.1  dyoung 		case IEEE80211_S_AUTH:
   1025  1.1.1.4  dyoung 			IEEE80211_DPRINTF(ic, IEEE80211_MSG_ANY,
   1026  1.1.1.4  dyoung 				"%s: invalid transition\n", __func__);
   1027  1.1.1.4  dyoung 			/* fall thru... */
   1028      1.1  dyoung 		case IEEE80211_S_RUN:
   1029      1.1  dyoung 			break;
   1030      1.1  dyoung 		case IEEE80211_S_SCAN:		/* adhoc/hostap mode */
   1031      1.1  dyoung 		case IEEE80211_S_ASSOC:		/* infra mode */
   1032      1.1  dyoung 			KASSERT(ni->ni_txrate < ni->ni_rates.rs_nrates,
   1033      1.1  dyoung 				("%s: bogus xmit rate %u setup\n", __func__,
   1034      1.1  dyoung 					ni->ni_txrate));
   1035  1.1.1.4  dyoung #ifdef IEEE80211_DEBUG
   1036  1.1.1.4  dyoung 			if (ieee80211_msg_debug(ic)) {
   1037      1.1  dyoung 				if (ic->ic_opmode == IEEE80211_M_STA)
   1038  1.1.1.4  dyoung 					if_printf(ifp, "associated ");
   1039      1.1  dyoung 				else
   1040  1.1.1.4  dyoung 					if_printf(ifp, "synchronized ");
   1041      1.1  dyoung 				printf("with %s ssid ",
   1042      1.1  dyoung 				    ether_sprintf(ni->ni_bssid));
   1043      1.1  dyoung 				ieee80211_print_essid(ic->ic_bss->ni_essid,
   1044      1.1  dyoung 				    ni->ni_esslen);
   1045      1.1  dyoung 				printf(" channel %d start %uMb\n",
   1046  1.1.1.6   skrll 					ieee80211_chan2ieee(ic, ic->ic_curchan),
   1047      1.1  dyoung 					IEEE80211_RATE2MBS(ni->ni_rates.rs_rates[ni->ni_txrate]));
   1048      1.1  dyoung 			}
   1049  1.1.1.4  dyoung #endif
   1050      1.1  dyoung 			ic->ic_mgt_timer = 0;
   1051  1.1.1.4  dyoung 			if (ic->ic_opmode == IEEE80211_M_STA)
   1052  1.1.1.4  dyoung 				ieee80211_notify_node_join(ic, ni,
   1053  1.1.1.4  dyoung 					arg == IEEE80211_FC0_SUBTYPE_ASSOC_RESP);
   1054  1.1.1.4  dyoung 			if_start(ifp);		/* XXX not authorized yet */
   1055      1.1  dyoung 			break;
   1056      1.1  dyoung 		}
   1057  1.1.1.4  dyoung 		/*
   1058  1.1.1.4  dyoung 		 * Start/stop the authenticator when operating as an
   1059  1.1.1.4  dyoung 		 * AP.  We delay until here to allow configuration to
   1060  1.1.1.4  dyoung 		 * happen out of order.
   1061  1.1.1.4  dyoung 		 */
   1062  1.1.1.4  dyoung 		if (ic->ic_opmode == IEEE80211_M_HOSTAP && /* XXX IBSS/AHDEMO */
   1063  1.1.1.4  dyoung 		    ic->ic_auth->ia_attach != NULL) {
   1064  1.1.1.4  dyoung 			/* XXX check failure */
   1065  1.1.1.4  dyoung 			ic->ic_auth->ia_attach(ic);
   1066  1.1.1.4  dyoung 		} else if (ic->ic_auth->ia_detach != NULL) {
   1067  1.1.1.4  dyoung 			ic->ic_auth->ia_detach(ic);
   1068  1.1.1.4  dyoung 		}
   1069  1.1.1.4  dyoung 		/*
   1070  1.1.1.4  dyoung 		 * When 802.1x is not in use mark the port authorized
   1071  1.1.1.4  dyoung 		 * at this point so traffic can flow.
   1072  1.1.1.4  dyoung 		 */
   1073  1.1.1.4  dyoung 		if (ni->ni_authmode != IEEE80211_AUTH_8021X)
   1074  1.1.1.6   skrll 			ieee80211_node_authorize(ni);
   1075  1.1.1.4  dyoung 		/*
   1076  1.1.1.4  dyoung 		 * Enable inactivity processing.
   1077  1.1.1.4  dyoung 		 * XXX
   1078  1.1.1.4  dyoung 		 */
   1079  1.1.1.4  dyoung 		ic->ic_scan.nt_inact_timer = IEEE80211_INACT_WAIT;
   1080  1.1.1.4  dyoung 		ic->ic_sta.nt_inact_timer = IEEE80211_INACT_WAIT;
   1081      1.1  dyoung 		break;
   1082      1.1  dyoung 	}
   1083      1.1  dyoung 	return 0;
   1084      1.1  dyoung }
   1085