Home | History | Annotate | Line # | Download | only in net80211
ieee80211_ioctl.c revision 1.60.18.7
      1  1.60.18.7      phil /*	$NetBSD: ieee80211_ioctl.c,v 1.60.18.7 2018/08/15 17:07:02 phil Exp $ */
      2  1.60.18.2      phil 
      3        1.1    dyoung /*-
      4  1.60.18.1      phil  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
      5  1.60.18.1      phil  *
      6        1.1    dyoung  * Copyright (c) 2001 Atsushi Onoe
      7  1.60.18.1      phil  * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
      8        1.1    dyoung  * All rights reserved.
      9        1.1    dyoung  *
     10        1.1    dyoung  * Redistribution and use in source and binary forms, with or without
     11        1.1    dyoung  * modification, are permitted provided that the following conditions
     12        1.1    dyoung  * are met:
     13        1.1    dyoung  * 1. Redistributions of source code must retain the above copyright
     14        1.1    dyoung  *    notice, this list of conditions and the following disclaimer.
     15        1.1    dyoung  * 2. Redistributions in binary form must reproduce the above copyright
     16        1.1    dyoung  *    notice, this list of conditions and the following disclaimer in the
     17        1.1    dyoung  *    documentation and/or other materials provided with the distribution.
     18        1.1    dyoung  *
     19        1.1    dyoung  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20        1.1    dyoung  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21        1.1    dyoung  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22        1.1    dyoung  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23        1.1    dyoung  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24        1.1    dyoung  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25        1.1    dyoung  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26        1.1    dyoung  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27        1.1    dyoung  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28        1.1    dyoung  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29        1.1    dyoung  */
     30        1.1    dyoung 
     31        1.1    dyoung #include <sys/cdefs.h>
     32  1.60.18.2      phil #if __FreeBSD__
     33  1.60.18.1      phil __FBSDID("$FreeBSD$");
     34  1.60.18.2      phil #endif
     35        1.1    dyoung 
     36        1.1    dyoung /*
     37        1.1    dyoung  * IEEE 802.11 ioctl support (FreeBSD-specific)
     38        1.1    dyoung  */
     39        1.1    dyoung 
     40       1.10    dyoung #include "opt_inet.h"
     41  1.60.18.1      phil #include "opt_wlan.h"
     42       1.10    dyoung 
     43        1.1    dyoung #include <sys/endian.h>
     44        1.1    dyoung #include <sys/param.h>
     45        1.1    dyoung #include <sys/kernel.h>
     46  1.60.18.1      phil #include <sys/malloc.h>
     47  1.60.18.2      phil #if __FreeBSD__
     48  1.60.18.1      phil #include <sys/priv.h>
     49  1.60.18.2      phil #endif
     50        1.1    dyoung #include <sys/socket.h>
     51        1.1    dyoung #include <sys/sockio.h>
     52        1.1    dyoung #include <sys/systm.h>
     53       1.19    dyoung 
     54        1.1    dyoung #include <net/if.h>
     55  1.60.18.2      phil #if __FreeBSD__
     56  1.60.18.1      phil #include <net/if_var.h>
     57  1.60.18.2      phil #endif
     58  1.60.18.1      phil #include <net/if_dl.h>
     59        1.1    dyoung #include <net/if_media.h>
     60  1.60.18.2      phil #if __FreeBSD__
     61  1.60.18.1      phil #include <net/ethernet.h>
     62  1.60.18.2      phil #endif
     63  1.60.18.2      phil #if __NetBSD__
     64  1.60.18.2      phil #include <net/if_ether.h>
     65  1.60.18.2      phil #endif
     66        1.1    dyoung 
     67       1.10    dyoung #ifdef INET
     68       1.10    dyoung #include <netinet/in.h>
     69  1.60.18.2      phil #if __FreeBSD__
     70  1.60.18.1      phil #include <netinet/if_ether.h>
     71       1.10    dyoung #endif
     72  1.60.18.2      phil #if __NetBSD__
     73  1.60.18.2      phil #include <netinet/if_inarp.h>
     74  1.60.18.2      phil #endif
     75  1.60.18.2      phil #endif
     76       1.10    dyoung 
     77        1.1    dyoung #include <net80211/ieee80211_var.h>
     78        1.1    dyoung #include <net80211/ieee80211_ioctl.h>
     79  1.60.18.1      phil #include <net80211/ieee80211_regdomain.h>
     80  1.60.18.1      phil #include <net80211/ieee80211_input.h>
     81        1.1    dyoung 
     82  1.60.18.2      phil #ifdef __NetBSD__
     83  1.60.18.2      phil #undef  KASSERT
     84  1.60.18.2      phil #define KASSERT(__cond, __complaint) FBSDKASSERT(__cond, __complaint)
     85  1.60.18.2      phil #endif
     86  1.60.18.2      phil 
     87  1.60.18.1      phil #define	IS_UP_AUTO(_vap) \
     88  1.60.18.1      phil 	(IFNET_IS_UP_RUNNING((_vap)->iv_ifp) && \
     89  1.60.18.1      phil 	 (_vap)->iv_roaming == IEEE80211_ROAMING_AUTO)
     90       1.19    dyoung 
     91  1.60.18.1      phil static const uint8_t zerobssid[IEEE80211_ADDR_LEN];
     92  1.60.18.1      phil static struct ieee80211_channel *findchannel(struct ieee80211com *,
     93  1.60.18.1      phil 		int ieee, int mode);
     94  1.60.18.1      phil static int ieee80211_scanreq(struct ieee80211vap *,
     95  1.60.18.1      phil 		struct ieee80211_scan_req *);
     96       1.19    dyoung 
     97       1.19    dyoung static int
     98  1.60.18.1      phil ieee80211_ioctl_getkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
     99       1.19    dyoung {
    100  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    101       1.19    dyoung 	struct ieee80211_node *ni;
    102       1.19    dyoung 	struct ieee80211req_key ik;
    103       1.19    dyoung 	struct ieee80211_key *wk;
    104       1.19    dyoung 	const struct ieee80211_cipher *cip;
    105       1.19    dyoung 	u_int kid;
    106       1.19    dyoung 	int error;
    107       1.19    dyoung 
    108       1.19    dyoung 	if (ireq->i_len != sizeof(ik))
    109       1.19    dyoung 		return EINVAL;
    110       1.19    dyoung 	error = copyin(ireq->i_data, &ik, sizeof(ik));
    111       1.19    dyoung 	if (error)
    112       1.19    dyoung 		return error;
    113       1.19    dyoung 	kid = ik.ik_keyix;
    114       1.19    dyoung 	if (kid == IEEE80211_KEYIX_NONE) {
    115  1.60.18.1      phil 		ni = ieee80211_find_vap_node(&ic->ic_sta, vap, ik.ik_macaddr);
    116       1.19    dyoung 		if (ni == NULL)
    117  1.60.18.1      phil 			return ENOENT;
    118       1.19    dyoung 		wk = &ni->ni_ucastkey;
    119       1.19    dyoung 	} else {
    120       1.19    dyoung 		if (kid >= IEEE80211_WEP_NKID)
    121       1.19    dyoung 			return EINVAL;
    122  1.60.18.1      phil 		wk = &vap->iv_nw_keys[kid];
    123  1.60.18.1      phil 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, vap->iv_bss->ni_macaddr);
    124       1.19    dyoung 		ni = NULL;
    125       1.19    dyoung 	}
    126       1.19    dyoung 	cip = wk->wk_cipher;
    127       1.19    dyoung 	ik.ik_type = cip->ic_cipher;
    128       1.19    dyoung 	ik.ik_keylen = wk->wk_keylen;
    129       1.19    dyoung 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
    130  1.60.18.1      phil 	if (wk->wk_keyix == vap->iv_def_txkey)
    131       1.19    dyoung 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
    132  1.60.18.1      phil 	if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
    133       1.19    dyoung 		/* NB: only root can read key data */
    134  1.60.18.1      phil 		ik.ik_keyrsc = wk->wk_keyrsc[IEEE80211_NONQOS_TID];
    135       1.19    dyoung 		ik.ik_keytsc = wk->wk_keytsc;
    136       1.19    dyoung 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
    137       1.19    dyoung 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
    138       1.19    dyoung 			memcpy(ik.ik_keydata+wk->wk_keylen,
    139       1.19    dyoung 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
    140       1.19    dyoung 				IEEE80211_MICBUF_SIZE);
    141       1.19    dyoung 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
    142       1.19    dyoung 		}
    143       1.19    dyoung 	} else {
    144       1.19    dyoung 		ik.ik_keyrsc = 0;
    145       1.19    dyoung 		ik.ik_keytsc = 0;
    146       1.19    dyoung 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
    147       1.19    dyoung 	}
    148       1.19    dyoung 	if (ni != NULL)
    149       1.19    dyoung 		ieee80211_free_node(ni);
    150       1.19    dyoung 	return copyout(&ik, ireq->i_data, sizeof(ik));
    151       1.19    dyoung }
    152       1.19    dyoung 
    153       1.19    dyoung static int
    154  1.60.18.1      phil ieee80211_ioctl_getchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
    155       1.19    dyoung {
    156  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    157       1.19    dyoung 
    158  1.60.18.1      phil 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
    159  1.60.18.1      phil 		ireq->i_len = sizeof(ic->ic_chan_active);
    160  1.60.18.1      phil 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
    161       1.19    dyoung }
    162       1.19    dyoung 
    163       1.19    dyoung static int
    164  1.60.18.1      phil ieee80211_ioctl_getchaninfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
    165       1.19    dyoung {
    166  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    167  1.60.18.1      phil 	uint32_t space;
    168       1.35  christos 
    169  1.60.18.1      phil 	space = __offsetof(struct ieee80211req_chaninfo,
    170  1.60.18.1      phil 			ic_chans[ic->ic_nchans]);
    171       1.19    dyoung 	if (space > ireq->i_len)
    172       1.19    dyoung 		space = ireq->i_len;
    173  1.60.18.1      phil 	/* XXX assumes compatible layout */
    174  1.60.18.1      phil 	return copyout(&ic->ic_nchans, ireq->i_data, space);
    175       1.19    dyoung }
    176       1.19    dyoung 
    177       1.19    dyoung static int
    178  1.60.18.1      phil ieee80211_ioctl_getwpaie(struct ieee80211vap *vap,
    179  1.60.18.1      phil 	struct ieee80211req *ireq, int req)
    180       1.19    dyoung {
    181       1.19    dyoung 	struct ieee80211_node *ni;
    182  1.60.18.1      phil 	struct ieee80211req_wpaie2 *wpaie;
    183       1.19    dyoung 	int error;
    184       1.19    dyoung 
    185       1.19    dyoung 	if (ireq->i_len < IEEE80211_ADDR_LEN)
    186       1.19    dyoung 		return EINVAL;
    187  1.60.18.1      phil 	wpaie = IEEE80211_MALLOC(sizeof(*wpaie), M_TEMP,
    188  1.60.18.1      phil 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    189  1.60.18.1      phil 	if (wpaie == NULL)
    190  1.60.18.1      phil 		return ENOMEM;
    191  1.60.18.1      phil 	error = copyin(ireq->i_data, wpaie->wpa_macaddr, IEEE80211_ADDR_LEN);
    192       1.19    dyoung 	if (error != 0)
    193  1.60.18.1      phil 		goto bad;
    194  1.60.18.1      phil 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, wpaie->wpa_macaddr);
    195  1.60.18.1      phil 	if (ni == NULL) {
    196  1.60.18.1      phil 		error = ENOENT;
    197  1.60.18.1      phil 		goto bad;
    198  1.60.18.1      phil 	}
    199  1.60.18.1      phil 	if (ni->ni_ies.wpa_ie != NULL) {
    200  1.60.18.1      phil 		int ielen = ni->ni_ies.wpa_ie[1] + 2;
    201  1.60.18.1      phil 		if (ielen > sizeof(wpaie->wpa_ie))
    202  1.60.18.1      phil 			ielen = sizeof(wpaie->wpa_ie);
    203  1.60.18.1      phil 		memcpy(wpaie->wpa_ie, ni->ni_ies.wpa_ie, ielen);
    204  1.60.18.1      phil 	}
    205  1.60.18.1      phil 	if (req == IEEE80211_IOC_WPAIE2) {
    206  1.60.18.1      phil 		if (ni->ni_ies.rsn_ie != NULL) {
    207  1.60.18.1      phil 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
    208  1.60.18.1      phil 			if (ielen > sizeof(wpaie->rsn_ie))
    209  1.60.18.1      phil 				ielen = sizeof(wpaie->rsn_ie);
    210  1.60.18.1      phil 			memcpy(wpaie->rsn_ie, ni->ni_ies.rsn_ie, ielen);
    211  1.60.18.1      phil 		}
    212  1.60.18.1      phil 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie2))
    213  1.60.18.1      phil 			ireq->i_len = sizeof(struct ieee80211req_wpaie2);
    214  1.60.18.1      phil 	} else {
    215  1.60.18.1      phil 		/* compatibility op, may overwrite wpa ie */
    216  1.60.18.1      phil 		/* XXX check ic_flags? */
    217  1.60.18.1      phil 		if (ni->ni_ies.rsn_ie != NULL) {
    218  1.60.18.1      phil 			int ielen = ni->ni_ies.rsn_ie[1] + 2;
    219  1.60.18.1      phil 			if (ielen > sizeof(wpaie->wpa_ie))
    220  1.60.18.1      phil 				ielen = sizeof(wpaie->wpa_ie);
    221  1.60.18.1      phil 			memcpy(wpaie->wpa_ie, ni->ni_ies.rsn_ie, ielen);
    222  1.60.18.1      phil 		}
    223  1.60.18.1      phil 		if (ireq->i_len > sizeof(struct ieee80211req_wpaie))
    224  1.60.18.1      phil 			ireq->i_len = sizeof(struct ieee80211req_wpaie);
    225       1.19    dyoung 	}
    226       1.19    dyoung 	ieee80211_free_node(ni);
    227  1.60.18.1      phil 	error = copyout(wpaie, ireq->i_data, ireq->i_len);
    228  1.60.18.1      phil bad:
    229  1.60.18.1      phil 	IEEE80211_FREE(wpaie, M_TEMP);
    230  1.60.18.1      phil 	return error;
    231       1.19    dyoung }
    232       1.19    dyoung 
    233       1.19    dyoung static int
    234  1.60.18.1      phil ieee80211_ioctl_getstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
    235       1.19    dyoung {
    236       1.19    dyoung 	struct ieee80211_node *ni;
    237  1.60.18.1      phil 	uint8_t macaddr[IEEE80211_ADDR_LEN];
    238  1.60.18.1      phil 	const size_t off = __offsetof(struct ieee80211req_sta_stats, is_stats);
    239       1.19    dyoung 	int error;
    240       1.19    dyoung 
    241       1.19    dyoung 	if (ireq->i_len < off)
    242       1.19    dyoung 		return EINVAL;
    243       1.19    dyoung 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
    244       1.19    dyoung 	if (error != 0)
    245       1.19    dyoung 		return error;
    246  1.60.18.1      phil 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
    247       1.19    dyoung 	if (ni == NULL)
    248  1.60.18.1      phil 		return ENOENT;
    249       1.19    dyoung 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
    250       1.19    dyoung 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
    251       1.19    dyoung 	/* NB: copy out only the statistics */
    252  1.60.18.1      phil 	error = copyout(&ni->ni_stats, (uint8_t *) ireq->i_data + off,
    253       1.19    dyoung 			ireq->i_len - off);
    254       1.19    dyoung 	ieee80211_free_node(ni);
    255       1.19    dyoung 	return error;
    256       1.19    dyoung }
    257       1.19    dyoung 
    258  1.60.18.1      phil struct scanreq {
    259  1.60.18.1      phil 	struct ieee80211req_scan_result *sr;
    260  1.60.18.1      phil 	size_t space;
    261  1.60.18.1      phil };
    262       1.19    dyoung 
    263  1.60.18.1      phil static size_t
    264  1.60.18.1      phil scan_space(const struct ieee80211_scan_entry *se, int *ielen)
    265  1.60.18.1      phil {
    266  1.60.18.1      phil 	size_t len;
    267       1.28  christos 
    268  1.60.18.1      phil 	*ielen = se->se_ies.len;
    269       1.28  christos 	/*
    270  1.60.18.1      phil 	 * NB: ie's can be no more than 255 bytes and the max 802.11
    271  1.60.18.1      phil 	 * packet is <3Kbytes so we are sure this doesn't overflow
    272  1.60.18.1      phil 	 * 16-bits; if this is a concern we can drop the ie's.
    273       1.28  christos 	 */
    274  1.60.18.1      phil 	len = sizeof(struct ieee80211req_scan_result) + se->se_ssid[1] +
    275  1.60.18.1      phil 	    se->se_meshid[1] + *ielen;
    276  1.60.18.1      phil 	return roundup(len, sizeof(uint32_t));
    277  1.60.18.1      phil }
    278  1.60.18.1      phil 
    279  1.60.18.1      phil static void
    280  1.60.18.1      phil get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
    281  1.60.18.1      phil {
    282  1.60.18.1      phil 	struct scanreq *req = arg;
    283  1.60.18.1      phil 	int ielen;
    284  1.60.18.1      phil 
    285  1.60.18.1      phil 	req->space += scan_space(se, &ielen);
    286  1.60.18.1      phil }
    287  1.60.18.1      phil 
    288  1.60.18.1      phil static void
    289  1.60.18.1      phil get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
    290  1.60.18.1      phil {
    291  1.60.18.1      phil 	struct scanreq *req = arg;
    292  1.60.18.1      phil 	struct ieee80211req_scan_result *sr;
    293  1.60.18.1      phil 	int ielen, len, nr, nxr;
    294  1.60.18.1      phil 	uint8_t *cp;
    295  1.60.18.1      phil 
    296  1.60.18.1      phil 	len = scan_space(se, &ielen);
    297  1.60.18.1      phil 	if (len > req->space)
    298  1.60.18.1      phil 		return;
    299  1.60.18.1      phil 
    300  1.60.18.1      phil 	sr = req->sr;
    301  1.60.18.1      phil 	KASSERT(len <= 65535 && ielen <= 65535,
    302  1.60.18.1      phil 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
    303  1.60.18.1      phil 	sr->isr_len = len;
    304  1.60.18.1      phil 	sr->isr_ie_off = sizeof(struct ieee80211req_scan_result);
    305       1.28  christos 	sr->isr_ie_len = ielen;
    306  1.60.18.1      phil 	sr->isr_freq = se->se_chan->ic_freq;
    307  1.60.18.1      phil 	sr->isr_flags = se->se_chan->ic_flags;
    308  1.60.18.1      phil 	sr->isr_rssi = se->se_rssi;
    309  1.60.18.1      phil 	sr->isr_noise = se->se_noise;
    310  1.60.18.1      phil 	sr->isr_intval = se->se_intval;
    311  1.60.18.1      phil 	sr->isr_capinfo = se->se_capinfo;
    312  1.60.18.1      phil 	sr->isr_erp = se->se_erp;
    313  1.60.18.1      phil 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
    314  1.60.18.1      phil 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
    315  1.60.18.1      phil 	memcpy(sr->isr_rates, se->se_rates+2, nr);
    316  1.60.18.1      phil 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
    317  1.60.18.1      phil 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
    318  1.60.18.1      phil 	sr->isr_nrates = nr + nxr;
    319  1.60.18.1      phil 
    320  1.60.18.1      phil 	/* copy SSID */
    321  1.60.18.1      phil 	sr->isr_ssid_len = se->se_ssid[1];
    322  1.60.18.1      phil 	cp = ((uint8_t *)sr) + sr->isr_ie_off;
    323  1.60.18.1      phil 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
    324  1.60.18.1      phil 
    325  1.60.18.1      phil 	/* copy mesh id */
    326  1.60.18.1      phil 	cp += sr->isr_ssid_len;
    327  1.60.18.1      phil 	sr->isr_meshid_len = se->se_meshid[1];
    328  1.60.18.1      phil 	memcpy(cp, se->se_meshid+2, sr->isr_meshid_len);
    329  1.60.18.1      phil 	cp += sr->isr_meshid_len;
    330  1.60.18.1      phil 
    331  1.60.18.1      phil 	if (ielen)
    332  1.60.18.1      phil 		memcpy(cp, se->se_ies.data, ielen);
    333  1.60.18.1      phil 
    334  1.60.18.1      phil 	req->space -= len;
    335  1.60.18.1      phil 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
    336       1.19    dyoung }
    337       1.19    dyoung 
    338       1.19    dyoung static int
    339  1.60.18.1      phil ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
    340  1.60.18.1      phil 	struct ieee80211req *ireq)
    341  1.60.18.1      phil {
    342  1.60.18.1      phil 	struct scanreq req;
    343       1.56  christos 	int error;
    344       1.19    dyoung 
    345  1.60.18.1      phil 	if (ireq->i_len < sizeof(struct scanreq))
    346  1.60.18.1      phil 		return EFAULT;
    347  1.60.18.1      phil 
    348       1.19    dyoung 	error = 0;
    349  1.60.18.1      phil 	req.space = 0;
    350  1.60.18.1      phil 	ieee80211_scan_iterate(vap, get_scan_space, &req);
    351  1.60.18.1      phil 	if (req.space > ireq->i_len)
    352  1.60.18.1      phil 		req.space = ireq->i_len;
    353  1.60.18.1      phil 	if (req.space > 0) {
    354  1.60.18.1      phil 		uint32_t space;
    355  1.60.18.1      phil 		void *p;
    356  1.60.18.1      phil 
    357  1.60.18.1      phil 		space = req.space;
    358  1.60.18.1      phil 		/* XXX M_WAITOK after driver lock released */
    359  1.60.18.1      phil 		p = IEEE80211_MALLOC(space, M_TEMP,
    360  1.60.18.1      phil 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    361  1.60.18.1      phil 		if (p == NULL)
    362  1.60.18.1      phil 			return ENOMEM;
    363  1.60.18.1      phil 		req.sr = p;
    364  1.60.18.1      phil 		ieee80211_scan_iterate(vap, get_scan_result, &req);
    365  1.60.18.1      phil 		ireq->i_len = space - req.space;
    366  1.60.18.1      phil 		error = copyout(p, ireq->i_data, ireq->i_len);
    367  1.60.18.1      phil 		IEEE80211_FREE(p, M_TEMP);
    368  1.60.18.1      phil 	} else
    369  1.60.18.1      phil 		ireq->i_len = 0;
    370  1.60.18.1      phil 
    371       1.19    dyoung 	return error;
    372       1.19    dyoung }
    373       1.19    dyoung 
    374  1.60.18.7      phil #ifdef OLD_IEEE80211_IOC_SCAN_RESULTS
    375  1.60.18.7      phil /* Code to get the old version of scan results, for NetBSD 8.0 and earlier. */
    376  1.60.18.7      phil 
    377  1.60.18.7      phil static size_t
    378  1.60.18.7      phil old_scan_space(const struct ieee80211_scan_entry *se, int *ielen)
    379  1.60.18.7      phil {
    380  1.60.18.7      phil 	size_t len;
    381  1.60.18.7      phil 
    382  1.60.18.7      phil 	*ielen = se->se_ies.len;
    383  1.60.18.7      phil 	/*
    384  1.60.18.7      phil 	 * NB: ie's can be no more than 255 bytes and the max 802.11
    385  1.60.18.7      phil 	 * packet is <3Kbytes so we are sure this doesn't overflow
    386  1.60.18.7      phil 	 * 16-bits; if this is a concern we can drop the ie's.
    387  1.60.18.7      phil 	 */
    388  1.60.18.7      phil 	len = sizeof(struct old_ieee80211req_scan_result) + se->se_ssid[1] +
    389  1.60.18.7      phil 	    se->se_meshid[1] + *ielen;
    390  1.60.18.7      phil 	return roundup(len, sizeof(uint32_t));
    391  1.60.18.7      phil }
    392  1.60.18.7      phil 
    393  1.60.18.7      phil static void
    394  1.60.18.7      phil old_get_scan_space(void *arg, const struct ieee80211_scan_entry *se)
    395  1.60.18.7      phil {
    396  1.60.18.7      phil 	struct scanreq *req = arg;
    397  1.60.18.7      phil 	int ielen;
    398  1.60.18.7      phil 
    399  1.60.18.7      phil 	req->space += old_scan_space(se, &ielen);
    400  1.60.18.7      phil }
    401  1.60.18.7      phil 
    402  1.60.18.7      phil static void
    403  1.60.18.7      phil old_get_scan_result(void *arg, const struct ieee80211_scan_entry *se)
    404  1.60.18.7      phil {
    405  1.60.18.7      phil 	struct scanreq *req = arg;
    406  1.60.18.7      phil 	struct old_ieee80211req_scan_result *sr;
    407  1.60.18.7      phil 	int ielen, len, nr, nxr;
    408  1.60.18.7      phil 	uint8_t *cp;
    409  1.60.18.7      phil 
    410  1.60.18.7      phil 	len = old_scan_space(se, &ielen);
    411  1.60.18.7      phil 	if (len > req->space)
    412  1.60.18.7      phil 		return;
    413  1.60.18.7      phil 
    414  1.60.18.7      phil 	sr = (struct old_ieee80211req_scan_result *)req->sr;
    415  1.60.18.7      phil 	KASSERT(len <= 65535 && ielen <= 65535,
    416  1.60.18.7      phil 	    ("len %u ssid %u ie %u", len, se->se_ssid[1], ielen));
    417  1.60.18.7      phil 	sr->isr_len = len;
    418  1.60.18.7      phil 	sr->isr_ie_len = ielen;
    419  1.60.18.7      phil 	sr->isr_freq = se->se_chan->ic_freq;
    420  1.60.18.7      phil 	sr->isr_flags = se->se_chan->ic_flags;
    421  1.60.18.7      phil 	sr->isr_rssi = se->se_rssi;
    422  1.60.18.7      phil 	sr->isr_noise = se->se_noise;
    423  1.60.18.7      phil 	sr->isr_intval = se->se_intval;
    424  1.60.18.7      phil 	sr->isr_capinfo = se->se_capinfo;
    425  1.60.18.7      phil 	sr->isr_erp = se->se_erp;
    426  1.60.18.7      phil 	IEEE80211_ADDR_COPY(sr->isr_bssid, se->se_bssid);
    427  1.60.18.7      phil 	nr = min(se->se_rates[1], IEEE80211_RATE_MAXSIZE);
    428  1.60.18.7      phil 	memcpy(sr->isr_rates, se->se_rates+2, nr);
    429  1.60.18.7      phil 	nxr = min(se->se_xrates[1], IEEE80211_RATE_MAXSIZE - nr);
    430  1.60.18.7      phil 	memcpy(sr->isr_rates+nr, se->se_xrates+2, nxr);
    431  1.60.18.7      phil 	sr->isr_nrates = nr + nxr;
    432  1.60.18.7      phil 	if (sr->isr_nrates > 15)
    433  1.60.18.7      phil 		sr->isr_nrates = 15;
    434  1.60.18.7      phil 
    435  1.60.18.7      phil 	printf ("old_get_scan_results: ssid=%.*s\n", se->se_ssid[1], &se->se_ssid[2]);
    436  1.60.18.7      phil 
    437  1.60.18.7      phil 	/* copy SSID */
    438  1.60.18.7      phil 	sr->isr_ssid_len = se->se_ssid[1];
    439  1.60.18.7      phil 	cp = ((uint8_t *)sr) + sizeof(struct old_ieee80211req_scan_result);
    440  1.60.18.7      phil 	memcpy(cp, se->se_ssid+2, sr->isr_ssid_len);
    441  1.60.18.7      phil 
    442  1.60.18.7      phil 	if (ielen)
    443  1.60.18.7      phil 		memcpy(cp+sr->isr_ssid_len, se->se_ies.data, ielen);
    444  1.60.18.7      phil 
    445  1.60.18.7      phil 	req->space -= len;
    446  1.60.18.7      phil 	req->sr = (struct ieee80211req_scan_result *)(((uint8_t *)sr) + len);
    447  1.60.18.7      phil }
    448  1.60.18.7      phil 
    449  1.60.18.7      phil static int
    450  1.60.18.7      phil old_ieee80211_ioctl_getscanresults(struct ieee80211vap *vap,
    451  1.60.18.7      phil 	struct ieee80211req *ireq)
    452  1.60.18.7      phil {
    453  1.60.18.7      phil 	struct scanreq req;
    454  1.60.18.7      phil 	int error;
    455  1.60.18.7      phil 
    456  1.60.18.7      phil 	if (ireq->i_len < sizeof(struct scanreq))
    457  1.60.18.7      phil 		return EFAULT;
    458  1.60.18.7      phil 
    459  1.60.18.7      phil 	error = 0;
    460  1.60.18.7      phil 	req.space = 0;
    461  1.60.18.7      phil 	ieee80211_scan_iterate(vap, old_get_scan_space, &req);
    462  1.60.18.7      phil 	if (req.space > ireq->i_len)
    463  1.60.18.7      phil 		req.space = ireq->i_len;
    464  1.60.18.7      phil 	if (req.space > 0) {
    465  1.60.18.7      phil 		uint32_t space;
    466  1.60.18.7      phil 		void *p;
    467  1.60.18.7      phil 
    468  1.60.18.7      phil 		space = req.space;
    469  1.60.18.7      phil 		/* XXX M_WAITOK after driver lock released */
    470  1.60.18.7      phil 		p = IEEE80211_MALLOC(space, M_TEMP,
    471  1.60.18.7      phil 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    472  1.60.18.7      phil 		if (p == NULL)
    473  1.60.18.7      phil 			return ENOMEM;
    474  1.60.18.7      phil 		req.sr = p;
    475  1.60.18.7      phil 		ieee80211_scan_iterate(vap, old_get_scan_result, &req);
    476  1.60.18.7      phil 		ireq->i_len = space - req.space;
    477  1.60.18.7      phil 		error = copyout(p, ireq->i_data, ireq->i_len);
    478  1.60.18.7      phil 		IEEE80211_FREE(p, M_TEMP);
    479  1.60.18.7      phil 	} else
    480  1.60.18.7      phil 		ireq->i_len = 0;
    481  1.60.18.7      phil 
    482  1.60.18.7      phil 	return error;
    483  1.60.18.7      phil }
    484  1.60.18.7      phil 
    485  1.60.18.7      phil #endif
    486  1.60.18.7      phil 
    487       1.26     skrll struct stainforeq {
    488       1.26     skrll 	struct ieee80211req_sta_info *si;
    489       1.26     skrll 	size_t	space;
    490       1.26     skrll };
    491       1.26     skrll 
    492       1.26     skrll static size_t
    493       1.26     skrll sta_space(const struct ieee80211_node *ni, size_t *ielen)
    494       1.26     skrll {
    495  1.60.18.1      phil 	*ielen = ni->ni_ies.len;
    496       1.26     skrll 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
    497  1.60.18.1      phil 		      sizeof(uint32_t));
    498       1.26     skrll }
    499       1.26     skrll 
    500       1.19    dyoung static void
    501       1.26     skrll get_sta_space(void *arg, struct ieee80211_node *ni)
    502       1.19    dyoung {
    503       1.26     skrll 	struct stainforeq *req = arg;
    504       1.26     skrll 	size_t ielen;
    505       1.19    dyoung 
    506  1.60.18.1      phil 	if (ni->ni_vap->iv_opmode == IEEE80211_M_HOSTAP &&
    507       1.26     skrll 	    ni->ni_associd == 0)	/* only associated stations */
    508       1.26     skrll 		return;
    509       1.26     skrll 	req->space += sta_space(ni, &ielen);
    510       1.26     skrll }
    511       1.26     skrll 
    512       1.26     skrll static void
    513       1.26     skrll get_sta_info(void *arg, struct ieee80211_node *ni)
    514       1.26     skrll {
    515       1.26     skrll 	struct stainforeq *req = arg;
    516  1.60.18.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
    517       1.26     skrll 	struct ieee80211req_sta_info *si;
    518       1.26     skrll 	size_t ielen, len;
    519  1.60.18.1      phil 	uint8_t *cp;
    520       1.26     skrll 
    521  1.60.18.1      phil 	if (vap->iv_opmode == IEEE80211_M_HOSTAP &&
    522       1.26     skrll 	    ni->ni_associd == 0)	/* only associated stations */
    523       1.26     skrll 		return;
    524       1.26     skrll 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
    525       1.26     skrll 		return;
    526       1.26     skrll 	len = sta_space(ni, &ielen);
    527       1.26     skrll 	if (len > req->space)
    528       1.26     skrll 		return;
    529       1.26     skrll 	si = req->si;
    530       1.26     skrll 	si->isi_len = len;
    531  1.60.18.1      phil 	si->isi_ie_off = sizeof(struct ieee80211req_sta_info);
    532       1.26     skrll 	si->isi_ie_len = ielen;
    533       1.19    dyoung 	si->isi_freq = ni->ni_chan->ic_freq;
    534       1.19    dyoung 	si->isi_flags = ni->ni_chan->ic_flags;
    535       1.19    dyoung 	si->isi_state = ni->ni_flags;
    536       1.19    dyoung 	si->isi_authmode = ni->ni_authmode;
    537  1.60.18.1      phil 	vap->iv_ic->ic_node_getsignal(ni, &si->isi_rssi, &si->isi_noise);
    538  1.60.18.1      phil 	vap->iv_ic->ic_node_getmimoinfo(ni, &si->isi_mimo);
    539       1.19    dyoung 	si->isi_capinfo = ni->ni_capinfo;
    540       1.19    dyoung 	si->isi_erp = ni->ni_erp;
    541       1.19    dyoung 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
    542       1.19    dyoung 	si->isi_nrates = ni->ni_rates.rs_nrates;
    543       1.19    dyoung 	if (si->isi_nrates > 15)
    544       1.19    dyoung 		si->isi_nrates = 15;
    545       1.19    dyoung 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
    546       1.19    dyoung 	si->isi_txrate = ni->ni_txrate;
    547  1.60.18.1      phil 	if (si->isi_txrate & IEEE80211_RATE_MCS) {
    548  1.60.18.1      phil 		const struct ieee80211_mcs_rates *mcs =
    549  1.60.18.1      phil 		    &ieee80211_htrates[ni->ni_txrate &~ IEEE80211_RATE_MCS];
    550  1.60.18.1      phil 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
    551  1.60.18.1      phil 			if (ni->ni_flags & IEEE80211_NODE_SGI40)
    552  1.60.18.1      phil 				si->isi_txmbps = mcs->ht40_rate_800ns;
    553  1.60.18.1      phil 			else
    554  1.60.18.1      phil 				si->isi_txmbps = mcs->ht40_rate_400ns;
    555  1.60.18.1      phil 		} else {
    556  1.60.18.1      phil 			if (ni->ni_flags & IEEE80211_NODE_SGI20)
    557  1.60.18.1      phil 				si->isi_txmbps = mcs->ht20_rate_800ns;
    558  1.60.18.1      phil 			else
    559  1.60.18.1      phil 				si->isi_txmbps = mcs->ht20_rate_400ns;
    560  1.60.18.1      phil 		}
    561  1.60.18.1      phil 	} else
    562  1.60.18.1      phil 		si->isi_txmbps = si->isi_txrate;
    563       1.19    dyoung 	si->isi_associd = ni->ni_associd;
    564       1.19    dyoung 	si->isi_txpower = ni->ni_txpower;
    565       1.19    dyoung 	si->isi_vlan = ni->ni_vlan;
    566       1.19    dyoung 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
    567       1.19    dyoung 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
    568       1.19    dyoung 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
    569       1.19    dyoung 	} else {
    570  1.60.18.1      phil 		si->isi_txseqs[0] = ni->ni_txseqs[IEEE80211_NONQOS_TID];
    571  1.60.18.1      phil 		si->isi_rxseqs[0] = ni->ni_rxseqs[IEEE80211_NONQOS_TID];
    572       1.19    dyoung 	}
    573       1.26     skrll 	/* NB: leave all cases in case we relax ni_associd == 0 check */
    574       1.26     skrll 	if (ieee80211_node_is_authorized(ni))
    575  1.60.18.1      phil 		si->isi_inact = vap->iv_inact_run;
    576  1.60.18.1      phil 	else if (ni->ni_associd != 0 ||
    577  1.60.18.1      phil 	    (vap->iv_opmode == IEEE80211_M_WDS &&
    578  1.60.18.1      phil 	     (vap->iv_flags_ext & IEEE80211_FEXT_WDSLEGACY)))
    579  1.60.18.1      phil 		si->isi_inact = vap->iv_inact_auth;
    580       1.19    dyoung 	else
    581  1.60.18.1      phil 		si->isi_inact = vap->iv_inact_init;
    582       1.19    dyoung 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
    583  1.60.18.1      phil 	si->isi_localid = ni->ni_mllid;
    584  1.60.18.1      phil 	si->isi_peerid = ni->ni_mlpid;
    585  1.60.18.1      phil 	si->isi_peerstate = ni->ni_mlstate;
    586       1.26     skrll 
    587  1.60.18.1      phil 	if (ielen) {
    588  1.60.18.1      phil 		cp = ((uint8_t *)si) + si->isi_ie_off;
    589  1.60.18.1      phil 		memcpy(cp, ni->ni_ies.data, ielen);
    590       1.26     skrll 	}
    591       1.26     skrll 
    592  1.60.18.1      phil 	req->si = (struct ieee80211req_sta_info *)(((uint8_t *)si) + len);
    593       1.26     skrll 	req->space -= len;
    594       1.19    dyoung }
    595       1.19    dyoung 
    596       1.19    dyoung static int
    597  1.60.18.1      phil getstainfo_common(struct ieee80211vap *vap, struct ieee80211req *ireq,
    598  1.60.18.1      phil 	struct ieee80211_node *ni, size_t off)
    599       1.19    dyoung {
    600  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    601       1.26     skrll 	struct stainforeq req;
    602  1.60.18.1      phil 	size_t space;
    603  1.60.18.1      phil 	void *p;
    604       1.26     skrll 	int error;
    605       1.26     skrll 
    606       1.19    dyoung 	error = 0;
    607       1.26     skrll 	req.space = 0;
    608  1.60.18.1      phil 	if (ni == NULL) {
    609  1.60.18.1      phil 		ieee80211_iterate_nodes_vap(&ic->ic_sta, vap, get_sta_space,
    610  1.60.18.1      phil 		    &req);
    611  1.60.18.1      phil 	} else
    612  1.60.18.1      phil 		get_sta_space(&req, ni);
    613       1.26     skrll 	if (req.space > ireq->i_len)
    614       1.26     skrll 		req.space = ireq->i_len;
    615       1.26     skrll 	if (req.space > 0) {
    616       1.26     skrll 		space = req.space;
    617       1.26     skrll 		/* XXX M_WAITOK after driver lock released */
    618  1.60.18.1      phil 		p = IEEE80211_MALLOC(space, M_TEMP,
    619  1.60.18.1      phil 		    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    620  1.60.18.1      phil 		if (p == NULL) {
    621  1.60.18.1      phil 			error = ENOMEM;
    622  1.60.18.1      phil 			goto bad;
    623  1.60.18.1      phil 		}
    624       1.26     skrll 		req.si = p;
    625  1.60.18.1      phil 		if (ni == NULL) {
    626  1.60.18.1      phil 			ieee80211_iterate_nodes_vap(&ic->ic_sta, vap,
    627  1.60.18.1      phil 			    get_sta_info, &req);
    628  1.60.18.1      phil 		} else
    629  1.60.18.1      phil 			get_sta_info(&req, ni);
    630       1.26     skrll 		ireq->i_len = space - req.space;
    631  1.60.18.1      phil 		error = copyout(p, (uint8_t *) ireq->i_data+off, ireq->i_len);
    632  1.60.18.1      phil 		IEEE80211_FREE(p, M_TEMP);
    633       1.26     skrll 	} else
    634       1.26     skrll 		ireq->i_len = 0;
    635  1.60.18.1      phil bad:
    636  1.60.18.1      phil 	if (ni != NULL)
    637  1.60.18.1      phil 		ieee80211_free_node(ni);
    638  1.60.18.1      phil 	return error;
    639  1.60.18.1      phil }
    640  1.60.18.1      phil 
    641  1.60.18.1      phil static int
    642  1.60.18.1      phil ieee80211_ioctl_getstainfo(struct ieee80211vap *vap, struct ieee80211req *ireq)
    643  1.60.18.1      phil {
    644  1.60.18.1      phil 	uint8_t macaddr[IEEE80211_ADDR_LEN];
    645  1.60.18.1      phil 	const size_t off = __offsetof(struct ieee80211req_sta_req, info);
    646  1.60.18.1      phil 	struct ieee80211_node *ni;
    647  1.60.18.1      phil 	int error;
    648  1.60.18.1      phil 
    649  1.60.18.1      phil 	if (ireq->i_len < sizeof(struct ieee80211req_sta_req))
    650  1.60.18.1      phil 		return EFAULT;
    651  1.60.18.1      phil 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
    652  1.60.18.1      phil 	if (error != 0)
    653  1.60.18.1      phil 		return error;
    654  1.60.18.1      phil 	if (IEEE80211_ADDR_EQ(macaddr, vap->iv_ifp->if_broadcastaddr)) {
    655  1.60.18.1      phil 		ni = NULL;
    656  1.60.18.1      phil 	} else {
    657  1.60.18.1      phil 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
    658  1.60.18.1      phil 		if (ni == NULL)
    659  1.60.18.1      phil 			return ENOENT;
    660  1.60.18.1      phil 	}
    661  1.60.18.1      phil 	return getstainfo_common(vap, ireq, ni, off);
    662       1.19    dyoung }
    663       1.19    dyoung 
    664       1.19    dyoung static int
    665  1.60.18.1      phil ieee80211_ioctl_getstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
    666       1.19    dyoung {
    667       1.19    dyoung 	struct ieee80211_node *ni;
    668       1.19    dyoung 	struct ieee80211req_sta_txpow txpow;
    669       1.19    dyoung 	int error;
    670       1.19    dyoung 
    671       1.19    dyoung 	if (ireq->i_len != sizeof(txpow))
    672       1.19    dyoung 		return EINVAL;
    673       1.19    dyoung 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
    674       1.19    dyoung 	if (error != 0)
    675       1.19    dyoung 		return error;
    676  1.60.18.1      phil 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
    677       1.19    dyoung 	if (ni == NULL)
    678  1.60.18.1      phil 		return ENOENT;
    679       1.19    dyoung 	txpow.it_txpow = ni->ni_txpower;
    680       1.19    dyoung 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
    681       1.19    dyoung 	ieee80211_free_node(ni);
    682        1.1    dyoung 	return error;
    683        1.1    dyoung }
    684        1.1    dyoung 
    685       1.19    dyoung static int
    686  1.60.18.1      phil ieee80211_ioctl_getwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
    687       1.19    dyoung {
    688  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    689       1.19    dyoung 	struct ieee80211_wme_state *wme = &ic->ic_wme;
    690       1.19    dyoung 	struct wmeParams *wmep;
    691       1.19    dyoung 	int ac;
    692       1.19    dyoung 
    693       1.19    dyoung 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
    694       1.19    dyoung 		return EINVAL;
    695       1.19    dyoung 
    696       1.19    dyoung 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
    697       1.19    dyoung 	if (ac >= WME_NUM_AC)
    698       1.19    dyoung 		ac = WME_AC_BE;
    699       1.19    dyoung 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
    700       1.19    dyoung 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
    701       1.19    dyoung 	else
    702       1.19    dyoung 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
    703       1.19    dyoung 	switch (ireq->i_type) {
    704       1.19    dyoung 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
    705       1.19    dyoung 		ireq->i_val = wmep->wmep_logcwmin;
    706       1.19    dyoung 		break;
    707       1.19    dyoung 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
    708       1.19    dyoung 		ireq->i_val = wmep->wmep_logcwmax;
    709       1.19    dyoung 		break;
    710       1.19    dyoung 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
    711       1.19    dyoung 		ireq->i_val = wmep->wmep_aifsn;
    712       1.19    dyoung 		break;
    713       1.19    dyoung 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
    714       1.19    dyoung 		ireq->i_val = wmep->wmep_txopLimit;
    715       1.19    dyoung 		break;
    716       1.19    dyoung 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
    717       1.19    dyoung 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
    718       1.19    dyoung 		ireq->i_val = wmep->wmep_acm;
    719       1.19    dyoung 		break;
    720       1.19    dyoung 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
    721       1.19    dyoung 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
    722       1.19    dyoung 		ireq->i_val = !wmep->wmep_noackPolicy;
    723       1.19    dyoung 		break;
    724       1.19    dyoung 	}
    725       1.19    dyoung 	return 0;
    726       1.19    dyoung }
    727       1.19    dyoung 
    728       1.26     skrll static int
    729  1.60.18.1      phil ieee80211_ioctl_getmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
    730  1.60.18.1      phil {
    731  1.60.18.1      phil 	const struct ieee80211_aclator *acl = vap->iv_acl;
    732  1.60.18.1      phil 
    733  1.60.18.1      phil 	return (acl == NULL ? EINVAL : acl->iac_getioctl(vap, ireq));
    734  1.60.18.1      phil }
    735  1.60.18.1      phil 
    736  1.60.18.1      phil static int
    737  1.60.18.1      phil ieee80211_ioctl_getcurchan(struct ieee80211vap *vap, struct ieee80211req *ireq)
    738  1.60.18.1      phil {
    739  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    740  1.60.18.1      phil 	struct ieee80211_channel *c;
    741  1.60.18.1      phil 
    742  1.60.18.1      phil 	if (ireq->i_len != sizeof(struct ieee80211_channel))
    743  1.60.18.1      phil 		return EINVAL;
    744  1.60.18.1      phil 	/*
    745  1.60.18.1      phil 	 * vap's may have different operating channels when HT is
    746  1.60.18.1      phil 	 * in use.  When in RUN state report the vap-specific channel.
    747  1.60.18.1      phil 	 * Otherwise return curchan.
    748  1.60.18.1      phil 	 */
    749  1.60.18.1      phil 	if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
    750  1.60.18.1      phil 		c = vap->iv_bss->ni_chan;
    751  1.60.18.1      phil 	else
    752  1.60.18.1      phil 		c = ic->ic_curchan;
    753  1.60.18.1      phil 	return copyout(c, ireq->i_data, sizeof(*c));
    754  1.60.18.1      phil }
    755  1.60.18.1      phil 
    756  1.60.18.1      phil static int
    757  1.60.18.1      phil getappie(const struct ieee80211_appie *aie, struct ieee80211req *ireq)
    758  1.60.18.1      phil {
    759  1.60.18.1      phil 	if (aie == NULL)
    760  1.60.18.1      phil 		return EINVAL;
    761  1.60.18.1      phil 	/* NB: truncate, caller can check length */
    762  1.60.18.1      phil 	if (ireq->i_len > aie->ie_len)
    763  1.60.18.1      phil 		ireq->i_len = aie->ie_len;
    764  1.60.18.1      phil 	return copyout(aie->ie_data, ireq->i_data, ireq->i_len);
    765  1.60.18.1      phil }
    766  1.60.18.1      phil 
    767  1.60.18.1      phil static int
    768  1.60.18.1      phil ieee80211_ioctl_getappie(struct ieee80211vap *vap, struct ieee80211req *ireq)
    769  1.60.18.1      phil {
    770  1.60.18.1      phil 	uint8_t fc0;
    771  1.60.18.1      phil 
    772  1.60.18.1      phil 	fc0 = ireq->i_val & 0xff;
    773  1.60.18.1      phil 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
    774  1.60.18.1      phil 		return EINVAL;
    775  1.60.18.1      phil 	/* NB: could check iv_opmode and reject but hardly worth the effort */
    776  1.60.18.1      phil 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
    777  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_BEACON:
    778  1.60.18.1      phil 		return getappie(vap->iv_appie_beacon, ireq);
    779  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    780  1.60.18.1      phil 		return getappie(vap->iv_appie_proberesp, ireq);
    781  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
    782  1.60.18.1      phil 		return getappie(vap->iv_appie_assocresp, ireq);
    783  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
    784  1.60.18.1      phil 		return getappie(vap->iv_appie_probereq, ireq);
    785  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
    786  1.60.18.1      phil 		return getappie(vap->iv_appie_assocreq, ireq);
    787  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_BEACON|IEEE80211_FC0_SUBTYPE_PROBE_RESP:
    788  1.60.18.1      phil 		return getappie(vap->iv_appie_wpa, ireq);
    789  1.60.18.1      phil 	}
    790  1.60.18.1      phil 	return EINVAL;
    791  1.60.18.1      phil }
    792  1.60.18.1      phil 
    793  1.60.18.1      phil static int
    794  1.60.18.1      phil ieee80211_ioctl_getregdomain(struct ieee80211vap *vap,
    795  1.60.18.1      phil 	const struct ieee80211req *ireq)
    796  1.60.18.1      phil {
    797  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    798  1.60.18.1      phil 
    799  1.60.18.1      phil 	if (ireq->i_len != sizeof(ic->ic_regdomain))
    800  1.60.18.1      phil 		return EINVAL;
    801  1.60.18.1      phil 	return copyout(&ic->ic_regdomain, ireq->i_data,
    802  1.60.18.1      phil 	    sizeof(ic->ic_regdomain));
    803  1.60.18.1      phil }
    804  1.60.18.1      phil 
    805  1.60.18.1      phil static int
    806  1.60.18.1      phil ieee80211_ioctl_getroam(struct ieee80211vap *vap,
    807  1.60.18.1      phil 	const struct ieee80211req *ireq)
    808  1.60.18.1      phil {
    809  1.60.18.1      phil 	size_t len = ireq->i_len;
    810  1.60.18.1      phil 	/* NB: accept short requests for backwards compat */
    811  1.60.18.1      phil 	if (len > sizeof(vap->iv_roamparms))
    812  1.60.18.1      phil 		len = sizeof(vap->iv_roamparms);
    813  1.60.18.1      phil 	return copyout(vap->iv_roamparms, ireq->i_data, len);
    814  1.60.18.1      phil }
    815  1.60.18.1      phil 
    816  1.60.18.1      phil static int
    817  1.60.18.1      phil ieee80211_ioctl_gettxparams(struct ieee80211vap *vap,
    818  1.60.18.1      phil 	const struct ieee80211req *ireq)
    819  1.60.18.1      phil {
    820  1.60.18.1      phil 	size_t len = ireq->i_len;
    821  1.60.18.1      phil 	/* NB: accept short requests for backwards compat */
    822  1.60.18.1      phil 	if (len > sizeof(vap->iv_txparms))
    823  1.60.18.1      phil 		len = sizeof(vap->iv_txparms);
    824  1.60.18.1      phil 	return copyout(vap->iv_txparms, ireq->i_data, len);
    825  1.60.18.1      phil }
    826  1.60.18.1      phil 
    827  1.60.18.1      phil static int
    828  1.60.18.1      phil ieee80211_ioctl_getdevcaps(struct ieee80211com *ic,
    829  1.60.18.1      phil 	const struct ieee80211req *ireq)
    830  1.60.18.1      phil {
    831  1.60.18.1      phil 	struct ieee80211_devcaps_req *dc;
    832  1.60.18.1      phil 	struct ieee80211req_chaninfo *ci;
    833  1.60.18.1      phil 	int maxchans, error;
    834  1.60.18.1      phil 
    835  1.60.18.1      phil 	maxchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_devcaps_req)) /
    836  1.60.18.1      phil 	    sizeof(struct ieee80211_channel));
    837  1.60.18.1      phil 	/* NB: require 1 so we know ic_nchans is accessible */
    838  1.60.18.1      phil 	if (maxchans < 1)
    839  1.60.18.1      phil 		return EINVAL;
    840  1.60.18.1      phil 	/* constrain max request size, 2K channels is ~24Kbytes */
    841  1.60.18.1      phil 	if (maxchans > 2048)
    842  1.60.18.1      phil 		maxchans = 2048;
    843  1.60.18.1      phil 	dc = (struct ieee80211_devcaps_req *)
    844  1.60.18.1      phil 	    IEEE80211_MALLOC(IEEE80211_DEVCAPS_SIZE(maxchans), M_TEMP,
    845  1.60.18.1      phil 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
    846  1.60.18.1      phil 	if (dc == NULL)
    847  1.60.18.1      phil 		return ENOMEM;
    848  1.60.18.1      phil 	dc->dc_drivercaps = ic->ic_caps;
    849  1.60.18.1      phil 	dc->dc_cryptocaps = ic->ic_cryptocaps;
    850  1.60.18.1      phil 	dc->dc_htcaps = ic->ic_htcaps;
    851  1.60.18.1      phil 	dc->dc_vhtcaps = ic->ic_vhtcaps;
    852  1.60.18.1      phil 	ci = &dc->dc_chaninfo;
    853  1.60.18.1      phil 	ic->ic_getradiocaps(ic, maxchans, &ci->ic_nchans, ci->ic_chans);
    854  1.60.18.1      phil 	KASSERT(ci->ic_nchans <= maxchans,
    855  1.60.18.1      phil 	    ("nchans %d maxchans %d", ci->ic_nchans, maxchans));
    856  1.60.18.1      phil 	ieee80211_sort_channels(ci->ic_chans, ci->ic_nchans);
    857  1.60.18.1      phil 	error = copyout(dc, ireq->i_data, IEEE80211_DEVCAPS_SPACE(dc));
    858  1.60.18.1      phil 	IEEE80211_FREE(dc, M_TEMP);
    859  1.60.18.1      phil 	return error;
    860  1.60.18.1      phil }
    861  1.60.18.1      phil 
    862  1.60.18.1      phil static int
    863  1.60.18.1      phil ieee80211_ioctl_getstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
    864  1.60.18.1      phil {
    865  1.60.18.1      phil 	struct ieee80211_node *ni;
    866  1.60.18.1      phil 	struct ieee80211req_sta_vlan vlan;
    867  1.60.18.1      phil 	int error;
    868  1.60.18.1      phil 
    869  1.60.18.1      phil 	if (ireq->i_len != sizeof(vlan))
    870  1.60.18.1      phil 		return EINVAL;
    871  1.60.18.1      phil 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
    872  1.60.18.1      phil 	if (error != 0)
    873  1.60.18.1      phil 		return error;
    874  1.60.18.1      phil 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
    875  1.60.18.1      phil 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
    876  1.60.18.1      phil 		    vlan.sv_macaddr);
    877  1.60.18.1      phil 		if (ni == NULL)
    878  1.60.18.1      phil 			return ENOENT;
    879  1.60.18.1      phil 	} else
    880  1.60.18.1      phil 		ni = ieee80211_ref_node(vap->iv_bss);
    881  1.60.18.1      phil 	vlan.sv_vlan = ni->ni_vlan;
    882  1.60.18.1      phil 	error = copyout(&vlan, ireq->i_data, sizeof(vlan));
    883  1.60.18.1      phil 	ieee80211_free_node(ni);
    884  1.60.18.1      phil 	return error;
    885  1.60.18.1      phil }
    886  1.60.18.1      phil 
    887  1.60.18.1      phil /*
    888  1.60.18.1      phil  * Dummy ioctl get handler so the linker set is defined.
    889  1.60.18.1      phil  */
    890  1.60.18.2      phil static __unused int
    891  1.60.18.1      phil dummy_ioctl_get(struct ieee80211vap *vap, struct ieee80211req *ireq)
    892  1.60.18.1      phil {
    893  1.60.18.1      phil 	return ENOSYS;
    894  1.60.18.1      phil }
    895  1.60.18.1      phil IEEE80211_IOCTL_GET(dummy, dummy_ioctl_get);
    896  1.60.18.1      phil 
    897  1.60.18.1      phil static int
    898  1.60.18.1      phil ieee80211_ioctl_getdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
    899       1.26     skrll {
    900  1.60.18.4      phil 	int error;
    901  1.60.18.3      phil #if notyet
    902  1.60.18.1      phil 	ieee80211_ioctl_getfunc * const *get;
    903       1.26     skrll 
    904  1.60.18.1      phil 	SET_FOREACH(get, ieee80211_ioctl_getset) {
    905  1.60.18.1      phil 		error = (*get)(vap, ireq);
    906  1.60.18.1      phil 		if (error != ENOSYS)
    907  1.60.18.1      phil 			return error;
    908  1.60.18.1      phil 	}
    909  1.60.18.3      phil #else
    910  1.60.18.4      phil 	printf ("ieee80211_ioctl_getdefault called\n");
    911  1.60.18.4      phil 	error = dummy_ioctl_get(vap, ireq);
    912  1.60.18.2      phil #endif
    913  1.60.18.4      phil 	return error;
    914       1.26     skrll }
    915       1.26     skrll 
    916       1.19    dyoung static int
    917  1.60.18.1      phil ieee80211_ioctl_get80211(struct ieee80211vap *vap, u_long cmd,
    918       1.40  christos     struct ieee80211req *ireq)
    919        1.1    dyoung {
    920  1.60.18.1      phil #define	MS(_v, _f)	(((_v) & _f) >> _f##_S)
    921  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
    922       1.23    dyoung 	u_int kid, len;
    923  1.60.18.1      phil 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
    924        1.1    dyoung 	char tmpssid[IEEE80211_NWID_LEN];
    925       1.44    dyoung 	int error = 0;
    926        1.1    dyoung 
    927       1.19    dyoung 	switch (ireq->i_type) {
    928       1.19    dyoung 	case IEEE80211_IOC_SSID:
    929  1.60.18.1      phil 		switch (vap->iv_state) {
    930       1.19    dyoung 		case IEEE80211_S_INIT:
    931       1.19    dyoung 		case IEEE80211_S_SCAN:
    932  1.60.18.1      phil 			ireq->i_len = vap->iv_des_ssid[0].len;
    933  1.60.18.1      phil 			memcpy(tmpssid, vap->iv_des_ssid[0].ssid, ireq->i_len);
    934        1.1    dyoung 			break;
    935       1.19    dyoung 		default:
    936  1.60.18.1      phil 			ireq->i_len = vap->iv_bss->ni_esslen;
    937  1.60.18.1      phil 			memcpy(tmpssid, vap->iv_bss->ni_essid, ireq->i_len);
    938        1.1    dyoung 			break;
    939       1.19    dyoung 		}
    940       1.19    dyoung 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
    941       1.19    dyoung 		break;
    942       1.19    dyoung 	case IEEE80211_IOC_NUMSSIDS:
    943       1.19    dyoung 		ireq->i_val = 1;
    944       1.19    dyoung 		break;
    945       1.19    dyoung 	case IEEE80211_IOC_WEP:
    946  1.60.18.1      phil 		if ((vap->iv_flags & IEEE80211_F_PRIVACY) == 0)
    947       1.19    dyoung 			ireq->i_val = IEEE80211_WEP_OFF;
    948  1.60.18.1      phil 		else if (vap->iv_flags & IEEE80211_F_DROPUNENC)
    949       1.19    dyoung 			ireq->i_val = IEEE80211_WEP_ON;
    950       1.19    dyoung 		else
    951       1.19    dyoung 			ireq->i_val = IEEE80211_WEP_MIXED;
    952       1.19    dyoung 		break;
    953       1.19    dyoung 	case IEEE80211_IOC_WEPKEY:
    954       1.19    dyoung 		kid = (u_int) ireq->i_val;
    955       1.19    dyoung 		if (kid >= IEEE80211_WEP_NKID)
    956       1.19    dyoung 			return EINVAL;
    957  1.60.18.1      phil 		len = (u_int) vap->iv_nw_keys[kid].wk_keylen;
    958       1.19    dyoung 		/* NB: only root can read WEP keys */
    959  1.60.18.1      phil 		if (priv_check(curthread, PRIV_NET80211_GETKEY) == 0) {
    960  1.60.18.1      phil 			bcopy(vap->iv_nw_keys[kid].wk_key, tmpkey, len);
    961       1.19    dyoung 		} else {
    962  1.60.18.1      phil 			bzero(tmpkey, len);
    963       1.19    dyoung 		}
    964       1.19    dyoung 		ireq->i_len = len;
    965       1.19    dyoung 		error = copyout(tmpkey, ireq->i_data, len);
    966       1.19    dyoung 		break;
    967       1.19    dyoung 	case IEEE80211_IOC_NUMWEPKEYS:
    968       1.19    dyoung 		ireq->i_val = IEEE80211_WEP_NKID;
    969       1.19    dyoung 		break;
    970       1.19    dyoung 	case IEEE80211_IOC_WEPTXKEY:
    971  1.60.18.1      phil 		ireq->i_val = vap->iv_def_txkey;
    972  1.60.18.1      phil 		break;
    973  1.60.18.1      phil 	case IEEE80211_IOC_AUTHMODE:
    974  1.60.18.1      phil 		if (vap->iv_flags & IEEE80211_F_WPA)
    975  1.60.18.1      phil 			ireq->i_val = IEEE80211_AUTH_WPA;
    976  1.60.18.1      phil 		else
    977  1.60.18.1      phil 			ireq->i_val = vap->iv_bss->ni_authmode;
    978       1.19    dyoung 		break;
    979       1.19    dyoung 	case IEEE80211_IOC_CHANNEL:
    980       1.26     skrll 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
    981       1.19    dyoung 		break;
    982       1.19    dyoung 	case IEEE80211_IOC_POWERSAVE:
    983  1.60.18.1      phil 		if (vap->iv_flags & IEEE80211_F_PMGTON)
    984       1.19    dyoung 			ireq->i_val = IEEE80211_POWERSAVE_ON;
    985       1.19    dyoung 		else
    986       1.19    dyoung 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
    987       1.19    dyoung 		break;
    988       1.19    dyoung 	case IEEE80211_IOC_POWERSAVESLEEP:
    989       1.19    dyoung 		ireq->i_val = ic->ic_lintval;
    990       1.19    dyoung 		break;
    991       1.19    dyoung 	case IEEE80211_IOC_RTSTHRESHOLD:
    992  1.60.18.1      phil 		ireq->i_val = vap->iv_rtsthreshold;
    993       1.19    dyoung 		break;
    994       1.19    dyoung 	case IEEE80211_IOC_PROTMODE:
    995       1.19    dyoung 		ireq->i_val = ic->ic_protmode;
    996       1.19    dyoung 		break;
    997       1.19    dyoung 	case IEEE80211_IOC_TXPOWER:
    998  1.60.18.1      phil 		/*
    999  1.60.18.1      phil 		 * Tx power limit is the min of max regulatory
   1000  1.60.18.1      phil 		 * power, any user-set limit, and the max the
   1001  1.60.18.1      phil 		 * radio can do.
   1002  1.60.18.1      phil 		 *
   1003  1.60.18.1      phil 		 * TODO: methodize this
   1004  1.60.18.1      phil 		 */
   1005  1.60.18.1      phil 		ireq->i_val = 2*ic->ic_curchan->ic_maxregpower;
   1006  1.60.18.1      phil 		if (ireq->i_val > ic->ic_txpowlimit)
   1007  1.60.18.1      phil 			ireq->i_val = ic->ic_txpowlimit;
   1008  1.60.18.1      phil 		if (ireq->i_val > ic->ic_curchan->ic_maxpower)
   1009  1.60.18.1      phil 			ireq->i_val = ic->ic_curchan->ic_maxpower;
   1010       1.19    dyoung 		break;
   1011       1.19    dyoung 	case IEEE80211_IOC_WPA:
   1012  1.60.18.1      phil 		switch (vap->iv_flags & IEEE80211_F_WPA) {
   1013       1.19    dyoung 		case IEEE80211_F_WPA1:
   1014       1.19    dyoung 			ireq->i_val = 1;
   1015       1.19    dyoung 			break;
   1016       1.19    dyoung 		case IEEE80211_F_WPA2:
   1017       1.19    dyoung 			ireq->i_val = 2;
   1018       1.19    dyoung 			break;
   1019       1.19    dyoung 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
   1020       1.19    dyoung 			ireq->i_val = 3;
   1021       1.19    dyoung 			break;
   1022       1.19    dyoung 		default:
   1023       1.19    dyoung 			ireq->i_val = 0;
   1024       1.19    dyoung 			break;
   1025       1.19    dyoung 		}
   1026       1.19    dyoung 		break;
   1027       1.19    dyoung 	case IEEE80211_IOC_CHANLIST:
   1028  1.60.18.1      phil 		error = ieee80211_ioctl_getchanlist(vap, ireq);
   1029       1.19    dyoung 		break;
   1030       1.19    dyoung 	case IEEE80211_IOC_ROAMING:
   1031  1.60.18.1      phil 		ireq->i_val = vap->iv_roaming;
   1032       1.19    dyoung 		break;
   1033       1.19    dyoung 	case IEEE80211_IOC_PRIVACY:
   1034  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PRIVACY) != 0;
   1035       1.19    dyoung 		break;
   1036       1.19    dyoung 	case IEEE80211_IOC_DROPUNENCRYPTED:
   1037  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DROPUNENC) != 0;
   1038       1.19    dyoung 		break;
   1039       1.19    dyoung 	case IEEE80211_IOC_COUNTERMEASURES:
   1040  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_COUNTERM) != 0;
   1041       1.19    dyoung 		break;
   1042       1.19    dyoung 	case IEEE80211_IOC_WME:
   1043  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_WME) != 0;
   1044       1.19    dyoung 		break;
   1045       1.19    dyoung 	case IEEE80211_IOC_HIDESSID:
   1046  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_HIDESSID) != 0;
   1047       1.19    dyoung 		break;
   1048       1.19    dyoung 	case IEEE80211_IOC_APBRIDGE:
   1049  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_NOBRIDGE) == 0;
   1050       1.19    dyoung 		break;
   1051       1.19    dyoung 	case IEEE80211_IOC_WPAKEY:
   1052  1.60.18.1      phil 		error = ieee80211_ioctl_getkey(vap, ireq);
   1053       1.19    dyoung 		break;
   1054       1.19    dyoung 	case IEEE80211_IOC_CHANINFO:
   1055  1.60.18.1      phil 		error = ieee80211_ioctl_getchaninfo(vap, ireq);
   1056  1.60.18.1      phil 		break;
   1057  1.60.18.1      phil 	case IEEE80211_IOC_BSSID:
   1058  1.60.18.1      phil 		if (ireq->i_len != IEEE80211_ADDR_LEN)
   1059  1.60.18.1      phil 			return EINVAL;
   1060  1.60.18.1      phil 		if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) {
   1061  1.60.18.1      phil 			error = copyout(vap->iv_opmode == IEEE80211_M_WDS ?
   1062  1.60.18.1      phil 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid,
   1063  1.60.18.1      phil 			    ireq->i_data, ireq->i_len);
   1064  1.60.18.1      phil 		} else
   1065  1.60.18.1      phil 			error = copyout(vap->iv_des_bssid, ireq->i_data,
   1066  1.60.18.1      phil 			    ireq->i_len);
   1067       1.19    dyoung 		break;
   1068       1.19    dyoung 	case IEEE80211_IOC_WPAIE:
   1069  1.60.18.1      phil 	case IEEE80211_IOC_WPAIE2:
   1070  1.60.18.1      phil 		error = ieee80211_ioctl_getwpaie(vap, ireq, ireq->i_type);
   1071       1.19    dyoung 		break;
   1072  1.60.18.7      phil #ifdef OLD_IEEE80211_IOC_SCAN_RESULTS
   1073  1.60.18.7      phil 	case OLD_IEEE80211_IOC_SCAN_RESULTS:
   1074  1.60.18.7      phil 		error = old_ieee80211_ioctl_getscanresults(vap, ireq);
   1075  1.60.18.7      phil 		break;
   1076  1.60.18.7      phil #endif
   1077       1.19    dyoung 	case IEEE80211_IOC_SCAN_RESULTS:
   1078  1.60.18.1      phil 		error = ieee80211_ioctl_getscanresults(vap, ireq);
   1079       1.19    dyoung 		break;
   1080       1.19    dyoung 	case IEEE80211_IOC_STA_STATS:
   1081  1.60.18.1      phil 		error = ieee80211_ioctl_getstastats(vap, ireq);
   1082       1.19    dyoung 		break;
   1083       1.19    dyoung 	case IEEE80211_IOC_TXPOWMAX:
   1084  1.60.18.1      phil 		ireq->i_val = vap->iv_bss->ni_txpower;
   1085       1.19    dyoung 		break;
   1086       1.19    dyoung 	case IEEE80211_IOC_STA_TXPOW:
   1087  1.60.18.1      phil 		error = ieee80211_ioctl_getstatxpow(vap, ireq);
   1088       1.19    dyoung 		break;
   1089       1.19    dyoung 	case IEEE80211_IOC_STA_INFO:
   1090  1.60.18.1      phil 		error = ieee80211_ioctl_getstainfo(vap, ireq);
   1091       1.19    dyoung 		break;
   1092       1.19    dyoung 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1093       1.19    dyoung 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1094       1.19    dyoung 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1095       1.19    dyoung 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1096       1.19    dyoung 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1097  1.60.18.1      phil 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
   1098  1.60.18.1      phil 		error = ieee80211_ioctl_getwmeparam(vap, ireq);
   1099       1.19    dyoung 		break;
   1100       1.19    dyoung 	case IEEE80211_IOC_DTIM_PERIOD:
   1101  1.60.18.1      phil 		ireq->i_val = vap->iv_dtim_period;
   1102       1.19    dyoung 		break;
   1103       1.19    dyoung 	case IEEE80211_IOC_BEACON_INTERVAL:
   1104       1.19    dyoung 		/* NB: get from ic_bss for station mode */
   1105  1.60.18.1      phil 		ireq->i_val = vap->iv_bss->ni_intval;
   1106       1.19    dyoung 		break;
   1107       1.21    dyoung 	case IEEE80211_IOC_PUREG:
   1108  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_PUREG) != 0;
   1109  1.60.18.1      phil 		break;
   1110  1.60.18.1      phil 	case IEEE80211_IOC_QUIET:
   1111  1.60.18.1      phil 		ireq->i_val = vap->iv_quiet;
   1112  1.60.18.1      phil 		break;
   1113  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_COUNT:
   1114  1.60.18.1      phil 		ireq->i_val = vap->iv_quiet_count;
   1115       1.21    dyoung 		break;
   1116  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_PERIOD:
   1117  1.60.18.1      phil 		ireq->i_val = vap->iv_quiet_period;
   1118  1.60.18.1      phil 		break;
   1119  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_DUR:
   1120  1.60.18.1      phil 		ireq->i_val = vap->iv_quiet_duration;
   1121  1.60.18.1      phil 		break;
   1122  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_OFFSET:
   1123  1.60.18.1      phil 		ireq->i_val = vap->iv_quiet_offset;
   1124  1.60.18.1      phil 		break;
   1125  1.60.18.1      phil 	case IEEE80211_IOC_BGSCAN:
   1126  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BGSCAN) != 0;
   1127  1.60.18.1      phil 		break;
   1128  1.60.18.1      phil 	case IEEE80211_IOC_BGSCAN_IDLE:
   1129  1.60.18.1      phil 		ireq->i_val = vap->iv_bgscanidle*hz/1000;	/* ms */
   1130  1.60.18.1      phil 		break;
   1131  1.60.18.1      phil 	case IEEE80211_IOC_BGSCAN_INTERVAL:
   1132  1.60.18.1      phil 		ireq->i_val = vap->iv_bgscanintvl/hz;		/* seconds */
   1133  1.60.18.1      phil 		break;
   1134  1.60.18.1      phil 	case IEEE80211_IOC_SCANVALID:
   1135  1.60.18.1      phil 		ireq->i_val = vap->iv_scanvalid/hz;		/* seconds */
   1136       1.30    dyoung 		break;
   1137       1.26     skrll 	case IEEE80211_IOC_FRAGTHRESHOLD:
   1138  1.60.18.1      phil 		ireq->i_val = vap->iv_fragthreshold;
   1139       1.26     skrll 		break;
   1140       1.26     skrll 	case IEEE80211_IOC_MACCMD:
   1141  1.60.18.1      phil 		error = ieee80211_ioctl_getmaccmd(vap, ireq);
   1142       1.26     skrll 		break;
   1143  1.60.18.1      phil 	case IEEE80211_IOC_BURST:
   1144  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_BURST) != 0;
   1145  1.60.18.1      phil 		break;
   1146  1.60.18.1      phil 	case IEEE80211_IOC_BMISSTHRESHOLD:
   1147  1.60.18.1      phil 		ireq->i_val = vap->iv_bmissthreshold;
   1148  1.60.18.1      phil 		break;
   1149  1.60.18.1      phil 	case IEEE80211_IOC_CURCHAN:
   1150  1.60.18.1      phil 		error = ieee80211_ioctl_getcurchan(vap, ireq);
   1151  1.60.18.1      phil 		break;
   1152  1.60.18.1      phil 	case IEEE80211_IOC_SHORTGI:
   1153  1.60.18.1      phil 		ireq->i_val = 0;
   1154  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI20)
   1155  1.60.18.1      phil 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI20;
   1156  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_SHORTGI40)
   1157  1.60.18.1      phil 			ireq->i_val |= IEEE80211_HTCAP_SHORTGI40;
   1158  1.60.18.1      phil 		break;
   1159  1.60.18.1      phil 	case IEEE80211_IOC_AMPDU:
   1160  1.60.18.1      phil 		ireq->i_val = 0;
   1161  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_TX)
   1162  1.60.18.1      phil 			ireq->i_val |= 1;
   1163  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_AMPDU_RX)
   1164  1.60.18.1      phil 			ireq->i_val |= 2;
   1165  1.60.18.1      phil 		break;
   1166  1.60.18.1      phil 	case IEEE80211_IOC_AMPDU_LIMIT:
   1167  1.60.18.1      phil 		/* XXX TODO: make this a per-node thing; and leave this as global */
   1168  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
   1169  1.60.18.1      phil 			ireq->i_val = vap->iv_ampdu_rxmax;
   1170  1.60.18.1      phil 		else if (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)
   1171  1.60.18.1      phil 			/*
   1172  1.60.18.1      phil 			 * XXX TODO: this isn't completely correct, as we've
   1173  1.60.18.1      phil 			 * negotiated the higher of the two.
   1174  1.60.18.1      phil 			 */
   1175  1.60.18.1      phil 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
   1176  1.60.18.1      phil 			    IEEE80211_HTCAP_MAXRXAMPDU);
   1177  1.60.18.1      phil 		else
   1178  1.60.18.1      phil 			ireq->i_val = vap->iv_ampdu_limit;
   1179  1.60.18.1      phil 		break;
   1180  1.60.18.1      phil 	case IEEE80211_IOC_AMPDU_DENSITY:
   1181  1.60.18.1      phil 		/* XXX TODO: make this a per-node thing; and leave this as global */
   1182  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA &&
   1183  1.60.18.1      phil 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
   1184  1.60.18.1      phil 			/*
   1185  1.60.18.1      phil 			 * XXX TODO: this isn't completely correct, as we've
   1186  1.60.18.1      phil 			 * negotiated the higher of the two.
   1187  1.60.18.1      phil 			 */
   1188  1.60.18.1      phil 			ireq->i_val = MS(vap->iv_bss->ni_htparam,
   1189  1.60.18.1      phil 			    IEEE80211_HTCAP_MPDUDENSITY);
   1190  1.60.18.1      phil 		else
   1191  1.60.18.1      phil 			ireq->i_val = vap->iv_ampdu_density;
   1192  1.60.18.1      phil 		break;
   1193  1.60.18.1      phil 	case IEEE80211_IOC_AMSDU:
   1194  1.60.18.1      phil 		ireq->i_val = 0;
   1195  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_TX)
   1196  1.60.18.1      phil 			ireq->i_val |= 1;
   1197  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_AMSDU_RX)
   1198  1.60.18.1      phil 			ireq->i_val |= 2;
   1199  1.60.18.1      phil 		break;
   1200  1.60.18.1      phil 	case IEEE80211_IOC_AMSDU_LIMIT:
   1201  1.60.18.1      phil 		ireq->i_val = vap->iv_amsdu_limit;	/* XXX truncation? */
   1202  1.60.18.1      phil 		break;
   1203  1.60.18.1      phil 	case IEEE80211_IOC_PUREN:
   1204  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_PUREN) != 0;
   1205  1.60.18.1      phil 		break;
   1206  1.60.18.1      phil 	case IEEE80211_IOC_DOTH:
   1207  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DOTH) != 0;
   1208  1.60.18.1      phil 		break;
   1209  1.60.18.1      phil 	case IEEE80211_IOC_REGDOMAIN:
   1210  1.60.18.1      phil 		error = ieee80211_ioctl_getregdomain(vap, ireq);
   1211  1.60.18.1      phil 		break;
   1212  1.60.18.1      phil 	case IEEE80211_IOC_ROAM:
   1213  1.60.18.1      phil 		error = ieee80211_ioctl_getroam(vap, ireq);
   1214  1.60.18.1      phil 		break;
   1215  1.60.18.1      phil 	case IEEE80211_IOC_TXPARAMS:
   1216  1.60.18.1      phil 		error = ieee80211_ioctl_gettxparams(vap, ireq);
   1217  1.60.18.1      phil 		break;
   1218  1.60.18.1      phil 	case IEEE80211_IOC_HTCOMPAT:
   1219  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags_ht & IEEE80211_FHT_HTCOMPAT) != 0;
   1220  1.60.18.1      phil 		break;
   1221  1.60.18.1      phil 	case IEEE80211_IOC_DWDS:
   1222  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags & IEEE80211_F_DWDS) != 0;
   1223  1.60.18.1      phil 		break;
   1224  1.60.18.1      phil 	case IEEE80211_IOC_INACTIVITY:
   1225  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_INACT) != 0;
   1226  1.60.18.1      phil 		break;
   1227  1.60.18.1      phil 	case IEEE80211_IOC_APPIE:
   1228  1.60.18.1      phil 		error = ieee80211_ioctl_getappie(vap, ireq);
   1229  1.60.18.1      phil 		break;
   1230  1.60.18.1      phil 	case IEEE80211_IOC_WPS:
   1231  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_WPS) != 0;
   1232  1.60.18.1      phil 		break;
   1233  1.60.18.1      phil 	case IEEE80211_IOC_TSN:
   1234  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_TSN) != 0;
   1235  1.60.18.1      phil 		break;
   1236  1.60.18.1      phil 	case IEEE80211_IOC_DFS:
   1237  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DFS) != 0;
   1238  1.60.18.1      phil 		break;
   1239  1.60.18.1      phil 	case IEEE80211_IOC_DOTD:
   1240  1.60.18.1      phil 		ireq->i_val = (vap->iv_flags_ext & IEEE80211_FEXT_DOTD) != 0;
   1241  1.60.18.1      phil 		break;
   1242  1.60.18.1      phil 	case IEEE80211_IOC_DEVCAPS:
   1243  1.60.18.1      phil 		error = ieee80211_ioctl_getdevcaps(ic, ireq);
   1244  1.60.18.1      phil 		break;
   1245  1.60.18.1      phil 	case IEEE80211_IOC_HTPROTMODE:
   1246  1.60.18.1      phil 		ireq->i_val = ic->ic_htprotmode;
   1247  1.60.18.1      phil 		break;
   1248  1.60.18.1      phil 	case IEEE80211_IOC_HTCONF:
   1249  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_HT) {
   1250  1.60.18.1      phil 			ireq->i_val = 1;
   1251  1.60.18.1      phil 			if (vap->iv_flags_ht & IEEE80211_FHT_USEHT40)
   1252  1.60.18.1      phil 				ireq->i_val |= 2;
   1253  1.60.18.1      phil 		} else
   1254  1.60.18.1      phil 			ireq->i_val = 0;
   1255  1.60.18.1      phil 		break;
   1256  1.60.18.1      phil 	case IEEE80211_IOC_STA_VLAN:
   1257  1.60.18.1      phil 		error = ieee80211_ioctl_getstavlan(vap, ireq);
   1258  1.60.18.1      phil 		break;
   1259  1.60.18.1      phil 	case IEEE80211_IOC_SMPS:
   1260  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA &&
   1261  1.60.18.1      phil 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP)) {
   1262  1.60.18.1      phil 			if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_RTS)
   1263  1.60.18.1      phil 				ireq->i_val = IEEE80211_HTCAP_SMPS_DYNAMIC;
   1264  1.60.18.1      phil 			else if (vap->iv_bss->ni_flags & IEEE80211_NODE_MIMO_PS)
   1265  1.60.18.1      phil 				ireq->i_val = IEEE80211_HTCAP_SMPS_ENA;
   1266  1.60.18.1      phil 			else
   1267  1.60.18.1      phil 				ireq->i_val = IEEE80211_HTCAP_SMPS_OFF;
   1268  1.60.18.1      phil 		} else
   1269  1.60.18.1      phil 			ireq->i_val = vap->iv_htcaps & IEEE80211_HTCAP_SMPS;
   1270  1.60.18.1      phil 		break;
   1271  1.60.18.1      phil 	case IEEE80211_IOC_RIFS:
   1272  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA &&
   1273  1.60.18.1      phil 		    (vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP))
   1274  1.60.18.1      phil 			ireq->i_val =
   1275  1.60.18.1      phil 			    (vap->iv_bss->ni_flags & IEEE80211_NODE_RIFS) != 0;
   1276  1.60.18.1      phil 		else
   1277  1.60.18.1      phil 			ireq->i_val =
   1278  1.60.18.1      phil 			    (vap->iv_flags_ht & IEEE80211_FHT_RIFS) != 0;
   1279  1.60.18.1      phil 		break;
   1280  1.60.18.1      phil 	case IEEE80211_IOC_STBC:
   1281  1.60.18.1      phil 		ireq->i_val = 0;
   1282  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_TX)
   1283  1.60.18.1      phil 			ireq->i_val |= 1;
   1284  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_STBC_RX)
   1285  1.60.18.1      phil 			ireq->i_val |= 2;
   1286  1.60.18.1      phil 		break;
   1287  1.60.18.1      phil 	case IEEE80211_IOC_LDPC:
   1288  1.60.18.1      phil 		ireq->i_val = 0;
   1289  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_TX)
   1290  1.60.18.1      phil 			ireq->i_val |= 1;
   1291  1.60.18.1      phil 		if (vap->iv_flags_ht & IEEE80211_FHT_LDPC_RX)
   1292  1.60.18.1      phil 			ireq->i_val |= 2;
   1293       1.19    dyoung 		break;
   1294       1.19    dyoung 
   1295  1.60.18.1      phil 	/* VHT */
   1296  1.60.18.1      phil 	case IEEE80211_IOC_VHTCONF:
   1297  1.60.18.1      phil 		ireq->i_val = 0;
   1298  1.60.18.1      phil 		if (vap->iv_flags_vht & IEEE80211_FVHT_VHT)
   1299  1.60.18.1      phil 			ireq->i_val |= 1;
   1300  1.60.18.1      phil 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT40)
   1301  1.60.18.1      phil 			ireq->i_val |= 2;
   1302  1.60.18.1      phil 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80)
   1303  1.60.18.1      phil 			ireq->i_val |= 4;
   1304  1.60.18.1      phil 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT80P80)
   1305  1.60.18.1      phil 			ireq->i_val |= 8;
   1306  1.60.18.1      phil 		if (vap->iv_flags_vht & IEEE80211_FVHT_USEVHT160)
   1307  1.60.18.1      phil 			ireq->i_val |= 16;
   1308  1.60.18.1      phil 		break;
   1309       1.19    dyoung 
   1310  1.60.18.1      phil 	default:
   1311  1.60.18.1      phil 		error = ieee80211_ioctl_getdefault(vap, ireq);
   1312  1.60.18.1      phil 		break;
   1313  1.60.18.1      phil 	}
   1314       1.58    martin 	return error;
   1315  1.60.18.1      phil #undef MS
   1316       1.19    dyoung }
   1317       1.19    dyoung 
   1318       1.19    dyoung static int
   1319  1.60.18.1      phil ieee80211_ioctl_setkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1320       1.19    dyoung {
   1321       1.19    dyoung 	struct ieee80211req_key ik;
   1322       1.19    dyoung 	struct ieee80211_node *ni;
   1323       1.19    dyoung 	struct ieee80211_key *wk;
   1324  1.60.18.1      phil 	uint16_t kid;
   1325  1.60.18.1      phil 	int error, i;
   1326       1.19    dyoung 
   1327       1.19    dyoung 	if (ireq->i_len != sizeof(ik))
   1328       1.19    dyoung 		return EINVAL;
   1329       1.19    dyoung 	error = copyin(ireq->i_data, &ik, sizeof(ik));
   1330       1.19    dyoung 	if (error)
   1331       1.19    dyoung 		return error;
   1332       1.19    dyoung 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
   1333       1.19    dyoung 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
   1334       1.19    dyoung 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
   1335       1.19    dyoung 		return E2BIG;
   1336       1.19    dyoung 	kid = ik.ik_keyix;
   1337       1.19    dyoung 	if (kid == IEEE80211_KEYIX_NONE) {
   1338       1.19    dyoung 		/* XXX unicast keys currently must be tx/rx */
   1339       1.19    dyoung 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
   1340       1.19    dyoung 			return EINVAL;
   1341  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA) {
   1342  1.60.18.1      phil 			ni = ieee80211_ref_node(vap->iv_bss);
   1343       1.21    dyoung 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
   1344       1.21    dyoung 				ieee80211_free_node(ni);
   1345       1.19    dyoung 				return EADDRNOTAVAIL;
   1346       1.21    dyoung 			}
   1347       1.19    dyoung 		} else {
   1348  1.60.18.1      phil 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
   1349  1.60.18.1      phil 				ik.ik_macaddr);
   1350       1.19    dyoung 			if (ni == NULL)
   1351       1.19    dyoung 				return ENOENT;
   1352       1.19    dyoung 		}
   1353       1.19    dyoung 		wk = &ni->ni_ucastkey;
   1354       1.19    dyoung 	} else {
   1355       1.19    dyoung 		if (kid >= IEEE80211_WEP_NKID)
   1356       1.19    dyoung 			return EINVAL;
   1357  1.60.18.1      phil 		wk = &vap->iv_nw_keys[kid];
   1358  1.60.18.1      phil 		/*
   1359  1.60.18.1      phil 		 * Global slots start off w/o any assigned key index.
   1360  1.60.18.1      phil 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
   1361  1.60.18.1      phil 		 */
   1362  1.60.18.1      phil 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
   1363  1.60.18.1      phil 			wk->wk_keyix = kid;
   1364       1.19    dyoung 		ni = NULL;
   1365       1.19    dyoung 	}
   1366       1.19    dyoung 	error = 0;
   1367  1.60.18.1      phil 	ieee80211_key_update_begin(vap);
   1368  1.60.18.1      phil 	if (ieee80211_crypto_newkey(vap, ik.ik_type, ik.ik_flags, wk)) {
   1369       1.19    dyoung 		wk->wk_keylen = ik.ik_keylen;
   1370       1.19    dyoung 		/* NB: MIC presence is implied by cipher type */
   1371       1.19    dyoung 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
   1372       1.19    dyoung 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
   1373  1.60.18.1      phil 		for (i = 0; i < IEEE80211_TID_SIZE; i++)
   1374  1.60.18.1      phil 			wk->wk_keyrsc[i] = ik.ik_keyrsc;
   1375       1.19    dyoung 		wk->wk_keytsc = 0;			/* new key, reset */
   1376       1.19    dyoung 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
   1377       1.19    dyoung 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
   1378  1.60.18.1      phil 		IEEE80211_ADDR_COPY(wk->wk_macaddr,
   1379  1.60.18.1      phil 		    ni != NULL ?  ni->ni_macaddr : ik.ik_macaddr);
   1380  1.60.18.1      phil 		if (!ieee80211_crypto_setkey(vap, wk))
   1381       1.19    dyoung 			error = EIO;
   1382       1.19    dyoung 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
   1383  1.60.18.1      phil 			/*
   1384  1.60.18.1      phil 			 * Inform the driver that this is the default
   1385  1.60.18.1      phil 			 * transmit key.  Now, ideally we'd just set
   1386  1.60.18.1      phil 			 * a flag in the key update that would
   1387  1.60.18.1      phil 			 * say "yes, we're the default key", but
   1388  1.60.18.1      phil 			 * that currently isn't the way the ioctl ->
   1389  1.60.18.1      phil 			 * key interface works.
   1390  1.60.18.1      phil 			 */
   1391  1.60.18.1      phil 			ieee80211_crypto_set_deftxkey(vap, kid);
   1392       1.19    dyoung 	} else
   1393       1.19    dyoung 		error = ENXIO;
   1394  1.60.18.1      phil 	ieee80211_key_update_end(vap);
   1395       1.19    dyoung 	if (ni != NULL)
   1396       1.19    dyoung 		ieee80211_free_node(ni);
   1397       1.19    dyoung 	return error;
   1398       1.19    dyoung }
   1399       1.19    dyoung 
   1400       1.19    dyoung static int
   1401  1.60.18.1      phil ieee80211_ioctl_delkey(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1402       1.19    dyoung {
   1403       1.19    dyoung 	struct ieee80211req_del_key dk;
   1404       1.19    dyoung 	int kid, error;
   1405       1.19    dyoung 
   1406       1.19    dyoung 	if (ireq->i_len != sizeof(dk))
   1407       1.19    dyoung 		return EINVAL;
   1408       1.19    dyoung 	error = copyin(ireq->i_data, &dk, sizeof(dk));
   1409       1.19    dyoung 	if (error)
   1410       1.19    dyoung 		return error;
   1411       1.19    dyoung 	kid = dk.idk_keyix;
   1412  1.60.18.1      phil 	/* XXX uint8_t -> uint16_t */
   1413  1.60.18.1      phil 	if (dk.idk_keyix == (uint8_t) IEEE80211_KEYIX_NONE) {
   1414       1.19    dyoung 		struct ieee80211_node *ni;
   1415       1.19    dyoung 
   1416  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA) {
   1417  1.60.18.1      phil 			ni = ieee80211_ref_node(vap->iv_bss);
   1418       1.21    dyoung 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
   1419       1.21    dyoung 				ieee80211_free_node(ni);
   1420       1.21    dyoung 				return EADDRNOTAVAIL;
   1421       1.21    dyoung 			}
   1422       1.21    dyoung 		} else {
   1423  1.60.18.1      phil 			ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
   1424  1.60.18.1      phil 				dk.idk_macaddr);
   1425       1.21    dyoung 			if (ni == NULL)
   1426       1.21    dyoung 				return ENOENT;
   1427       1.21    dyoung 		}
   1428       1.19    dyoung 		/* XXX error return */
   1429       1.26     skrll 		ieee80211_node_delucastkey(ni);
   1430       1.19    dyoung 		ieee80211_free_node(ni);
   1431       1.19    dyoung 	} else {
   1432       1.19    dyoung 		if (kid >= IEEE80211_WEP_NKID)
   1433       1.19    dyoung 			return EINVAL;
   1434       1.19    dyoung 		/* XXX error return */
   1435  1.60.18.1      phil 		ieee80211_crypto_delkey(vap, &vap->iv_nw_keys[kid]);
   1436       1.19    dyoung 	}
   1437       1.19    dyoung 	return 0;
   1438       1.19    dyoung }
   1439       1.19    dyoung 
   1440  1.60.18.1      phil struct mlmeop {
   1441  1.60.18.1      phil 	struct ieee80211vap *vap;
   1442  1.60.18.1      phil 	int	op;
   1443  1.60.18.1      phil 	int	reason;
   1444  1.60.18.1      phil };
   1445  1.60.18.1      phil 
   1446  1.60.18.1      phil static void
   1447  1.60.18.1      phil mlmedebug(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
   1448  1.60.18.1      phil 	int op, int reason)
   1449  1.60.18.1      phil {
   1450  1.60.18.1      phil #ifdef IEEE80211_DEBUG
   1451  1.60.18.1      phil 	static const struct {
   1452  1.60.18.1      phil 		int mask;
   1453  1.60.18.1      phil 		const char *opstr;
   1454  1.60.18.1      phil 	} ops[] = {
   1455  1.60.18.1      phil 		{ 0, "op#0" },
   1456  1.60.18.1      phil 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
   1457  1.60.18.1      phil 		  IEEE80211_MSG_ASSOC, "assoc" },
   1458  1.60.18.1      phil 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
   1459  1.60.18.1      phil 		  IEEE80211_MSG_ASSOC, "disassoc" },
   1460  1.60.18.1      phil 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
   1461  1.60.18.1      phil 		  IEEE80211_MSG_AUTH, "deauth" },
   1462  1.60.18.1      phil 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
   1463  1.60.18.1      phil 		  IEEE80211_MSG_AUTH, "authorize" },
   1464  1.60.18.1      phil 		{ IEEE80211_MSG_IOCTL | IEEE80211_MSG_STATE |
   1465  1.60.18.1      phil 		  IEEE80211_MSG_AUTH, "unauthorize" },
   1466  1.60.18.1      phil 	};
   1467  1.60.18.1      phil 
   1468  1.60.18.1      phil 	if (op == IEEE80211_MLME_AUTH) {
   1469  1.60.18.1      phil 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_IOCTL |
   1470  1.60.18.1      phil 		    IEEE80211_MSG_STATE | IEEE80211_MSG_AUTH, mac,
   1471  1.60.18.1      phil 		    "station authenticate %s via MLME (reason: %d (%s))",
   1472  1.60.18.1      phil 		    reason == IEEE80211_STATUS_SUCCESS ? "ACCEPT" : "REJECT",
   1473  1.60.18.1      phil 		    reason, ieee80211_reason_to_string(reason));
   1474  1.60.18.1      phil 	} else if (!(IEEE80211_MLME_ASSOC <= op && op <= IEEE80211_MLME_AUTH)) {
   1475  1.60.18.1      phil 		IEEE80211_NOTE_MAC(vap, IEEE80211_MSG_ANY, mac,
   1476  1.60.18.1      phil 		    "unknown MLME request %d (reason: %d (%s))", op, reason,
   1477  1.60.18.1      phil 		    ieee80211_reason_to_string(reason));
   1478  1.60.18.1      phil 	} else if (reason == IEEE80211_STATUS_SUCCESS) {
   1479  1.60.18.1      phil 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
   1480  1.60.18.1      phil 		    "station %s via MLME", ops[op].opstr);
   1481  1.60.18.1      phil 	} else {
   1482  1.60.18.1      phil 		IEEE80211_NOTE_MAC(vap, ops[op].mask, mac,
   1483  1.60.18.1      phil 		    "station %s via MLME (reason: %d (%s))", ops[op].opstr,
   1484  1.60.18.1      phil 		    reason, ieee80211_reason_to_string(reason));
   1485  1.60.18.1      phil 	}
   1486  1.60.18.1      phil #endif /* IEEE80211_DEBUG */
   1487  1.60.18.1      phil }
   1488  1.60.18.1      phil 
   1489       1.19    dyoung static void
   1490       1.19    dyoung domlme(void *arg, struct ieee80211_node *ni)
   1491       1.19    dyoung {
   1492  1.60.18.1      phil 	struct mlmeop *mop = arg;
   1493  1.60.18.1      phil 	struct ieee80211vap *vap = ni->ni_vap;
   1494       1.19    dyoung 
   1495  1.60.18.1      phil 	if (vap != mop->vap)
   1496  1.60.18.1      phil 		return;
   1497  1.60.18.1      phil 	/*
   1498  1.60.18.1      phil 	 * NB: if ni_associd is zero then the node is already cleaned
   1499  1.60.18.1      phil 	 * up and we don't need to do this (we're safely holding a
   1500  1.60.18.1      phil 	 * reference but should otherwise not modify it's state).
   1501  1.60.18.1      phil 	 */
   1502  1.60.18.1      phil 	if (ni->ni_associd == 0)
   1503  1.60.18.1      phil 		return;
   1504  1.60.18.1      phil 	mlmedebug(vap, ni->ni_macaddr, mop->op, mop->reason);
   1505  1.60.18.1      phil 	if (mop->op == IEEE80211_MLME_DEAUTH) {
   1506  1.60.18.1      phil 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
   1507  1.60.18.1      phil 		    mop->reason);
   1508  1.60.18.1      phil 	} else {
   1509  1.60.18.1      phil 		IEEE80211_SEND_MGMT(ni, IEEE80211_FC0_SUBTYPE_DISASSOC,
   1510  1.60.18.1      phil 		    mop->reason);
   1511       1.19    dyoung 	}
   1512  1.60.18.1      phil 	ieee80211_node_leave(ni);
   1513       1.19    dyoung }
   1514       1.19    dyoung 
   1515       1.19    dyoung static int
   1516  1.60.18.1      phil setmlme_dropsta(struct ieee80211vap *vap,
   1517  1.60.18.1      phil 	const uint8_t mac[IEEE80211_ADDR_LEN], struct mlmeop *mlmeop)
   1518       1.19    dyoung {
   1519  1.60.18.1      phil 	struct ieee80211_node_table *nt = &vap->iv_ic->ic_sta;
   1520       1.19    dyoung 	struct ieee80211_node *ni;
   1521  1.60.18.1      phil 	int error = 0;
   1522       1.19    dyoung 
   1523  1.60.18.1      phil 	/* NB: the broadcast address means do 'em all */
   1524  1.60.18.1      phil 	if (!IEEE80211_ADDR_EQ(mac, vap->iv_ifp->if_broadcastaddr)) {
   1525  1.60.18.1      phil 		IEEE80211_NODE_LOCK(nt);
   1526  1.60.18.1      phil 		ni = ieee80211_find_node_locked(nt, mac);
   1527  1.60.18.1      phil 		IEEE80211_NODE_UNLOCK(nt);
   1528  1.60.18.1      phil 		/*
   1529  1.60.18.1      phil 		 * Don't do the node update inside the node
   1530  1.60.18.1      phil 		 * table lock.  This unfortunately causes LORs
   1531  1.60.18.1      phil 		 * with drivers and their TX paths.
   1532  1.60.18.1      phil 		 */
   1533  1.60.18.1      phil 		if (ni != NULL) {
   1534  1.60.18.1      phil 			domlme(mlmeop, ni);
   1535       1.19    dyoung 			ieee80211_free_node(ni);
   1536  1.60.18.1      phil 		} else
   1537  1.60.18.1      phil 			error = ENOENT;
   1538  1.60.18.1      phil 	} else {
   1539  1.60.18.1      phil 		ieee80211_iterate_nodes(nt, domlme, mlmeop);
   1540  1.60.18.1      phil 	}
   1541  1.60.18.1      phil 	return error;
   1542  1.60.18.1      phil }
   1543  1.60.18.1      phil 
   1544  1.60.18.1      phil static int
   1545  1.60.18.1      phil setmlme_common(struct ieee80211vap *vap, int op,
   1546  1.60.18.1      phil 	const uint8_t mac[IEEE80211_ADDR_LEN], int reason)
   1547  1.60.18.1      phil {
   1548  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1549  1.60.18.1      phil 	struct ieee80211_node_table *nt = &ic->ic_sta;
   1550  1.60.18.1      phil 	struct ieee80211_node *ni;
   1551  1.60.18.1      phil 	struct mlmeop mlmeop;
   1552  1.60.18.1      phil 	int error;
   1553  1.60.18.1      phil 
   1554  1.60.18.1      phil 	error = 0;
   1555  1.60.18.1      phil 	switch (op) {
   1556  1.60.18.1      phil 	case IEEE80211_MLME_DISASSOC:
   1557  1.60.18.1      phil 	case IEEE80211_MLME_DEAUTH:
   1558  1.60.18.1      phil 		switch (vap->iv_opmode) {
   1559  1.60.18.1      phil 		case IEEE80211_M_STA:
   1560  1.60.18.1      phil 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
   1561       1.19    dyoung 			/* XXX not quite right */
   1562  1.60.18.1      phil 			ieee80211_new_state(vap, IEEE80211_S_INIT, reason);
   1563       1.19    dyoung 			break;
   1564       1.19    dyoung 		case IEEE80211_M_HOSTAP:
   1565  1.60.18.1      phil 			mlmeop.vap = vap;
   1566  1.60.18.1      phil 			mlmeop.op = op;
   1567  1.60.18.1      phil 			mlmeop.reason = reason;
   1568  1.60.18.1      phil 			error = setmlme_dropsta(vap, mac, &mlmeop);
   1569  1.60.18.1      phil 			break;
   1570  1.60.18.1      phil 		case IEEE80211_M_WDS:
   1571  1.60.18.1      phil 			/* XXX user app should send raw frame? */
   1572  1.60.18.1      phil 			if (op != IEEE80211_MLME_DEAUTH) {
   1573  1.60.18.1      phil 				error = EINVAL;
   1574  1.60.18.1      phil 				break;
   1575  1.60.18.1      phil 			}
   1576  1.60.18.1      phil #if 0
   1577  1.60.18.1      phil 			/* XXX accept any address, simplifies user code */
   1578  1.60.18.1      phil 			if (!IEEE80211_ADDR_EQ(mac, vap->iv_bss->ni_macaddr)) {
   1579  1.60.18.1      phil 				error = EINVAL;
   1580  1.60.18.1      phil 				break;
   1581  1.60.18.1      phil 			}
   1582  1.60.18.1      phil #endif
   1583  1.60.18.1      phil 			mlmedebug(vap, vap->iv_bss->ni_macaddr, op, reason);
   1584  1.60.18.1      phil 			ni = ieee80211_ref_node(vap->iv_bss);
   1585  1.60.18.1      phil 			IEEE80211_SEND_MGMT(ni,
   1586  1.60.18.1      phil 			    IEEE80211_FC0_SUBTYPE_DEAUTH, reason);
   1587  1.60.18.1      phil 			ieee80211_free_node(ni);
   1588  1.60.18.1      phil 			break;
   1589  1.60.18.1      phil 		case IEEE80211_M_MBSS:
   1590  1.60.18.1      phil 			IEEE80211_NODE_LOCK(nt);
   1591  1.60.18.1      phil 			ni = ieee80211_find_node_locked(nt, mac);
   1592  1.60.18.1      phil 			/*
   1593  1.60.18.1      phil 			 * Don't do the node update inside the node
   1594  1.60.18.1      phil 			 * table lock.  This unfortunately causes LORs
   1595  1.60.18.1      phil 			 * with drivers and their TX paths.
   1596  1.60.18.1      phil 			 */
   1597  1.60.18.1      phil 			IEEE80211_NODE_UNLOCK(nt);
   1598  1.60.18.1      phil 			if (ni != NULL) {
   1599  1.60.18.1      phil 				ieee80211_node_leave(ni);
   1600       1.19    dyoung 				ieee80211_free_node(ni);
   1601        1.1    dyoung 			} else {
   1602  1.60.18.1      phil 				error = ENOENT;
   1603        1.1    dyoung 			}
   1604        1.1    dyoung 			break;
   1605       1.19    dyoung 		default:
   1606  1.60.18.1      phil 			error = EINVAL;
   1607  1.60.18.1      phil 			break;
   1608       1.19    dyoung 		}
   1609       1.19    dyoung 		break;
   1610       1.19    dyoung 	case IEEE80211_MLME_AUTHORIZE:
   1611       1.19    dyoung 	case IEEE80211_MLME_UNAUTHORIZE:
   1612  1.60.18.1      phil 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
   1613  1.60.18.1      phil 		    vap->iv_opmode != IEEE80211_M_WDS) {
   1614  1.60.18.1      phil 			error = EINVAL;
   1615  1.60.18.1      phil 			break;
   1616  1.60.18.1      phil 		}
   1617  1.60.18.1      phil 		IEEE80211_NODE_LOCK(nt);
   1618  1.60.18.1      phil 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
   1619  1.60.18.1      phil 		/*
   1620  1.60.18.1      phil 		 * Don't do the node update inside the node
   1621  1.60.18.1      phil 		 * table lock.  This unfortunately causes LORs
   1622  1.60.18.1      phil 		 * with drivers and their TX paths.
   1623  1.60.18.1      phil 		 */
   1624  1.60.18.1      phil 		IEEE80211_NODE_UNLOCK(nt);
   1625  1.60.18.1      phil 		if (ni != NULL) {
   1626  1.60.18.1      phil 			mlmedebug(vap, mac, op, reason);
   1627  1.60.18.1      phil 			if (op == IEEE80211_MLME_AUTHORIZE)
   1628  1.60.18.1      phil 				ieee80211_node_authorize(ni);
   1629  1.60.18.1      phil 			else
   1630  1.60.18.1      phil 				ieee80211_node_unauthorize(ni);
   1631  1.60.18.1      phil 			ieee80211_free_node(ni);
   1632  1.60.18.1      phil 		} else
   1633  1.60.18.1      phil 			error = ENOENT;
   1634  1.60.18.1      phil 		break;
   1635  1.60.18.1      phil 	case IEEE80211_MLME_AUTH:
   1636  1.60.18.1      phil 		if (vap->iv_opmode != IEEE80211_M_HOSTAP) {
   1637  1.60.18.1      phil 			error = EINVAL;
   1638  1.60.18.1      phil 			break;
   1639  1.60.18.1      phil 		}
   1640  1.60.18.1      phil 		IEEE80211_NODE_LOCK(nt);
   1641  1.60.18.1      phil 		ni = ieee80211_find_vap_node_locked(nt, vap, mac);
   1642  1.60.18.1      phil 		/*
   1643  1.60.18.1      phil 		 * Don't do the node update inside the node
   1644  1.60.18.1      phil 		 * table lock.  This unfortunately causes LORs
   1645  1.60.18.1      phil 		 * with drivers and their TX paths.
   1646  1.60.18.1      phil 		 */
   1647  1.60.18.1      phil 		IEEE80211_NODE_UNLOCK(nt);
   1648  1.60.18.1      phil 		if (ni != NULL) {
   1649  1.60.18.1      phil 			mlmedebug(vap, mac, op, reason);
   1650  1.60.18.1      phil 			if (reason == IEEE80211_STATUS_SUCCESS) {
   1651  1.60.18.1      phil 				IEEE80211_SEND_MGMT(ni,
   1652  1.60.18.1      phil 				    IEEE80211_FC0_SUBTYPE_AUTH, 2);
   1653  1.60.18.1      phil 				/*
   1654  1.60.18.1      phil 				 * For shared key auth, just continue the
   1655  1.60.18.1      phil 				 * exchange.  Otherwise when 802.1x is not in
   1656  1.60.18.1      phil 				 * use mark the port authorized at this point
   1657  1.60.18.1      phil 				 * so traffic can flow.
   1658  1.60.18.1      phil 				 */
   1659  1.60.18.1      phil 				if (ni->ni_authmode != IEEE80211_AUTH_8021X &&
   1660  1.60.18.1      phil 				    ni->ni_challenge == NULL)
   1661  1.60.18.1      phil 				      ieee80211_node_authorize(ni);
   1662  1.60.18.1      phil 			} else {
   1663  1.60.18.1      phil 				vap->iv_stats.is_rx_acl++;
   1664  1.60.18.1      phil 				ieee80211_send_error(ni, ni->ni_macaddr,
   1665  1.60.18.1      phil 				    IEEE80211_FC0_SUBTYPE_AUTH, 2|(reason<<16));
   1666  1.60.18.1      phil 				ieee80211_node_leave(ni);
   1667  1.60.18.1      phil 			}
   1668  1.60.18.1      phil 			ieee80211_free_node(ni);
   1669  1.60.18.1      phil 		} else
   1670  1.60.18.1      phil 			error = ENOENT;
   1671       1.19    dyoung 		break;
   1672       1.19    dyoung 	default:
   1673  1.60.18.1      phil 		error = EINVAL;
   1674  1.60.18.1      phil 		break;
   1675       1.19    dyoung 	}
   1676  1.60.18.1      phil 	return error;
   1677  1.60.18.1      phil }
   1678  1.60.18.1      phil 
   1679  1.60.18.1      phil struct scanlookup {
   1680  1.60.18.1      phil 	const uint8_t *mac;
   1681  1.60.18.1      phil 	int esslen;
   1682  1.60.18.1      phil 	const uint8_t *essid;
   1683  1.60.18.1      phil 	const struct ieee80211_scan_entry *se;
   1684  1.60.18.1      phil };
   1685  1.60.18.1      phil 
   1686  1.60.18.1      phil /*
   1687  1.60.18.1      phil  * Match mac address and any ssid.
   1688  1.60.18.1      phil  */
   1689  1.60.18.1      phil static void
   1690  1.60.18.1      phil mlmelookup(void *arg, const struct ieee80211_scan_entry *se)
   1691  1.60.18.1      phil {
   1692  1.60.18.1      phil 	struct scanlookup *look = arg;
   1693  1.60.18.1      phil 
   1694  1.60.18.1      phil 	if (!IEEE80211_ADDR_EQ(look->mac, se->se_macaddr))
   1695  1.60.18.1      phil 		return;
   1696  1.60.18.1      phil 	if (look->esslen != 0) {
   1697  1.60.18.1      phil 		if (se->se_ssid[1] != look->esslen)
   1698  1.60.18.1      phil 			return;
   1699  1.60.18.1      phil 		if (memcmp(look->essid, se->se_ssid+2, look->esslen))
   1700  1.60.18.1      phil 			return;
   1701  1.60.18.1      phil 	}
   1702  1.60.18.1      phil 	look->se = se;
   1703  1.60.18.1      phil }
   1704  1.60.18.1      phil 
   1705  1.60.18.1      phil static int
   1706  1.60.18.1      phil setmlme_assoc_sta(struct ieee80211vap *vap,
   1707  1.60.18.1      phil 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
   1708  1.60.18.1      phil 	const uint8_t ssid[IEEE80211_NWID_LEN])
   1709  1.60.18.1      phil {
   1710  1.60.18.1      phil 	struct scanlookup lookup;
   1711  1.60.18.1      phil 
   1712  1.60.18.1      phil 	KASSERT(vap->iv_opmode == IEEE80211_M_STA,
   1713  1.60.18.1      phil 	    ("expected opmode STA not %s",
   1714  1.60.18.1      phil 	    ieee80211_opmode_name[vap->iv_opmode]));
   1715  1.60.18.1      phil 
   1716  1.60.18.1      phil 	/* NB: this is racey if roaming is !manual */
   1717  1.60.18.1      phil 	lookup.se = NULL;
   1718  1.60.18.1      phil 	lookup.mac = mac;
   1719  1.60.18.1      phil 	lookup.esslen = ssid_len;
   1720  1.60.18.1      phil 	lookup.essid = ssid;
   1721  1.60.18.1      phil 	ieee80211_scan_iterate(vap, mlmelookup, &lookup);
   1722  1.60.18.1      phil 	if (lookup.se == NULL)
   1723  1.60.18.1      phil 		return ENOENT;
   1724  1.60.18.1      phil 	mlmedebug(vap, mac, IEEE80211_MLME_ASSOC, 0);
   1725  1.60.18.1      phil 	if (!ieee80211_sta_join(vap, lookup.se->se_chan, lookup.se))
   1726  1.60.18.1      phil 		return EIO;		/* XXX unique but could be better */
   1727       1.19    dyoung 	return 0;
   1728       1.19    dyoung }
   1729       1.19    dyoung 
   1730       1.19    dyoung static int
   1731  1.60.18.1      phil setmlme_assoc_adhoc(struct ieee80211vap *vap,
   1732  1.60.18.1      phil 	const uint8_t mac[IEEE80211_ADDR_LEN], int ssid_len,
   1733  1.60.18.1      phil 	const uint8_t ssid[IEEE80211_NWID_LEN])
   1734  1.60.18.1      phil {
   1735  1.60.18.1      phil 	struct ieee80211_scan_req *sr;
   1736  1.60.18.1      phil 	int error;
   1737  1.60.18.1      phil 
   1738  1.60.18.1      phil 	KASSERT(vap->iv_opmode == IEEE80211_M_IBSS ||
   1739  1.60.18.1      phil 	    vap->iv_opmode == IEEE80211_M_AHDEMO,
   1740  1.60.18.1      phil 	    ("expected opmode IBSS or AHDEMO not %s",
   1741  1.60.18.1      phil 	    ieee80211_opmode_name[vap->iv_opmode]));
   1742  1.60.18.1      phil 
   1743  1.60.18.1      phil 	if (ssid_len == 0)
   1744  1.60.18.1      phil 		return EINVAL;
   1745  1.60.18.1      phil 
   1746  1.60.18.1      phil 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
   1747  1.60.18.1      phil 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   1748  1.60.18.1      phil 	if (sr == NULL)
   1749  1.60.18.1      phil 		return ENOMEM;
   1750  1.60.18.1      phil 
   1751  1.60.18.1      phil 	/* NB: IEEE80211_IOC_SSID call missing for ap_scan=2. */
   1752  1.60.18.1      phil 	memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
   1753  1.60.18.1      phil 	vap->iv_des_ssid[0].len = ssid_len;
   1754  1.60.18.1      phil 	memcpy(vap->iv_des_ssid[0].ssid, ssid, ssid_len);
   1755  1.60.18.1      phil 	vap->iv_des_nssid = 1;
   1756  1.60.18.1      phil 
   1757  1.60.18.1      phil 	sr->sr_flags = IEEE80211_IOC_SCAN_ACTIVE | IEEE80211_IOC_SCAN_ONCE;
   1758  1.60.18.1      phil 	sr->sr_duration = IEEE80211_IOC_SCAN_FOREVER;
   1759  1.60.18.1      phil 	memcpy(sr->sr_ssid[0].ssid, ssid, ssid_len);
   1760  1.60.18.1      phil 	sr->sr_ssid[0].len = ssid_len;
   1761  1.60.18.1      phil 	sr->sr_nssid = 1;
   1762  1.60.18.1      phil 
   1763  1.60.18.1      phil 	error = ieee80211_scanreq(vap, sr);
   1764  1.60.18.1      phil 
   1765  1.60.18.1      phil 	IEEE80211_FREE(sr, M_TEMP);
   1766  1.60.18.1      phil 	return error;
   1767  1.60.18.1      phil }
   1768  1.60.18.1      phil 
   1769  1.60.18.1      phil static int
   1770  1.60.18.1      phil ieee80211_ioctl_setmlme(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1771  1.60.18.1      phil {
   1772  1.60.18.1      phil 	struct ieee80211req_mlme mlme;
   1773  1.60.18.1      phil 	int error;
   1774  1.60.18.1      phil 
   1775  1.60.18.1      phil 	if (ireq->i_len != sizeof(mlme))
   1776  1.60.18.1      phil 		return EINVAL;
   1777  1.60.18.1      phil 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
   1778  1.60.18.1      phil 	if (error)
   1779  1.60.18.1      phil 		return error;
   1780  1.60.18.1      phil 	if  (vap->iv_opmode == IEEE80211_M_STA &&
   1781  1.60.18.1      phil 	    mlme.im_op == IEEE80211_MLME_ASSOC)
   1782  1.60.18.1      phil 		return setmlme_assoc_sta(vap, mlme.im_macaddr,
   1783  1.60.18.1      phil 		    vap->iv_des_ssid[0].len, vap->iv_des_ssid[0].ssid);
   1784  1.60.18.1      phil 	else if ((vap->iv_opmode == IEEE80211_M_IBSS ||
   1785  1.60.18.1      phil 	    vap->iv_opmode == IEEE80211_M_AHDEMO) &&
   1786  1.60.18.1      phil 	    mlme.im_op == IEEE80211_MLME_ASSOC)
   1787  1.60.18.1      phil 		return setmlme_assoc_adhoc(vap, mlme.im_macaddr,
   1788  1.60.18.1      phil 		    mlme.im_ssid_len, mlme.im_ssid);
   1789  1.60.18.1      phil 	else
   1790  1.60.18.1      phil 		return setmlme_common(vap, mlme.im_op,
   1791  1.60.18.1      phil 		    mlme.im_macaddr, mlme.im_reason);
   1792  1.60.18.1      phil }
   1793  1.60.18.1      phil 
   1794  1.60.18.1      phil static int
   1795  1.60.18.1      phil ieee80211_ioctl_macmac(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1796       1.19    dyoung {
   1797  1.60.18.1      phil 	uint8_t mac[IEEE80211_ADDR_LEN];
   1798  1.60.18.1      phil 	const struct ieee80211_aclator *acl = vap->iv_acl;
   1799       1.19    dyoung 	int error;
   1800       1.19    dyoung 
   1801       1.19    dyoung 	if (ireq->i_len != sizeof(mac))
   1802       1.19    dyoung 		return EINVAL;
   1803       1.19    dyoung 	error = copyin(ireq->i_data, mac, ireq->i_len);
   1804       1.19    dyoung 	if (error)
   1805       1.19    dyoung 		return error;
   1806       1.19    dyoung 	if (acl == NULL) {
   1807       1.19    dyoung 		acl = ieee80211_aclator_get("mac");
   1808  1.60.18.1      phil 		if (acl == NULL || !acl->iac_attach(vap))
   1809       1.19    dyoung 			return EINVAL;
   1810  1.60.18.1      phil 		vap->iv_acl = acl;
   1811       1.19    dyoung 	}
   1812       1.19    dyoung 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
   1813  1.60.18.1      phil 		acl->iac_add(vap, mac);
   1814       1.19    dyoung 	else
   1815  1.60.18.1      phil 		acl->iac_remove(vap, mac);
   1816       1.19    dyoung 	return 0;
   1817       1.19    dyoung }
   1818       1.19    dyoung 
   1819       1.19    dyoung static int
   1820  1.60.18.1      phil ieee80211_ioctl_setmaccmd(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1821       1.19    dyoung {
   1822  1.60.18.1      phil 	const struct ieee80211_aclator *acl = vap->iv_acl;
   1823       1.19    dyoung 
   1824       1.19    dyoung 	switch (ireq->i_val) {
   1825       1.19    dyoung 	case IEEE80211_MACCMD_POLICY_OPEN:
   1826       1.19    dyoung 	case IEEE80211_MACCMD_POLICY_ALLOW:
   1827       1.19    dyoung 	case IEEE80211_MACCMD_POLICY_DENY:
   1828  1.60.18.1      phil 	case IEEE80211_MACCMD_POLICY_RADIUS:
   1829       1.19    dyoung 		if (acl == NULL) {
   1830       1.19    dyoung 			acl = ieee80211_aclator_get("mac");
   1831  1.60.18.1      phil 			if (acl == NULL || !acl->iac_attach(vap))
   1832       1.19    dyoung 				return EINVAL;
   1833  1.60.18.1      phil 			vap->iv_acl = acl;
   1834       1.19    dyoung 		}
   1835  1.60.18.1      phil 		acl->iac_setpolicy(vap, ireq->i_val);
   1836       1.19    dyoung 		break;
   1837       1.19    dyoung 	case IEEE80211_MACCMD_FLUSH:
   1838       1.19    dyoung 		if (acl != NULL)
   1839  1.60.18.1      phil 			acl->iac_flush(vap);
   1840       1.19    dyoung 		/* NB: silently ignore when not in use */
   1841       1.19    dyoung 		break;
   1842       1.19    dyoung 	case IEEE80211_MACCMD_DETACH:
   1843       1.19    dyoung 		if (acl != NULL) {
   1844  1.60.18.1      phil 			vap->iv_acl = NULL;
   1845  1.60.18.1      phil 			acl->iac_detach(vap);
   1846       1.19    dyoung 		}
   1847       1.19    dyoung 		break;
   1848       1.19    dyoung 	default:
   1849       1.26     skrll 		if (acl == NULL)
   1850       1.26     skrll 			return EINVAL;
   1851       1.26     skrll 		else
   1852  1.60.18.1      phil 			return acl->iac_setioctl(vap, ireq);
   1853       1.19    dyoung 	}
   1854       1.19    dyoung 	return 0;
   1855       1.19    dyoung }
   1856       1.19    dyoung 
   1857       1.19    dyoung static int
   1858  1.60.18.1      phil ieee80211_ioctl_setchanlist(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1859       1.19    dyoung {
   1860  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1861  1.60.18.1      phil 	uint8_t *chanlist, *list;
   1862  1.60.18.1      phil 	int i, nchan, maxchan, error;
   1863  1.60.18.1      phil 
   1864  1.60.18.1      phil 	if (ireq->i_len > sizeof(ic->ic_chan_active))
   1865  1.60.18.1      phil 		ireq->i_len = sizeof(ic->ic_chan_active);
   1866  1.60.18.1      phil 	list = IEEE80211_MALLOC(ireq->i_len + IEEE80211_CHAN_BYTES, M_TEMP,
   1867  1.60.18.1      phil 	    IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   1868  1.60.18.1      phil 	if (list == NULL)
   1869  1.60.18.1      phil 		return ENOMEM;
   1870  1.60.18.1      phil 	error = copyin(ireq->i_data, list, ireq->i_len);
   1871  1.60.18.1      phil 	if (error) {
   1872  1.60.18.1      phil 		IEEE80211_FREE(list, M_TEMP);
   1873       1.19    dyoung 		return error;
   1874  1.60.18.1      phil 	}
   1875  1.60.18.1      phil 	nchan = 0;
   1876  1.60.18.1      phil 	chanlist = list + ireq->i_len;		/* NB: zero'd already */
   1877  1.60.18.1      phil 	maxchan = ireq->i_len * NBBY;
   1878  1.60.18.1      phil 	for (i = 0; i < ic->ic_nchans; i++) {
   1879  1.60.18.1      phil 		const struct ieee80211_channel *c = &ic->ic_channels[i];
   1880       1.19    dyoung 		/*
   1881  1.60.18.1      phil 		 * Calculate the intersection of the user list and the
   1882  1.60.18.1      phil 		 * available channels so users can do things like specify
   1883  1.60.18.1      phil 		 * 1-255 to get all available channels.
   1884       1.19    dyoung 		 */
   1885  1.60.18.1      phil 		if (c->ic_ieee < maxchan && isset(list, c->ic_ieee)) {
   1886  1.60.18.1      phil 			setbit(chanlist, c->ic_ieee);
   1887  1.60.18.1      phil 			nchan++;
   1888  1.60.18.1      phil 		}
   1889       1.19    dyoung 	}
   1890  1.60.18.1      phil 	if (nchan == 0) {
   1891  1.60.18.1      phil 		IEEE80211_FREE(list, M_TEMP);
   1892  1.60.18.1      phil 		return EINVAL;
   1893       1.19    dyoung 	}
   1894  1.60.18.1      phil 	if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&	/* XXX */
   1895  1.60.18.1      phil 	    isclr(chanlist, ic->ic_bsschan->ic_ieee))
   1896  1.60.18.1      phil 		ic->ic_bsschan = IEEE80211_CHAN_ANYC;
   1897  1.60.18.1      phil 	memcpy(ic->ic_chan_active, chanlist, IEEE80211_CHAN_BYTES);
   1898  1.60.18.1      phil 	ieee80211_scan_flush(vap);
   1899  1.60.18.1      phil 	IEEE80211_FREE(list, M_TEMP);
   1900  1.60.18.1      phil 	return ENETRESET;
   1901  1.60.18.1      phil }
   1902  1.60.18.1      phil 
   1903  1.60.18.1      phil static int
   1904  1.60.18.1      phil ieee80211_ioctl_setstastats(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1905  1.60.18.1      phil {
   1906  1.60.18.1      phil 	struct ieee80211_node *ni;
   1907  1.60.18.1      phil 	uint8_t macaddr[IEEE80211_ADDR_LEN];
   1908  1.60.18.1      phil 	int error;
   1909  1.60.18.1      phil 
   1910  1.60.18.1      phil 	/*
   1911  1.60.18.1      phil 	 * NB: we could copyin ieee80211req_sta_stats so apps
   1912  1.60.18.1      phil 	 *     could make selective changes but that's overkill;
   1913  1.60.18.1      phil 	 *     just clear all stats for now.
   1914  1.60.18.1      phil 	 */
   1915  1.60.18.1      phil 	if (ireq->i_len < IEEE80211_ADDR_LEN)
   1916  1.60.18.1      phil 		return EINVAL;
   1917  1.60.18.1      phil 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
   1918  1.60.18.1      phil 	if (error != 0)
   1919  1.60.18.1      phil 		return error;
   1920  1.60.18.1      phil 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, macaddr);
   1921  1.60.18.1      phil 	if (ni == NULL)
   1922  1.60.18.1      phil 		return ENOENT;
   1923  1.60.18.1      phil 	/* XXX require ni_vap == vap? */
   1924  1.60.18.1      phil 	memset(&ni->ni_stats, 0, sizeof(ni->ni_stats));
   1925  1.60.18.1      phil 	ieee80211_free_node(ni);
   1926  1.60.18.1      phil 	return 0;
   1927       1.19    dyoung }
   1928       1.19    dyoung 
   1929       1.19    dyoung static int
   1930  1.60.18.1      phil ieee80211_ioctl_setstatxpow(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1931       1.19    dyoung {
   1932       1.19    dyoung 	struct ieee80211_node *ni;
   1933       1.19    dyoung 	struct ieee80211req_sta_txpow txpow;
   1934       1.19    dyoung 	int error;
   1935       1.19    dyoung 
   1936       1.19    dyoung 	if (ireq->i_len != sizeof(txpow))
   1937       1.19    dyoung 		return EINVAL;
   1938       1.19    dyoung 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
   1939       1.19    dyoung 	if (error != 0)
   1940       1.19    dyoung 		return error;
   1941  1.60.18.1      phil 	ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap, txpow.it_macaddr);
   1942       1.19    dyoung 	if (ni == NULL)
   1943  1.60.18.1      phil 		return ENOENT;
   1944       1.19    dyoung 	ni->ni_txpower = txpow.it_txpow;
   1945       1.19    dyoung 	ieee80211_free_node(ni);
   1946       1.19    dyoung 	return error;
   1947       1.19    dyoung }
   1948       1.19    dyoung 
   1949       1.19    dyoung static int
   1950  1.60.18.1      phil ieee80211_ioctl_setwmeparam(struct ieee80211vap *vap, struct ieee80211req *ireq)
   1951       1.19    dyoung {
   1952  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   1953       1.19    dyoung 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1954       1.19    dyoung 	struct wmeParams *wmep, *chanp;
   1955  1.60.18.1      phil 	int isbss, ac, aggrmode;
   1956       1.19    dyoung 
   1957       1.19    dyoung 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   1958  1.60.18.1      phil 		return EOPNOTSUPP;
   1959       1.19    dyoung 
   1960       1.19    dyoung 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
   1961       1.19    dyoung 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
   1962  1.60.18.1      phil 	aggrmode = (wme->wme_flags & WME_F_AGGRMODE);
   1963       1.19    dyoung 	if (ac >= WME_NUM_AC)
   1964       1.19    dyoung 		ac = WME_AC_BE;
   1965       1.19    dyoung 	if (isbss) {
   1966       1.19    dyoung 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
   1967       1.19    dyoung 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1968       1.19    dyoung 	} else {
   1969       1.19    dyoung 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
   1970       1.19    dyoung 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1971       1.19    dyoung 	}
   1972       1.19    dyoung 	switch (ireq->i_type) {
   1973       1.19    dyoung 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1974  1.60.18.1      phil 		wmep->wmep_logcwmin = ireq->i_val;
   1975  1.60.18.1      phil 		if (!isbss || !aggrmode)
   1976  1.60.18.1      phil 			chanp->wmep_logcwmin = ireq->i_val;
   1977       1.19    dyoung 		break;
   1978       1.19    dyoung 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1979  1.60.18.1      phil 		wmep->wmep_logcwmax = ireq->i_val;
   1980  1.60.18.1      phil 		if (!isbss || !aggrmode)
   1981  1.60.18.1      phil 			chanp->wmep_logcwmax = ireq->i_val;
   1982       1.19    dyoung 		break;
   1983       1.19    dyoung 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1984  1.60.18.1      phil 		wmep->wmep_aifsn = ireq->i_val;
   1985  1.60.18.1      phil 		if (!isbss || !aggrmode)
   1986  1.60.18.1      phil 			chanp->wmep_aifsn = ireq->i_val;
   1987       1.19    dyoung 		break;
   1988       1.19    dyoung 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1989  1.60.18.1      phil 		wmep->wmep_txopLimit = ireq->i_val;
   1990  1.60.18.1      phil 		if (!isbss || !aggrmode)
   1991  1.60.18.1      phil 			chanp->wmep_txopLimit = ireq->i_val;
   1992       1.19    dyoung 		break;
   1993       1.19    dyoung 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1994       1.19    dyoung 		wmep->wmep_acm = ireq->i_val;
   1995  1.60.18.1      phil 		if (!aggrmode)
   1996       1.19    dyoung 			chanp->wmep_acm = ireq->i_val;
   1997       1.19    dyoung 		break;
   1998       1.19    dyoung 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
   1999       1.19    dyoung 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
   2000       1.19    dyoung 			(ireq->i_val) == 0;
   2001       1.19    dyoung 		break;
   2002       1.19    dyoung 	}
   2003  1.60.18.1      phil 	ieee80211_wme_updateparams(vap);
   2004       1.19    dyoung 	return 0;
   2005       1.19    dyoung }
   2006       1.19    dyoung 
   2007       1.19    dyoung static int
   2008  1.60.18.1      phil find11gchannel(struct ieee80211com *ic, int start, int freq)
   2009       1.19    dyoung {
   2010  1.60.18.1      phil 	const struct ieee80211_channel *c;
   2011  1.60.18.1      phil 	int i;
   2012  1.60.18.1      phil 
   2013  1.60.18.1      phil 	for (i = start+1; i < ic->ic_nchans; i++) {
   2014  1.60.18.1      phil 		c = &ic->ic_channels[i];
   2015  1.60.18.1      phil 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
   2016  1.60.18.1      phil 			return 1;
   2017  1.60.18.1      phil 	}
   2018  1.60.18.1      phil 	/* NB: should not be needed but in case things are mis-sorted */
   2019  1.60.18.1      phil 	for (i = 0; i < start; i++) {
   2020  1.60.18.1      phil 		c = &ic->ic_channels[i];
   2021  1.60.18.1      phil 		if (c->ic_freq == freq && IEEE80211_IS_CHAN_ANYG(c))
   2022  1.60.18.1      phil 			return 1;
   2023       1.19    dyoung 	}
   2024       1.19    dyoung 	return 0;
   2025       1.19    dyoung }
   2026       1.19    dyoung 
   2027  1.60.18.1      phil static struct ieee80211_channel *
   2028  1.60.18.1      phil findchannel(struct ieee80211com *ic, int ieee, int mode)
   2029  1.60.18.1      phil {
   2030  1.60.18.1      phil 	static const u_int chanflags[IEEE80211_MODE_MAX] = {
   2031  1.60.18.1      phil 	    [IEEE80211_MODE_AUTO]	= 0,
   2032  1.60.18.1      phil 	    [IEEE80211_MODE_11A]	= IEEE80211_CHAN_A,
   2033  1.60.18.1      phil 	    [IEEE80211_MODE_11B]	= IEEE80211_CHAN_B,
   2034  1.60.18.1      phil 	    [IEEE80211_MODE_11G]	= IEEE80211_CHAN_G,
   2035  1.60.18.1      phil 	    [IEEE80211_MODE_FH]		= IEEE80211_CHAN_FHSS,
   2036  1.60.18.1      phil 	    [IEEE80211_MODE_TURBO_A]	= IEEE80211_CHAN_108A,
   2037  1.60.18.1      phil 	    [IEEE80211_MODE_TURBO_G]	= IEEE80211_CHAN_108G,
   2038  1.60.18.1      phil 	    [IEEE80211_MODE_STURBO_A]	= IEEE80211_CHAN_STURBO,
   2039  1.60.18.1      phil 	    [IEEE80211_MODE_HALF]	= IEEE80211_CHAN_HALF,
   2040  1.60.18.1      phil 	    [IEEE80211_MODE_QUARTER]	= IEEE80211_CHAN_QUARTER,
   2041  1.60.18.1      phil 	    /* NB: handled specially below */
   2042  1.60.18.1      phil 	    [IEEE80211_MODE_11NA]	= IEEE80211_CHAN_A,
   2043  1.60.18.1      phil 	    [IEEE80211_MODE_11NG]	= IEEE80211_CHAN_G,
   2044  1.60.18.1      phil 	    [IEEE80211_MODE_VHT_5GHZ]	= IEEE80211_CHAN_A,
   2045  1.60.18.1      phil 	    [IEEE80211_MODE_VHT_2GHZ]	= IEEE80211_CHAN_G,
   2046  1.60.18.1      phil 	};
   2047  1.60.18.1      phil 	u_int modeflags;
   2048  1.60.18.1      phil 	int i;
   2049  1.60.18.1      phil 
   2050  1.60.18.1      phil 	modeflags = chanflags[mode];
   2051  1.60.18.1      phil 	for (i = 0; i < ic->ic_nchans; i++) {
   2052  1.60.18.1      phil 		struct ieee80211_channel *c = &ic->ic_channels[i];
   2053  1.60.18.1      phil 
   2054  1.60.18.1      phil 		if (c->ic_ieee != ieee)
   2055  1.60.18.1      phil 			continue;
   2056  1.60.18.1      phil 		if (mode == IEEE80211_MODE_AUTO) {
   2057  1.60.18.1      phil 			/* ignore turbo channels for autoselect */
   2058  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_TURBO(c))
   2059  1.60.18.1      phil 				continue;
   2060  1.60.18.1      phil 			/*
   2061  1.60.18.1      phil 			 * XXX special-case 11b/g channels so we
   2062  1.60.18.1      phil 			 *     always select the g channel if both
   2063  1.60.18.1      phil 			 *     are present.
   2064  1.60.18.1      phil 			 * XXX prefer HT to non-HT?
   2065  1.60.18.1      phil 			 */
   2066  1.60.18.1      phil 			if (!IEEE80211_IS_CHAN_B(c) ||
   2067  1.60.18.1      phil 			    !find11gchannel(ic, i, c->ic_freq))
   2068  1.60.18.1      phil 				return c;
   2069  1.60.18.1      phil 		} else {
   2070  1.60.18.1      phil 			/* must check VHT specifically */
   2071  1.60.18.1      phil 			if ((mode == IEEE80211_MODE_VHT_5GHZ ||
   2072  1.60.18.1      phil 			    mode == IEEE80211_MODE_VHT_2GHZ) &&
   2073  1.60.18.1      phil 			    !IEEE80211_IS_CHAN_VHT(c))
   2074  1.60.18.1      phil 				continue;
   2075  1.60.18.1      phil 
   2076  1.60.18.1      phil 			/*
   2077  1.60.18.1      phil 			 * Must check HT specially - only match on HT,
   2078  1.60.18.1      phil 			 * not HT+VHT channels
   2079  1.60.18.1      phil 			 */
   2080  1.60.18.1      phil 			if ((mode == IEEE80211_MODE_11NA ||
   2081  1.60.18.1      phil 			    mode == IEEE80211_MODE_11NG) &&
   2082  1.60.18.1      phil 			    !IEEE80211_IS_CHAN_HT(c))
   2083  1.60.18.1      phil 				continue;
   2084  1.60.18.1      phil 
   2085  1.60.18.1      phil 			if ((mode == IEEE80211_MODE_11NA ||
   2086  1.60.18.1      phil 			    mode == IEEE80211_MODE_11NG) &&
   2087  1.60.18.1      phil 			    IEEE80211_IS_CHAN_VHT(c))
   2088  1.60.18.1      phil 				continue;
   2089  1.60.18.1      phil 
   2090  1.60.18.1      phil 			/* Check that the modeflags above match */
   2091  1.60.18.1      phil 			if ((c->ic_flags & modeflags) == modeflags)
   2092  1.60.18.1      phil 				return c;
   2093  1.60.18.1      phil 		}
   2094  1.60.18.1      phil 	}
   2095  1.60.18.1      phil 	return NULL;
   2096  1.60.18.1      phil }
   2097  1.60.18.1      phil 
   2098  1.60.18.1      phil /*
   2099  1.60.18.1      phil  * Check the specified against any desired mode (aka netband).
   2100  1.60.18.1      phil  * This is only used (presently) when operating in hostap mode
   2101  1.60.18.1      phil  * to enforce consistency.
   2102  1.60.18.1      phil  */
   2103       1.19    dyoung static int
   2104  1.60.18.1      phil check_mode_consistency(const struct ieee80211_channel *c, int mode)
   2105       1.19    dyoung {
   2106  1.60.18.1      phil 	KASSERT(c != IEEE80211_CHAN_ANYC, ("oops, no channel"));
   2107  1.60.18.1      phil 
   2108  1.60.18.1      phil 	switch (mode) {
   2109  1.60.18.1      phil 	case IEEE80211_MODE_11B:
   2110  1.60.18.1      phil 		return (IEEE80211_IS_CHAN_B(c));
   2111  1.60.18.1      phil 	case IEEE80211_MODE_11G:
   2112  1.60.18.1      phil 		return (IEEE80211_IS_CHAN_ANYG(c) && !IEEE80211_IS_CHAN_HT(c));
   2113  1.60.18.1      phil 	case IEEE80211_MODE_11A:
   2114  1.60.18.1      phil 		return (IEEE80211_IS_CHAN_A(c) && !IEEE80211_IS_CHAN_HT(c));
   2115  1.60.18.1      phil 	case IEEE80211_MODE_STURBO_A:
   2116  1.60.18.1      phil 		return (IEEE80211_IS_CHAN_STURBO(c));
   2117  1.60.18.1      phil 	case IEEE80211_MODE_11NA:
   2118  1.60.18.1      phil 		return (IEEE80211_IS_CHAN_HTA(c));
   2119  1.60.18.1      phil 	case IEEE80211_MODE_11NG:
   2120  1.60.18.1      phil 		return (IEEE80211_IS_CHAN_HTG(c));
   2121  1.60.18.1      phil 	}
   2122  1.60.18.1      phil 	return 1;
   2123  1.60.18.1      phil 
   2124  1.60.18.1      phil }
   2125  1.60.18.1      phil 
   2126  1.60.18.1      phil /*
   2127  1.60.18.1      phil  * Common code to set the current channel.  If the device
   2128  1.60.18.1      phil  * is up and running this may result in an immediate channel
   2129  1.60.18.1      phil  * change or a kick of the state machine.
   2130  1.60.18.1      phil  */
   2131  1.60.18.1      phil static int
   2132  1.60.18.1      phil setcurchan(struct ieee80211vap *vap, struct ieee80211_channel *c)
   2133  1.60.18.1      phil {
   2134  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2135       1.23    dyoung 	int error;
   2136  1.60.18.1      phil 
   2137  1.60.18.1      phil 	if (c != IEEE80211_CHAN_ANYC) {
   2138  1.60.18.1      phil 		if (IEEE80211_IS_CHAN_RADAR(c))
   2139  1.60.18.1      phil 			return EBUSY;	/* XXX better code? */
   2140  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
   2141  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_NOHOSTAP(c))
   2142  1.60.18.1      phil 				return EINVAL;
   2143  1.60.18.1      phil 			if (!check_mode_consistency(c, vap->iv_des_mode))
   2144  1.60.18.1      phil 				return EINVAL;
   2145  1.60.18.1      phil 		} else if (vap->iv_opmode == IEEE80211_M_IBSS) {
   2146  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_NOADHOC(c))
   2147  1.60.18.1      phil 				return EINVAL;
   2148  1.60.18.1      phil 		}
   2149  1.60.18.1      phil 		if ((vap->iv_state == IEEE80211_S_RUN || vap->iv_state == IEEE80211_S_SLEEP) &&
   2150  1.60.18.1      phil 		    vap->iv_bss->ni_chan == c)
   2151  1.60.18.1      phil 			return 0;	/* NB: nothing to do */
   2152  1.60.18.1      phil 	}
   2153  1.60.18.1      phil 	vap->iv_des_chan = c;
   2154       1.19    dyoung 
   2155       1.19    dyoung 	error = 0;
   2156  1.60.18.1      phil 	if (vap->iv_opmode == IEEE80211_M_MONITOR &&
   2157  1.60.18.1      phil 	    vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
   2158  1.60.18.1      phil 		/*
   2159  1.60.18.1      phil 		 * Monitor mode can switch directly.
   2160  1.60.18.1      phil 		 */
   2161  1.60.18.1      phil 		if (IFNET_IS_UP_RUNNING(vap->iv_ifp)) {
   2162  1.60.18.1      phil 			/* XXX need state machine for other vap's to follow */
   2163  1.60.18.1      phil 			ieee80211_setcurchan(ic, vap->iv_des_chan);
   2164  1.60.18.1      phil 			vap->iv_bss->ni_chan = ic->ic_curchan;
   2165  1.60.18.1      phil 		} else {
   2166  1.60.18.1      phil 			ic->ic_curchan = vap->iv_des_chan;
   2167  1.60.18.1      phil 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
   2168  1.60.18.1      phil 		}
   2169  1.60.18.1      phil 	} else {
   2170  1.60.18.1      phil 		/*
   2171  1.60.18.1      phil 		 * Need to go through the state machine in case we
   2172  1.60.18.1      phil 		 * need to reassociate or the like.  The state machine
   2173  1.60.18.1      phil 		 * will pickup the desired channel and avoid scanning.
   2174  1.60.18.1      phil 		 */
   2175  1.60.18.1      phil 		if (IS_UP_AUTO(vap))
   2176  1.60.18.1      phil 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
   2177  1.60.18.1      phil 		else if (vap->iv_des_chan != IEEE80211_CHAN_ANYC) {
   2178  1.60.18.1      phil 			/*
   2179  1.60.18.1      phil 			 * When not up+running and a real channel has
   2180  1.60.18.1      phil 			 * been specified fix the current channel so
   2181  1.60.18.1      phil 			 * there is immediate feedback; e.g. via ifconfig.
   2182  1.60.18.1      phil 			 */
   2183  1.60.18.1      phil 			ic->ic_curchan = vap->iv_des_chan;
   2184  1.60.18.1      phil 			ic->ic_rt = ieee80211_get_ratetable(ic->ic_curchan);
   2185  1.60.18.1      phil 		}
   2186  1.60.18.1      phil 	}
   2187  1.60.18.1      phil 	return error;
   2188  1.60.18.1      phil }
   2189  1.60.18.1      phil 
   2190  1.60.18.1      phil /*
   2191  1.60.18.1      phil  * Old api for setting the current channel; this is
   2192  1.60.18.1      phil  * deprecated because channel numbers are ambiguous.
   2193  1.60.18.1      phil  */
   2194  1.60.18.1      phil static int
   2195  1.60.18.1      phil ieee80211_ioctl_setchannel(struct ieee80211vap *vap,
   2196  1.60.18.1      phil 	const struct ieee80211req *ireq)
   2197  1.60.18.1      phil {
   2198  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2199  1.60.18.1      phil 	struct ieee80211_channel *c;
   2200  1.60.18.1      phil 
   2201  1.60.18.1      phil 	/* XXX 0xffff overflows 16-bit signed */
   2202  1.60.18.1      phil 	if (ireq->i_val == 0 ||
   2203  1.60.18.1      phil 	    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY) {
   2204  1.60.18.1      phil 		c = IEEE80211_CHAN_ANYC;
   2205  1.60.18.1      phil 	} else {
   2206  1.60.18.1      phil 		struct ieee80211_channel *c2;
   2207  1.60.18.1      phil 
   2208  1.60.18.1      phil 		c = findchannel(ic, ireq->i_val, vap->iv_des_mode);
   2209  1.60.18.1      phil 		if (c == NULL) {
   2210  1.60.18.1      phil 			c = findchannel(ic, ireq->i_val,
   2211  1.60.18.1      phil 				IEEE80211_MODE_AUTO);
   2212  1.60.18.1      phil 			if (c == NULL)
   2213  1.60.18.1      phil 				return EINVAL;
   2214  1.60.18.1      phil 		}
   2215  1.60.18.1      phil 
   2216  1.60.18.1      phil 		/*
   2217  1.60.18.1      phil 		 * Fine tune channel selection based on desired mode:
   2218  1.60.18.1      phil 		 *   if 11b is requested, find the 11b version of any
   2219  1.60.18.1      phil 		 *      11g channel returned,
   2220  1.60.18.1      phil 		 *   if static turbo, find the turbo version of any
   2221  1.60.18.1      phil 		 *	11a channel return,
   2222  1.60.18.1      phil 		 *   if 11na is requested, find the ht version of any
   2223  1.60.18.1      phil 		 *      11a channel returned,
   2224  1.60.18.1      phil 		 *   if 11ng is requested, find the ht version of any
   2225  1.60.18.1      phil 		 *      11g channel returned,
   2226  1.60.18.1      phil 		 *   if 11ac is requested, find the 11ac version
   2227  1.60.18.1      phil 		 *      of any 11a/11na channel returned,
   2228  1.60.18.1      phil 		 *   (TBD) 11acg (2GHz VHT)
   2229  1.60.18.1      phil 		 *   otherwise we should be ok with what we've got.
   2230  1.60.18.1      phil 		 */
   2231  1.60.18.1      phil 		switch (vap->iv_des_mode) {
   2232  1.60.18.1      phil 		case IEEE80211_MODE_11B:
   2233  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_ANYG(c)) {
   2234  1.60.18.1      phil 				c2 = findchannel(ic, ireq->i_val,
   2235  1.60.18.1      phil 					IEEE80211_MODE_11B);
   2236  1.60.18.1      phil 				/* NB: should not happen, =>'s 11g w/o 11b */
   2237  1.60.18.1      phil 				if (c2 != NULL)
   2238  1.60.18.1      phil 					c = c2;
   2239  1.60.18.1      phil 			}
   2240        1.1    dyoung 			break;
   2241  1.60.18.1      phil 		case IEEE80211_MODE_TURBO_A:
   2242  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_A(c)) {
   2243  1.60.18.1      phil 				c2 = findchannel(ic, ireq->i_val,
   2244  1.60.18.1      phil 					IEEE80211_MODE_TURBO_A);
   2245  1.60.18.1      phil 				if (c2 != NULL)
   2246  1.60.18.1      phil 					c = c2;
   2247  1.60.18.1      phil 			}
   2248        1.1    dyoung 			break;
   2249  1.60.18.1      phil 		case IEEE80211_MODE_11NA:
   2250  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_A(c)) {
   2251  1.60.18.1      phil 				c2 = findchannel(ic, ireq->i_val,
   2252  1.60.18.1      phil 					IEEE80211_MODE_11NA);
   2253  1.60.18.1      phil 				if (c2 != NULL)
   2254  1.60.18.1      phil 					c = c2;
   2255  1.60.18.1      phil 			}
   2256        1.1    dyoung 			break;
   2257  1.60.18.1      phil 		case IEEE80211_MODE_11NG:
   2258  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_ANYG(c)) {
   2259  1.60.18.1      phil 				c2 = findchannel(ic, ireq->i_val,
   2260  1.60.18.1      phil 					IEEE80211_MODE_11NG);
   2261  1.60.18.1      phil 				if (c2 != NULL)
   2262  1.60.18.1      phil 					c = c2;
   2263  1.60.18.1      phil 			}
   2264        1.1    dyoung 			break;
   2265  1.60.18.1      phil 		case IEEE80211_MODE_VHT_2GHZ:
   2266  1.60.18.1      phil 			printf("%s: TBD\n", __func__);
   2267  1.60.18.1      phil 			break;
   2268  1.60.18.1      phil 		case IEEE80211_MODE_VHT_5GHZ:
   2269  1.60.18.1      phil 			if (IEEE80211_IS_CHAN_A(c)) {
   2270  1.60.18.1      phil 				c2 = findchannel(ic, ireq->i_val,
   2271  1.60.18.1      phil 					IEEE80211_MODE_VHT_5GHZ);
   2272  1.60.18.1      phil 				if (c2 != NULL)
   2273  1.60.18.1      phil 					c = c2;
   2274  1.60.18.1      phil 			}
   2275  1.60.18.1      phil 			break;
   2276  1.60.18.1      phil 		default:		/* NB: no static turboG */
   2277        1.1    dyoung 			break;
   2278       1.19    dyoung 		}
   2279  1.60.18.1      phil 	}
   2280  1.60.18.1      phil 	return setcurchan(vap, c);
   2281  1.60.18.1      phil }
   2282  1.60.18.1      phil 
   2283  1.60.18.1      phil /*
   2284  1.60.18.1      phil  * New/current api for setting the current channel; a complete
   2285  1.60.18.1      phil  * channel description is provide so there is no ambiguity in
   2286  1.60.18.1      phil  * identifying the channel.
   2287  1.60.18.1      phil  */
   2288  1.60.18.1      phil static int
   2289  1.60.18.1      phil ieee80211_ioctl_setcurchan(struct ieee80211vap *vap,
   2290  1.60.18.1      phil 	const struct ieee80211req *ireq)
   2291  1.60.18.1      phil {
   2292  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2293  1.60.18.1      phil 	struct ieee80211_channel chan, *c;
   2294  1.60.18.1      phil 	int error;
   2295  1.60.18.1      phil 
   2296  1.60.18.1      phil 	if (ireq->i_len != sizeof(chan))
   2297  1.60.18.1      phil 		return EINVAL;
   2298  1.60.18.1      phil 	error = copyin(ireq->i_data, &chan, sizeof(chan));
   2299  1.60.18.1      phil 	if (error != 0)
   2300  1.60.18.1      phil 		return error;
   2301  1.60.18.1      phil 
   2302  1.60.18.1      phil 	/* XXX 0xffff overflows 16-bit signed */
   2303  1.60.18.1      phil 	if (chan.ic_freq == 0 || chan.ic_freq == IEEE80211_CHAN_ANY) {
   2304  1.60.18.1      phil 		c = IEEE80211_CHAN_ANYC;
   2305  1.60.18.1      phil 	} else {
   2306  1.60.18.1      phil 		c = ieee80211_find_channel(ic, chan.ic_freq, chan.ic_flags);
   2307  1.60.18.1      phil 		if (c == NULL)
   2308       1.19    dyoung 			return EINVAL;
   2309  1.60.18.1      phil 	}
   2310  1.60.18.1      phil 	return setcurchan(vap, c);
   2311  1.60.18.1      phil }
   2312  1.60.18.1      phil 
   2313  1.60.18.1      phil static int
   2314  1.60.18.1      phil ieee80211_ioctl_setregdomain(struct ieee80211vap *vap,
   2315  1.60.18.1      phil 	const struct ieee80211req *ireq)
   2316  1.60.18.1      phil {
   2317  1.60.18.1      phil 	struct ieee80211_regdomain_req *reg;
   2318  1.60.18.1      phil 	int nchans, error;
   2319  1.60.18.1      phil 
   2320  1.60.18.1      phil 	nchans = 1 + ((ireq->i_len - sizeof(struct ieee80211_regdomain_req)) /
   2321  1.60.18.1      phil 	    sizeof(struct ieee80211_channel));
   2322  1.60.18.1      phil 	if (!(1 <= nchans && nchans <= IEEE80211_CHAN_MAX)) {
   2323  1.60.18.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
   2324  1.60.18.1      phil 		    "%s: bad # chans, i_len %d nchans %d\n", __func__,
   2325  1.60.18.1      phil 		    ireq->i_len, nchans);
   2326  1.60.18.1      phil 		return EINVAL;
   2327  1.60.18.1      phil 	}
   2328  1.60.18.1      phil 	reg = (struct ieee80211_regdomain_req *)
   2329  1.60.18.1      phil 	    IEEE80211_MALLOC(IEEE80211_REGDOMAIN_SIZE(nchans), M_TEMP,
   2330  1.60.18.1      phil 	      IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   2331  1.60.18.1      phil 	if (reg == NULL) {
   2332  1.60.18.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
   2333  1.60.18.1      phil 		    "%s: no memory, nchans %d\n", __func__, nchans);
   2334  1.60.18.1      phil 		return ENOMEM;
   2335  1.60.18.1      phil 	}
   2336  1.60.18.1      phil 	error = copyin(ireq->i_data, reg, IEEE80211_REGDOMAIN_SIZE(nchans));
   2337  1.60.18.1      phil 	if (error == 0) {
   2338  1.60.18.1      phil 		/* NB: validate inline channel count against storage size */
   2339  1.60.18.1      phil 		if (reg->chaninfo.ic_nchans != nchans) {
   2340  1.60.18.1      phil 			IEEE80211_DPRINTF(vap, IEEE80211_MSG_IOCTL,
   2341  1.60.18.1      phil 			    "%s: chan cnt mismatch, %d != %d\n", __func__,
   2342  1.60.18.1      phil 				reg->chaninfo.ic_nchans, nchans);
   2343  1.60.18.1      phil 			error = EINVAL;
   2344  1.60.18.1      phil 		} else
   2345  1.60.18.1      phil 			error = ieee80211_setregdomain(vap, reg);
   2346  1.60.18.1      phil 	}
   2347  1.60.18.1      phil 	IEEE80211_FREE(reg, M_TEMP);
   2348  1.60.18.1      phil 
   2349  1.60.18.1      phil 	return (error == 0 ? ENETRESET : error);
   2350  1.60.18.1      phil }
   2351  1.60.18.1      phil 
   2352  1.60.18.1      phil static int
   2353  1.60.18.1      phil ieee80211_ioctl_setroam(struct ieee80211vap *vap,
   2354  1.60.18.1      phil 	const struct ieee80211req *ireq)
   2355  1.60.18.1      phil {
   2356  1.60.18.1      phil 	if (ireq->i_len != sizeof(vap->iv_roamparms))
   2357  1.60.18.1      phil 		return EINVAL;
   2358  1.60.18.1      phil 	/* XXX validate params */
   2359  1.60.18.1      phil 	/* XXX? ENETRESET to push to device? */
   2360  1.60.18.1      phil 	return copyin(ireq->i_data, vap->iv_roamparms,
   2361  1.60.18.1      phil 	    sizeof(vap->iv_roamparms));
   2362  1.60.18.1      phil }
   2363  1.60.18.1      phil 
   2364  1.60.18.1      phil static int
   2365  1.60.18.1      phil checkrate(const struct ieee80211_rateset *rs, int rate)
   2366  1.60.18.1      phil {
   2367  1.60.18.1      phil 	int i;
   2368  1.60.18.1      phil 
   2369  1.60.18.1      phil 	if (rate == IEEE80211_FIXED_RATE_NONE)
   2370  1.60.18.1      phil 		return 1;
   2371  1.60.18.1      phil 	for (i = 0; i < rs->rs_nrates; i++)
   2372  1.60.18.1      phil 		if ((rs->rs_rates[i] & IEEE80211_RATE_VAL) == rate)
   2373  1.60.18.1      phil 			return 1;
   2374  1.60.18.1      phil 	return 0;
   2375  1.60.18.1      phil }
   2376  1.60.18.1      phil 
   2377  1.60.18.1      phil static int
   2378  1.60.18.1      phil checkmcs(const struct ieee80211_htrateset *rs, int mcs)
   2379  1.60.18.1      phil {
   2380  1.60.18.1      phil 	int rate_val = IEEE80211_RV(mcs);
   2381  1.60.18.1      phil 	int i;
   2382  1.60.18.1      phil 
   2383  1.60.18.1      phil 	if (mcs == IEEE80211_FIXED_RATE_NONE)
   2384  1.60.18.1      phil 		return 1;
   2385  1.60.18.1      phil 	if ((mcs & IEEE80211_RATE_MCS) == 0)	/* MCS always have 0x80 set */
   2386  1.60.18.1      phil 		return 0;
   2387  1.60.18.1      phil 	for (i = 0; i < rs->rs_nrates; i++)
   2388  1.60.18.1      phil 		if (IEEE80211_RV(rs->rs_rates[i]) == rate_val)
   2389  1.60.18.1      phil 			return 1;
   2390  1.60.18.1      phil 	return 0;
   2391  1.60.18.1      phil }
   2392  1.60.18.1      phil 
   2393  1.60.18.1      phil static int
   2394  1.60.18.1      phil ieee80211_ioctl_settxparams(struct ieee80211vap *vap,
   2395  1.60.18.1      phil 	const struct ieee80211req *ireq)
   2396  1.60.18.1      phil {
   2397  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2398  1.60.18.1      phil 	struct ieee80211_txparams_req parms;	/* XXX stack use? */
   2399  1.60.18.1      phil 	struct ieee80211_txparam *src, *dst;
   2400  1.60.18.1      phil 	const struct ieee80211_htrateset *rs_ht;
   2401  1.60.18.1      phil 	const struct ieee80211_rateset *rs;
   2402  1.60.18.1      phil 	int error, mode, changed, is11n, nmodes;
   2403  1.60.18.1      phil 
   2404  1.60.18.1      phil 	/* NB: accept short requests for backwards compat */
   2405  1.60.18.1      phil 	if (ireq->i_len > sizeof(parms))
   2406  1.60.18.1      phil 		return EINVAL;
   2407  1.60.18.1      phil 	error = copyin(ireq->i_data, &parms, ireq->i_len);
   2408  1.60.18.1      phil 	if (error != 0)
   2409  1.60.18.1      phil 		return error;
   2410  1.60.18.1      phil 	nmodes = ireq->i_len / sizeof(struct ieee80211_txparam);
   2411  1.60.18.1      phil 	changed = 0;
   2412  1.60.18.1      phil 	/* validate parameters and check if anything changed */
   2413  1.60.18.1      phil 	for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
   2414  1.60.18.1      phil 		if (isclr(ic->ic_modecaps, mode))
   2415  1.60.18.1      phil 			continue;
   2416  1.60.18.1      phil 		src = &parms.params[mode];
   2417  1.60.18.1      phil 		dst = &vap->iv_txparms[mode];
   2418  1.60.18.1      phil 		rs = &ic->ic_sup_rates[mode];	/* NB: 11n maps to legacy */
   2419  1.60.18.1      phil 		rs_ht = &ic->ic_sup_htrates;
   2420  1.60.18.1      phil 		is11n = (mode == IEEE80211_MODE_11NA ||
   2421  1.60.18.1      phil 			 mode == IEEE80211_MODE_11NG);
   2422  1.60.18.1      phil 		if (src->ucastrate != dst->ucastrate) {
   2423  1.60.18.1      phil 			if (!checkrate(rs, src->ucastrate) &&
   2424  1.60.18.1      phil 			    (!is11n || !checkmcs(rs_ht, src->ucastrate)))
   2425  1.60.18.1      phil 				return EINVAL;
   2426  1.60.18.1      phil 			changed++;
   2427  1.60.18.1      phil 		}
   2428  1.60.18.1      phil 		if (src->mcastrate != dst->mcastrate) {
   2429  1.60.18.1      phil 			if (!checkrate(rs, src->mcastrate) &&
   2430  1.60.18.1      phil 			    (!is11n || !checkmcs(rs_ht, src->mcastrate)))
   2431  1.60.18.1      phil 				return EINVAL;
   2432  1.60.18.1      phil 			changed++;
   2433  1.60.18.1      phil 		}
   2434  1.60.18.1      phil 		if (src->mgmtrate != dst->mgmtrate) {
   2435  1.60.18.1      phil 			if (!checkrate(rs, src->mgmtrate) &&
   2436  1.60.18.1      phil 			    (!is11n || !checkmcs(rs_ht, src->mgmtrate)))
   2437  1.60.18.1      phil 				return EINVAL;
   2438  1.60.18.1      phil 			changed++;
   2439  1.60.18.1      phil 		}
   2440  1.60.18.1      phil 		if (src->maxretry != dst->maxretry)	/* NB: no bounds */
   2441  1.60.18.1      phil 			changed++;
   2442  1.60.18.1      phil 	}
   2443  1.60.18.1      phil 	if (changed) {
   2444  1.60.18.1      phil 		/*
   2445  1.60.18.1      phil 		 * Copy new parameters in place and notify the
   2446  1.60.18.1      phil 		 * driver so it can push state to the device.
   2447  1.60.18.1      phil 		 */
   2448  1.60.18.1      phil 		for (mode = IEEE80211_MODE_11A; mode < nmodes; mode++) {
   2449  1.60.18.1      phil 			if (isset(ic->ic_modecaps, mode))
   2450  1.60.18.1      phil 				vap->iv_txparms[mode] = parms.params[mode];
   2451  1.60.18.1      phil 		}
   2452  1.60.18.1      phil 		/* XXX could be more intelligent,
   2453  1.60.18.1      phil 		   e.g. don't reset if setting not being used */
   2454  1.60.18.1      phil 		return ENETRESET;
   2455  1.60.18.1      phil 	}
   2456  1.60.18.1      phil 	return 0;
   2457  1.60.18.1      phil }
   2458  1.60.18.1      phil 
   2459  1.60.18.1      phil /*
   2460  1.60.18.1      phil  * Application Information Element support.
   2461  1.60.18.1      phil  */
   2462  1.60.18.1      phil static int
   2463  1.60.18.1      phil setappie(struct ieee80211_appie **aie, const struct ieee80211req *ireq)
   2464  1.60.18.1      phil {
   2465  1.60.18.1      phil 	struct ieee80211_appie *app = *aie;
   2466  1.60.18.1      phil 	struct ieee80211_appie *napp;
   2467  1.60.18.1      phil 	int error;
   2468  1.60.18.1      phil 
   2469  1.60.18.1      phil 	if (ireq->i_len == 0) {		/* delete any existing ie */
   2470  1.60.18.1      phil 		if (app != NULL) {
   2471  1.60.18.1      phil 			*aie = NULL;	/* XXX racey */
   2472  1.60.18.1      phil 			IEEE80211_FREE(app, M_80211_NODE_IE);
   2473  1.60.18.1      phil 		}
   2474  1.60.18.1      phil 		return 0;
   2475  1.60.18.1      phil 	}
   2476  1.60.18.1      phil 	if (!(2 <= ireq->i_len && ireq->i_len <= IEEE80211_MAX_APPIE))
   2477  1.60.18.1      phil 		return EINVAL;
   2478  1.60.18.1      phil 	/*
   2479  1.60.18.1      phil 	 * Allocate a new appie structure and copy in the user data.
   2480  1.60.18.1      phil 	 * When done swap in the new structure.  Note that we do not
   2481  1.60.18.1      phil 	 * guard against users holding a ref to the old structure;
   2482  1.60.18.1      phil 	 * this must be handled outside this code.
   2483  1.60.18.1      phil 	 *
   2484  1.60.18.1      phil 	 * XXX bad bad bad
   2485  1.60.18.1      phil 	 */
   2486  1.60.18.1      phil 	napp = (struct ieee80211_appie *) IEEE80211_MALLOC(
   2487  1.60.18.1      phil 	    sizeof(struct ieee80211_appie) + ireq->i_len, M_80211_NODE_IE,
   2488  1.60.18.1      phil 	    IEEE80211_M_NOWAIT);
   2489  1.60.18.1      phil 	if (napp == NULL)
   2490  1.60.18.1      phil 		return ENOMEM;
   2491  1.60.18.1      phil 	/* XXX holding ic lock */
   2492  1.60.18.1      phil 	error = copyin(ireq->i_data, napp->ie_data, ireq->i_len);
   2493  1.60.18.1      phil 	if (error) {
   2494  1.60.18.1      phil 		IEEE80211_FREE(napp, M_80211_NODE_IE);
   2495  1.60.18.1      phil 		return error;
   2496  1.60.18.1      phil 	}
   2497  1.60.18.1      phil 	napp->ie_len = ireq->i_len;
   2498  1.60.18.1      phil 	*aie = napp;
   2499  1.60.18.1      phil 	if (app != NULL)
   2500  1.60.18.1      phil 		IEEE80211_FREE(app, M_80211_NODE_IE);
   2501  1.60.18.1      phil 	return 0;
   2502  1.60.18.1      phil }
   2503  1.60.18.1      phil 
   2504  1.60.18.1      phil static void
   2505  1.60.18.1      phil setwparsnie(struct ieee80211vap *vap, uint8_t *ie, int space)
   2506  1.60.18.1      phil {
   2507  1.60.18.1      phil 	/* validate data is present as best we can */
   2508  1.60.18.1      phil 	if (space == 0 || 2+ie[1] > space)
   2509  1.60.18.1      phil 		return;
   2510  1.60.18.1      phil 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
   2511  1.60.18.1      phil 		vap->iv_wpa_ie = ie;
   2512  1.60.18.1      phil 	else if (ie[0] == IEEE80211_ELEMID_RSN)
   2513  1.60.18.1      phil 		vap->iv_rsn_ie = ie;
   2514  1.60.18.1      phil }
   2515  1.60.18.1      phil 
   2516  1.60.18.1      phil static int
   2517  1.60.18.1      phil ieee80211_ioctl_setappie_locked(struct ieee80211vap *vap,
   2518  1.60.18.1      phil 	const struct ieee80211req *ireq, int fc0)
   2519  1.60.18.1      phil {
   2520  1.60.18.1      phil 	int error;
   2521  1.60.18.1      phil 
   2522  1.60.18.1      phil 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
   2523  1.60.18.1      phil 
   2524  1.60.18.1      phil 	switch (fc0 & IEEE80211_FC0_SUBTYPE_MASK) {
   2525  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_BEACON:
   2526  1.60.18.1      phil 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
   2527  1.60.18.1      phil 		    vap->iv_opmode != IEEE80211_M_IBSS) {
   2528  1.60.18.1      phil 			error = EINVAL;
   2529  1.60.18.1      phil 			break;
   2530  1.60.18.1      phil 		}
   2531  1.60.18.1      phil 		error = setappie(&vap->iv_appie_beacon, ireq);
   2532  1.60.18.1      phil 		if (error == 0)
   2533  1.60.18.1      phil 			ieee80211_beacon_notify(vap, IEEE80211_BEACON_APPIE);
   2534  1.60.18.1      phil 		break;
   2535  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
   2536  1.60.18.1      phil 		error = setappie(&vap->iv_appie_proberesp, ireq);
   2537  1.60.18.1      phil 		break;
   2538  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_ASSOC_RESP:
   2539  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
   2540  1.60.18.1      phil 			error = setappie(&vap->iv_appie_assocresp, ireq);
   2541  1.60.18.1      phil 		else
   2542  1.60.18.1      phil 			error = EINVAL;
   2543  1.60.18.1      phil 		break;
   2544  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_PROBE_REQ:
   2545  1.60.18.1      phil 		error = setappie(&vap->iv_appie_probereq, ireq);
   2546  1.60.18.1      phil 		break;
   2547  1.60.18.1      phil 	case IEEE80211_FC0_SUBTYPE_ASSOC_REQ:
   2548  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA)
   2549  1.60.18.1      phil 			error = setappie(&vap->iv_appie_assocreq, ireq);
   2550  1.60.18.1      phil 		else
   2551  1.60.18.1      phil 			error = EINVAL;
   2552  1.60.18.1      phil 		break;
   2553  1.60.18.1      phil 	case (IEEE80211_APPIE_WPA & IEEE80211_FC0_SUBTYPE_MASK):
   2554  1.60.18.1      phil 		error = setappie(&vap->iv_appie_wpa, ireq);
   2555  1.60.18.1      phil 		if (error == 0) {
   2556  1.60.18.1      phil 			/*
   2557  1.60.18.1      phil 			 * Must split single blob of data into separate
   2558  1.60.18.1      phil 			 * WPA and RSN ie's because they go in different
   2559  1.60.18.1      phil 			 * locations in the mgt frames.
   2560  1.60.18.1      phil 			 * XXX use IEEE80211_IOC_WPA2 so user code does split
   2561  1.60.18.1      phil 			 */
   2562  1.60.18.1      phil 			vap->iv_wpa_ie = NULL;
   2563  1.60.18.1      phil 			vap->iv_rsn_ie = NULL;
   2564  1.60.18.1      phil 			if (vap->iv_appie_wpa != NULL) {
   2565  1.60.18.1      phil 				struct ieee80211_appie *appie =
   2566  1.60.18.1      phil 				    vap->iv_appie_wpa;
   2567  1.60.18.1      phil 				uint8_t *data = appie->ie_data;
   2568  1.60.18.1      phil 
   2569  1.60.18.1      phil 				/* XXX ie length validate is painful, cheat */
   2570  1.60.18.1      phil 				setwparsnie(vap, data, appie->ie_len);
   2571  1.60.18.1      phil 				setwparsnie(vap, data + 2 + data[1],
   2572  1.60.18.1      phil 				    appie->ie_len - (2 + data[1]));
   2573  1.60.18.1      phil 			}
   2574  1.60.18.1      phil 			if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
   2575  1.60.18.1      phil 			    vap->iv_opmode == IEEE80211_M_IBSS) {
   2576  1.60.18.1      phil 				/*
   2577  1.60.18.1      phil 				 * Must rebuild beacon frame as the update
   2578  1.60.18.1      phil 				 * mechanism doesn't handle WPA/RSN ie's.
   2579  1.60.18.1      phil 				 * Could extend it but it doesn't normally
   2580  1.60.18.1      phil 				 * change; this is just to deal with hostapd
   2581  1.60.18.1      phil 				 * plumbing the ie after the interface is up.
   2582  1.60.18.1      phil 				 */
   2583  1.60.18.1      phil 				error = ENETRESET;
   2584  1.60.18.1      phil 			}
   2585  1.60.18.1      phil 		}
   2586  1.60.18.1      phil 		break;
   2587  1.60.18.1      phil 	default:
   2588  1.60.18.1      phil 		error = EINVAL;
   2589  1.60.18.1      phil 		break;
   2590  1.60.18.1      phil 	}
   2591  1.60.18.1      phil 	return error;
   2592  1.60.18.1      phil }
   2593  1.60.18.1      phil 
   2594  1.60.18.1      phil static int
   2595  1.60.18.1      phil ieee80211_ioctl_setappie(struct ieee80211vap *vap,
   2596  1.60.18.1      phil 	const struct ieee80211req *ireq)
   2597  1.60.18.1      phil {
   2598  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2599  1.60.18.1      phil 	int error;
   2600  1.60.18.1      phil 	uint8_t fc0;
   2601  1.60.18.1      phil 
   2602  1.60.18.1      phil 	fc0 = ireq->i_val & 0xff;
   2603  1.60.18.1      phil 	if ((fc0 & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT)
   2604  1.60.18.1      phil 		return EINVAL;
   2605  1.60.18.1      phil 	/* NB: could check iv_opmode and reject but hardly worth the effort */
   2606  1.60.18.1      phil 	IEEE80211_LOCK(ic);
   2607  1.60.18.1      phil 	error = ieee80211_ioctl_setappie_locked(vap, ireq, fc0);
   2608  1.60.18.1      phil 	IEEE80211_UNLOCK(ic);
   2609  1.60.18.1      phil 	return error;
   2610  1.60.18.1      phil }
   2611  1.60.18.1      phil 
   2612  1.60.18.1      phil static int
   2613  1.60.18.1      phil ieee80211_ioctl_chanswitch(struct ieee80211vap *vap, struct ieee80211req *ireq)
   2614  1.60.18.1      phil {
   2615  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2616  1.60.18.1      phil 	struct ieee80211_chanswitch_req csr;
   2617  1.60.18.1      phil 	struct ieee80211_channel *c;
   2618  1.60.18.1      phil 	int error;
   2619  1.60.18.1      phil 
   2620  1.60.18.1      phil 	if (ireq->i_len != sizeof(csr))
   2621  1.60.18.1      phil 		return EINVAL;
   2622  1.60.18.1      phil 	error = copyin(ireq->i_data, &csr, sizeof(csr));
   2623  1.60.18.1      phil 	if (error != 0)
   2624  1.60.18.1      phil 		return error;
   2625  1.60.18.1      phil 	/* XXX adhoc mode not supported */
   2626  1.60.18.1      phil 	if (vap->iv_opmode != IEEE80211_M_HOSTAP ||
   2627  1.60.18.1      phil 	    (vap->iv_flags & IEEE80211_F_DOTH) == 0)
   2628  1.60.18.1      phil 		return EOPNOTSUPP;
   2629  1.60.18.1      phil 	c = ieee80211_find_channel(ic,
   2630  1.60.18.1      phil 	    csr.csa_chan.ic_freq, csr.csa_chan.ic_flags);
   2631  1.60.18.1      phil 	if (c == NULL)
   2632  1.60.18.1      phil 		return ENOENT;
   2633  1.60.18.1      phil 	IEEE80211_LOCK(ic);
   2634  1.60.18.1      phil 	if ((ic->ic_flags & IEEE80211_F_CSAPENDING) == 0)
   2635  1.60.18.1      phil 		ieee80211_csa_startswitch(ic, c, csr.csa_mode, csr.csa_count);
   2636  1.60.18.1      phil 	else if (csr.csa_count == 0)
   2637  1.60.18.1      phil 		ieee80211_csa_cancelswitch(ic);
   2638  1.60.18.1      phil 	else
   2639  1.60.18.1      phil 		error = EBUSY;
   2640  1.60.18.1      phil 	IEEE80211_UNLOCK(ic);
   2641  1.60.18.1      phil 	return error;
   2642  1.60.18.1      phil }
   2643  1.60.18.1      phil 
   2644  1.60.18.1      phil static int
   2645  1.60.18.1      phil ieee80211_scanreq(struct ieee80211vap *vap, struct ieee80211_scan_req *sr)
   2646  1.60.18.1      phil {
   2647  1.60.18.1      phil #define	IEEE80211_IOC_SCAN_FLAGS \
   2648  1.60.18.1      phil 	(IEEE80211_IOC_SCAN_NOPICK | IEEE80211_IOC_SCAN_ACTIVE | \
   2649  1.60.18.1      phil 	 IEEE80211_IOC_SCAN_PICK1ST | IEEE80211_IOC_SCAN_BGSCAN | \
   2650  1.60.18.1      phil 	 IEEE80211_IOC_SCAN_ONCE | IEEE80211_IOC_SCAN_NOBCAST | \
   2651  1.60.18.1      phil 	 IEEE80211_IOC_SCAN_NOJOIN | IEEE80211_IOC_SCAN_FLUSH | \
   2652  1.60.18.1      phil 	 IEEE80211_IOC_SCAN_CHECK)
   2653  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2654  1.60.18.1      phil 	int error, i;
   2655  1.60.18.1      phil 
   2656  1.60.18.1      phil 	/* convert duration */
   2657  1.60.18.1      phil 	if (sr->sr_duration == IEEE80211_IOC_SCAN_FOREVER)
   2658  1.60.18.1      phil 		sr->sr_duration = IEEE80211_SCAN_FOREVER;
   2659  1.60.18.1      phil 	else {
   2660  1.60.18.1      phil 		if (sr->sr_duration < IEEE80211_IOC_SCAN_DURATION_MIN ||
   2661  1.60.18.1      phil 		    sr->sr_duration > IEEE80211_IOC_SCAN_DURATION_MAX)
   2662  1.60.18.1      phil 			return EINVAL;
   2663  1.60.18.1      phil 		sr->sr_duration = msecs_to_ticks(sr->sr_duration);
   2664  1.60.18.1      phil 		if (sr->sr_duration < 1)
   2665  1.60.18.1      phil 			sr->sr_duration = 1;
   2666  1.60.18.1      phil 	}
   2667  1.60.18.1      phil 	/* convert min/max channel dwell */
   2668  1.60.18.1      phil 	if (sr->sr_mindwell != 0) {
   2669  1.60.18.1      phil 		sr->sr_mindwell = msecs_to_ticks(sr->sr_mindwell);
   2670  1.60.18.1      phil 		if (sr->sr_mindwell < 1)
   2671  1.60.18.1      phil 			sr->sr_mindwell = 1;
   2672  1.60.18.1      phil 	}
   2673  1.60.18.1      phil 	if (sr->sr_maxdwell != 0) {
   2674  1.60.18.1      phil 		sr->sr_maxdwell = msecs_to_ticks(sr->sr_maxdwell);
   2675  1.60.18.1      phil 		if (sr->sr_maxdwell < 1)
   2676  1.60.18.1      phil 			sr->sr_maxdwell = 1;
   2677  1.60.18.1      phil 	}
   2678  1.60.18.1      phil 	/* NB: silently reduce ssid count to what is supported */
   2679  1.60.18.1      phil 	if (sr->sr_nssid > IEEE80211_SCAN_MAX_SSID)
   2680  1.60.18.1      phil 		sr->sr_nssid = IEEE80211_SCAN_MAX_SSID;
   2681  1.60.18.1      phil 	for (i = 0; i < sr->sr_nssid; i++)
   2682  1.60.18.1      phil 		if (sr->sr_ssid[i].len > IEEE80211_NWID_LEN)
   2683  1.60.18.1      phil 			return EINVAL;
   2684  1.60.18.1      phil 	/* cleanse flags just in case, could reject if invalid flags */
   2685  1.60.18.1      phil 	sr->sr_flags &= IEEE80211_IOC_SCAN_FLAGS;
   2686  1.60.18.1      phil 	/*
   2687  1.60.18.1      phil 	 * Add an implicit NOPICK if the vap is not marked UP.  This
   2688  1.60.18.1      phil 	 * allows applications to scan without joining a bss (or picking
   2689  1.60.18.1      phil 	 * a channel and setting up a bss) and without forcing manual
   2690  1.60.18.1      phil 	 * roaming mode--you just need to mark the parent device UP.
   2691  1.60.18.1      phil 	 */
   2692  1.60.18.1      phil 	if ((vap->iv_ifp->if_flags & IFF_UP) == 0)
   2693  1.60.18.1      phil 		sr->sr_flags |= IEEE80211_IOC_SCAN_NOPICK;
   2694  1.60.18.1      phil 
   2695  1.60.18.1      phil 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
   2696  1.60.18.1      phil 	    "%s: flags 0x%x%s duration 0x%x mindwell %u maxdwell %u nssid %d\n",
   2697  1.60.18.1      phil 	    __func__, sr->sr_flags,
   2698  1.60.18.1      phil 	    (vap->iv_ifp->if_flags & IFF_UP) == 0 ? " (!IFF_UP)" : "",
   2699  1.60.18.1      phil 	    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell, sr->sr_nssid);
   2700  1.60.18.1      phil 	/*
   2701  1.60.18.1      phil 	 * If we are in INIT state then the driver has never had a chance
   2702  1.60.18.1      phil 	 * to setup hardware state to do a scan; we must use the state
   2703  1.60.18.1      phil 	 * machine to get us up to the SCAN state but once we reach SCAN
   2704  1.60.18.1      phil 	 * state we then want to use the supplied params.  Stash the
   2705  1.60.18.1      phil 	 * parameters in the vap and mark IEEE80211_FEXT_SCANREQ; the
   2706  1.60.18.1      phil 	 * state machines will recognize this and use the stashed params
   2707  1.60.18.1      phil 	 * to issue the scan request.
   2708  1.60.18.1      phil 	 *
   2709  1.60.18.1      phil 	 * Otherwise just invoke the scan machinery directly.
   2710  1.60.18.1      phil 	 */
   2711  1.60.18.1      phil 	IEEE80211_LOCK(ic);
   2712  1.60.18.1      phil 	if (ic->ic_nrunning == 0) {
   2713  1.60.18.1      phil 		IEEE80211_UNLOCK(ic);
   2714  1.60.18.1      phil 		return ENXIO;
   2715  1.60.18.1      phil 	}
   2716  1.60.18.1      phil 
   2717  1.60.18.1      phil 	if (vap->iv_state == IEEE80211_S_INIT) {
   2718  1.60.18.1      phil 		/* NB: clobbers previous settings */
   2719  1.60.18.1      phil 		vap->iv_scanreq_flags = sr->sr_flags;
   2720  1.60.18.1      phil 		vap->iv_scanreq_duration = sr->sr_duration;
   2721  1.60.18.1      phil 		vap->iv_scanreq_nssid = sr->sr_nssid;
   2722  1.60.18.1      phil 		for (i = 0; i < sr->sr_nssid; i++) {
   2723  1.60.18.1      phil 			vap->iv_scanreq_ssid[i].len = sr->sr_ssid[i].len;
   2724  1.60.18.1      phil 			memcpy(vap->iv_scanreq_ssid[i].ssid,
   2725  1.60.18.1      phil 			    sr->sr_ssid[i].ssid, sr->sr_ssid[i].len);
   2726  1.60.18.1      phil 		}
   2727  1.60.18.1      phil 		vap->iv_flags_ext |= IEEE80211_FEXT_SCANREQ;
   2728  1.60.18.1      phil 		IEEE80211_UNLOCK(ic);
   2729  1.60.18.1      phil 		ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
   2730  1.60.18.1      phil 	} else {
   2731  1.60.18.1      phil 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
   2732  1.60.18.1      phil 		IEEE80211_UNLOCK(ic);
   2733  1.60.18.1      phil 		if (sr->sr_flags & IEEE80211_IOC_SCAN_CHECK) {
   2734  1.60.18.1      phil 			error = ieee80211_check_scan(vap, sr->sr_flags,
   2735  1.60.18.1      phil 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
   2736  1.60.18.1      phil 			    sr->sr_nssid,
   2737  1.60.18.1      phil 			    /* NB: cheat, we assume structures are compatible */
   2738  1.60.18.1      phil 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
   2739  1.60.18.1      phil 		} else {
   2740  1.60.18.1      phil 			error = ieee80211_start_scan(vap, sr->sr_flags,
   2741  1.60.18.1      phil 			    sr->sr_duration, sr->sr_mindwell, sr->sr_maxdwell,
   2742  1.60.18.1      phil 			    sr->sr_nssid,
   2743  1.60.18.1      phil 			    /* NB: cheat, we assume structures are compatible */
   2744  1.60.18.1      phil 			    (const struct ieee80211_scan_ssid *) &sr->sr_ssid[0]);
   2745  1.60.18.1      phil 		}
   2746  1.60.18.1      phil 		if (error == 0)
   2747  1.60.18.1      phil 			return EINPROGRESS;
   2748  1.60.18.1      phil 	}
   2749  1.60.18.1      phil 	return 0;
   2750  1.60.18.1      phil #undef IEEE80211_IOC_SCAN_FLAGS
   2751  1.60.18.1      phil }
   2752  1.60.18.1      phil 
   2753  1.60.18.1      phil static int
   2754  1.60.18.1      phil ieee80211_ioctl_scanreq(struct ieee80211vap *vap, struct ieee80211req *ireq)
   2755  1.60.18.1      phil {
   2756  1.60.18.1      phil 	struct ieee80211_scan_req *sr;
   2757  1.60.18.1      phil 	int error;
   2758  1.60.18.1      phil 
   2759  1.60.18.1      phil 	if (ireq->i_len != sizeof(*sr))
   2760  1.60.18.1      phil 		return EINVAL;
   2761  1.60.18.1      phil 	sr = IEEE80211_MALLOC(sizeof(*sr), M_TEMP,
   2762  1.60.18.1      phil 	     IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
   2763  1.60.18.1      phil 	if (sr == NULL)
   2764  1.60.18.1      phil 		return ENOMEM;
   2765  1.60.18.1      phil 	error = copyin(ireq->i_data, sr, sizeof(*sr));
   2766  1.60.18.1      phil 	if (error != 0)
   2767  1.60.18.1      phil 		goto bad;
   2768  1.60.18.1      phil 	error = ieee80211_scanreq(vap, sr);
   2769  1.60.18.1      phil bad:
   2770  1.60.18.1      phil 	IEEE80211_FREE(sr, M_TEMP);
   2771  1.60.18.1      phil 	return error;
   2772  1.60.18.1      phil }
   2773  1.60.18.1      phil 
   2774  1.60.18.1      phil static int
   2775  1.60.18.1      phil ieee80211_ioctl_setstavlan(struct ieee80211vap *vap, struct ieee80211req *ireq)
   2776  1.60.18.1      phil {
   2777  1.60.18.1      phil 	struct ieee80211_node *ni;
   2778  1.60.18.1      phil 	struct ieee80211req_sta_vlan vlan;
   2779  1.60.18.1      phil 	int error;
   2780  1.60.18.1      phil 
   2781  1.60.18.1      phil 	if (ireq->i_len != sizeof(vlan))
   2782  1.60.18.1      phil 		return EINVAL;
   2783  1.60.18.1      phil 	error = copyin(ireq->i_data, &vlan, sizeof(vlan));
   2784  1.60.18.1      phil 	if (error != 0)
   2785  1.60.18.1      phil 		return error;
   2786  1.60.18.1      phil 	if (!IEEE80211_ADDR_EQ(vlan.sv_macaddr, zerobssid)) {
   2787  1.60.18.1      phil 		ni = ieee80211_find_vap_node(&vap->iv_ic->ic_sta, vap,
   2788  1.60.18.1      phil 		    vlan.sv_macaddr);
   2789  1.60.18.1      phil 		if (ni == NULL)
   2790  1.60.18.1      phil 			return ENOENT;
   2791  1.60.18.1      phil 	} else
   2792  1.60.18.1      phil 		ni = ieee80211_ref_node(vap->iv_bss);
   2793  1.60.18.1      phil 	ni->ni_vlan = vlan.sv_vlan;
   2794  1.60.18.1      phil 	ieee80211_free_node(ni);
   2795  1.60.18.1      phil 	return error;
   2796  1.60.18.1      phil }
   2797  1.60.18.1      phil 
   2798  1.60.18.1      phil static int
   2799  1.60.18.1      phil isvap11g(const struct ieee80211vap *vap)
   2800  1.60.18.1      phil {
   2801  1.60.18.1      phil 	const struct ieee80211_node *bss = vap->iv_bss;
   2802  1.60.18.1      phil 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
   2803  1.60.18.1      phil 	    IEEE80211_IS_CHAN_ANYG(bss->ni_chan);
   2804  1.60.18.1      phil }
   2805  1.60.18.1      phil 
   2806  1.60.18.1      phil static int
   2807  1.60.18.1      phil isvapht(const struct ieee80211vap *vap)
   2808  1.60.18.1      phil {
   2809  1.60.18.1      phil 	const struct ieee80211_node *bss = vap->iv_bss;
   2810  1.60.18.1      phil 	return bss->ni_chan != IEEE80211_CHAN_ANYC &&
   2811  1.60.18.1      phil 	    IEEE80211_IS_CHAN_HT(bss->ni_chan);
   2812  1.60.18.1      phil }
   2813  1.60.18.1      phil 
   2814  1.60.18.1      phil /*
   2815  1.60.18.1      phil  * Dummy ioctl set handler so the linker set is defined.
   2816  1.60.18.1      phil  */
   2817  1.60.18.2      phil static __unused int
   2818  1.60.18.1      phil dummy_ioctl_set(struct ieee80211vap *vap, struct ieee80211req *ireq)
   2819  1.60.18.1      phil {
   2820  1.60.18.1      phil 	return ENOSYS;
   2821  1.60.18.1      phil }
   2822  1.60.18.1      phil IEEE80211_IOCTL_SET(dummy, dummy_ioctl_set);
   2823  1.60.18.1      phil 
   2824  1.60.18.1      phil static int
   2825  1.60.18.1      phil ieee80211_ioctl_setdefault(struct ieee80211vap *vap, struct ieee80211req *ireq)
   2826  1.60.18.1      phil {
   2827  1.60.18.2      phil #ifdef notyet
   2828  1.60.18.1      phil 	ieee80211_ioctl_setfunc * const *set;
   2829  1.60.18.1      phil 	int error;
   2830  1.60.18.1      phil 
   2831  1.60.18.1      phil 	SET_FOREACH(set, ieee80211_ioctl_setset) {
   2832  1.60.18.1      phil 		error = (*set)(vap, ireq);
   2833  1.60.18.1      phil 		if (error != ENOSYS)
   2834  1.60.18.1      phil 			return error;
   2835  1.60.18.1      phil 	}
   2836  1.60.18.3      phil #else
   2837  1.60.18.3      phil 	printf ("ieee80211_ioctl_setdefault called\n");
   2838  1.60.18.2      phil #endif
   2839  1.60.18.1      phil 	return EINVAL;
   2840  1.60.18.1      phil }
   2841  1.60.18.1      phil 
   2842  1.60.18.1      phil static int
   2843  1.60.18.1      phil ieee80211_ioctl_set80211(struct ieee80211vap *vap, u_long cmd, struct ieee80211req *ireq)
   2844  1.60.18.1      phil {
   2845  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   2846  1.60.18.1      phil 	int error;
   2847  1.60.18.1      phil 	const struct ieee80211_authenticator *auth;
   2848  1.60.18.1      phil 	uint8_t tmpkey[IEEE80211_KEYBUF_SIZE];
   2849  1.60.18.1      phil 	char tmpssid[IEEE80211_NWID_LEN];
   2850  1.60.18.1      phil 	uint8_t tmpbssid[IEEE80211_ADDR_LEN];
   2851  1.60.18.1      phil 	struct ieee80211_key *k;
   2852  1.60.18.1      phil 	u_int kid;
   2853  1.60.18.1      phil 	uint32_t flags;
   2854  1.60.18.1      phil 
   2855  1.60.18.1      phil 	error = 0;
   2856  1.60.18.1      phil 	switch (ireq->i_type) {
   2857  1.60.18.1      phil 	case IEEE80211_IOC_SSID:
   2858  1.60.18.1      phil 		if (ireq->i_val != 0 ||
   2859  1.60.18.1      phil 		    ireq->i_len > IEEE80211_NWID_LEN)
   2860  1.60.18.1      phil 			return EINVAL;
   2861  1.60.18.1      phil 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
   2862  1.60.18.1      phil 		if (error)
   2863  1.60.18.1      phil 			break;
   2864  1.60.18.1      phil 		memset(vap->iv_des_ssid[0].ssid, 0, IEEE80211_NWID_LEN);
   2865  1.60.18.1      phil 		vap->iv_des_ssid[0].len = ireq->i_len;
   2866  1.60.18.1      phil 		memcpy(vap->iv_des_ssid[0].ssid, tmpssid, ireq->i_len);
   2867  1.60.18.1      phil 		vap->iv_des_nssid = (ireq->i_len > 0);
   2868  1.60.18.1      phil 		error = ENETRESET;
   2869  1.60.18.1      phil 		break;
   2870  1.60.18.1      phil 	case IEEE80211_IOC_WEP:
   2871  1.60.18.1      phil 		switch (ireq->i_val) {
   2872  1.60.18.1      phil 		case IEEE80211_WEP_OFF:
   2873  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
   2874  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
   2875  1.60.18.1      phil 			break;
   2876  1.60.18.1      phil 		case IEEE80211_WEP_ON:
   2877  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_PRIVACY;
   2878  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
   2879  1.60.18.1      phil 			break;
   2880  1.60.18.1      phil 		case IEEE80211_WEP_MIXED:
   2881  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_PRIVACY;
   2882  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
   2883  1.60.18.1      phil 			break;
   2884  1.60.18.1      phil 		}
   2885  1.60.18.1      phil 		error = ENETRESET;
   2886  1.60.18.1      phil 		break;
   2887  1.60.18.1      phil 	case IEEE80211_IOC_WEPKEY:
   2888  1.60.18.1      phil 		kid = (u_int) ireq->i_val;
   2889  1.60.18.1      phil 		if (kid >= IEEE80211_WEP_NKID)
   2890  1.60.18.1      phil 			return EINVAL;
   2891  1.60.18.1      phil 		k = &vap->iv_nw_keys[kid];
   2892  1.60.18.1      phil 		if (ireq->i_len == 0) {
   2893  1.60.18.1      phil 			/* zero-len =>'s delete any existing key */
   2894  1.60.18.1      phil 			(void) ieee80211_crypto_delkey(vap, k);
   2895  1.60.18.1      phil 			break;
   2896  1.60.18.1      phil 		}
   2897  1.60.18.1      phil 		if (ireq->i_len > sizeof(tmpkey))
   2898  1.60.18.1      phil 			return EINVAL;
   2899  1.60.18.1      phil 		memset(tmpkey, 0, sizeof(tmpkey));
   2900  1.60.18.1      phil 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
   2901  1.60.18.1      phil 		if (error)
   2902  1.60.18.1      phil 			break;
   2903  1.60.18.1      phil 		ieee80211_key_update_begin(vap);
   2904  1.60.18.1      phil 		k->wk_keyix = kid;	/* NB: force fixed key id */
   2905  1.60.18.1      phil 		if (ieee80211_crypto_newkey(vap, IEEE80211_CIPHER_WEP,
   2906  1.60.18.1      phil 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
   2907  1.60.18.1      phil 			k->wk_keylen = ireq->i_len;
   2908       1.19    dyoung 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
   2909  1.60.18.1      phil 			IEEE80211_ADDR_COPY(k->wk_macaddr, vap->iv_myaddr);
   2910  1.60.18.1      phil 			if  (!ieee80211_crypto_setkey(vap, k))
   2911       1.19    dyoung 				error = EINVAL;
   2912       1.19    dyoung 		} else
   2913       1.19    dyoung 			error = EINVAL;
   2914  1.60.18.1      phil 		ieee80211_key_update_end(vap);
   2915       1.19    dyoung 		break;
   2916       1.19    dyoung 	case IEEE80211_IOC_WEPTXKEY:
   2917       1.19    dyoung 		kid = (u_int) ireq->i_val;
   2918       1.19    dyoung 		if (kid >= IEEE80211_WEP_NKID &&
   2919  1.60.18.1      phil 		    (uint16_t) kid != IEEE80211_KEYIX_NONE)
   2920       1.19    dyoung 			return EINVAL;
   2921  1.60.18.1      phil 		/*
   2922  1.60.18.1      phil 		 * Firmware devices may need to be told about an explicit
   2923  1.60.18.1      phil 		 * key index here, versus just inferring it from the
   2924  1.60.18.1      phil 		 * key set / change.  Since we may also need to pause
   2925  1.60.18.1      phil 		 * things like transmit before the key is updated,
   2926  1.60.18.1      phil 		 * give the driver a chance to flush things by tying
   2927  1.60.18.1      phil 		 * into key update begin/end.
   2928  1.60.18.1      phil 		 */
   2929  1.60.18.1      phil 		ieee80211_key_update_begin(vap);
   2930  1.60.18.1      phil 		ieee80211_crypto_set_deftxkey(vap, kid);
   2931  1.60.18.1      phil 		ieee80211_key_update_end(vap);
   2932       1.19    dyoung 		break;
   2933       1.19    dyoung 	case IEEE80211_IOC_AUTHMODE:
   2934       1.19    dyoung 		switch (ireq->i_val) {
   2935       1.19    dyoung 		case IEEE80211_AUTH_WPA:
   2936       1.19    dyoung 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2937       1.19    dyoung 		case IEEE80211_AUTH_OPEN:	/* open */
   2938       1.19    dyoung 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2939       1.19    dyoung 		case IEEE80211_AUTH_AUTO:	/* auto */
   2940       1.19    dyoung 			auth = ieee80211_authenticator_get(ireq->i_val);
   2941       1.19    dyoung 			if (auth == NULL)
   2942       1.19    dyoung 				return EINVAL;
   2943        1.1    dyoung 			break;
   2944       1.19    dyoung 		default:
   2945       1.19    dyoung 			return EINVAL;
   2946       1.19    dyoung 		}
   2947       1.19    dyoung 		switch (ireq->i_val) {
   2948       1.19    dyoung 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
   2949  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_PRIVACY;
   2950       1.19    dyoung 			ireq->i_val = IEEE80211_AUTH_8021X;
   2951        1.1    dyoung 			break;
   2952       1.19    dyoung 		case IEEE80211_AUTH_OPEN:	/* open */
   2953  1.60.18.1      phil 			vap->iv_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
   2954       1.10    dyoung 			break;
   2955       1.19    dyoung 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2956       1.19    dyoung 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2957  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_WPA;
   2958       1.19    dyoung 			/* both require a key so mark the PRIVACY capability */
   2959  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_PRIVACY;
   2960       1.10    dyoung 			break;
   2961       1.19    dyoung 		case IEEE80211_AUTH_AUTO:	/* auto */
   2962  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_WPA;
   2963       1.19    dyoung 			/* XXX PRIVACY handling? */
   2964       1.19    dyoung 			/* XXX what's the right way to do this? */
   2965       1.10    dyoung 			break;
   2966        1.1    dyoung 		}
   2967       1.19    dyoung 		/* NB: authenticator attach/detach happens on state change */
   2968  1.60.18.1      phil 		vap->iv_bss->ni_authmode = ireq->i_val;
   2969       1.19    dyoung 		/* XXX mixed/mode/usage? */
   2970  1.60.18.1      phil 		vap->iv_auth = auth;
   2971       1.19    dyoung 		error = ENETRESET;
   2972        1.1    dyoung 		break;
   2973       1.19    dyoung 	case IEEE80211_IOC_CHANNEL:
   2974  1.60.18.1      phil 		error = ieee80211_ioctl_setchannel(vap, ireq);
   2975       1.19    dyoung 		break;
   2976       1.19    dyoung 	case IEEE80211_IOC_POWERSAVE:
   2977       1.19    dyoung 		switch (ireq->i_val) {
   2978       1.19    dyoung 		case IEEE80211_POWERSAVE_OFF:
   2979  1.60.18.1      phil 			if (vap->iv_flags & IEEE80211_F_PMGTON) {
   2980  1.60.18.1      phil 				ieee80211_syncflag(vap, -IEEE80211_F_PMGTON);
   2981  1.60.18.1      phil 				error = ERESTART;
   2982       1.18     perry 			}
   2983        1.1    dyoung 			break;
   2984       1.19    dyoung 		case IEEE80211_POWERSAVE_ON:
   2985  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_PMGT) == 0)
   2986  1.60.18.1      phil 				error = EOPNOTSUPP;
   2987  1.60.18.1      phil 			else if ((vap->iv_flags & IEEE80211_F_PMGTON) == 0) {
   2988  1.60.18.1      phil 				ieee80211_syncflag(vap, IEEE80211_F_PMGTON);
   2989  1.60.18.1      phil 				error = ERESTART;
   2990        1.1    dyoung 			}
   2991        1.1    dyoung 			break;
   2992       1.19    dyoung 		default:
   2993       1.19    dyoung 			error = EINVAL;
   2994        1.1    dyoung 			break;
   2995       1.19    dyoung 		}
   2996       1.19    dyoung 		break;
   2997       1.19    dyoung 	case IEEE80211_IOC_POWERSAVESLEEP:
   2998       1.19    dyoung 		if (ireq->i_val < 0)
   2999       1.19    dyoung 			return EINVAL;
   3000       1.19    dyoung 		ic->ic_lintval = ireq->i_val;
   3001  1.60.18.1      phil 		error = ERESTART;
   3002       1.19    dyoung 		break;
   3003       1.19    dyoung 	case IEEE80211_IOC_RTSTHRESHOLD:
   3004       1.26     skrll 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
   3005       1.26     skrll 		      ireq->i_val <= IEEE80211_RTS_MAX))
   3006       1.19    dyoung 			return EINVAL;
   3007  1.60.18.1      phil 		vap->iv_rtsthreshold = ireq->i_val;
   3008  1.60.18.1      phil 		error = ERESTART;
   3009       1.19    dyoung 		break;
   3010       1.19    dyoung 	case IEEE80211_IOC_PROTMODE:
   3011       1.19    dyoung 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
   3012       1.19    dyoung 			return EINVAL;
   3013  1.60.18.1      phil 		ic->ic_protmode = (enum ieee80211_protmode)ireq->i_val;
   3014       1.19    dyoung 		/* NB: if not operating in 11g this can wait */
   3015  1.60.18.1      phil 		if (ic->ic_bsschan != IEEE80211_CHAN_ANYC &&
   3016  1.60.18.1      phil 		    IEEE80211_IS_CHAN_ANYG(ic->ic_bsschan))
   3017  1.60.18.1      phil 			error = ERESTART;
   3018       1.19    dyoung 		break;
   3019       1.19    dyoung 	case IEEE80211_IOC_TXPOWER:
   3020       1.19    dyoung 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
   3021  1.60.18.1      phil 			return EOPNOTSUPP;
   3022  1.60.18.1      phil 		if (!(IEEE80211_TXPOWER_MIN <= ireq->i_val &&
   3023  1.60.18.1      phil 		      ireq->i_val <= IEEE80211_TXPOWER_MAX))
   3024       1.19    dyoung 			return EINVAL;
   3025       1.19    dyoung 		ic->ic_txpowlimit = ireq->i_val;
   3026  1.60.18.1      phil 		error = ERESTART;
   3027       1.19    dyoung 		break;
   3028       1.19    dyoung 	case IEEE80211_IOC_ROAMING:
   3029       1.19    dyoung 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
   3030       1.19    dyoung 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
   3031       1.19    dyoung 			return EINVAL;
   3032  1.60.18.1      phil 		vap->iv_roaming = (enum ieee80211_roamingmode)ireq->i_val;
   3033       1.19    dyoung 		/* XXXX reset? */
   3034       1.19    dyoung 		break;
   3035       1.19    dyoung 	case IEEE80211_IOC_PRIVACY:
   3036       1.19    dyoung 		if (ireq->i_val) {
   3037       1.19    dyoung 			/* XXX check for key state? */
   3038  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_PRIVACY;
   3039       1.19    dyoung 		} else
   3040  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_PRIVACY;
   3041  1.60.18.1      phil 		/* XXX ERESTART? */
   3042       1.19    dyoung 		break;
   3043       1.19    dyoung 	case IEEE80211_IOC_DROPUNENCRYPTED:
   3044       1.19    dyoung 		if (ireq->i_val)
   3045  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_DROPUNENC;
   3046       1.19    dyoung 		else
   3047  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_DROPUNENC;
   3048  1.60.18.1      phil 		/* XXX ERESTART? */
   3049       1.19    dyoung 		break;
   3050       1.19    dyoung 	case IEEE80211_IOC_WPAKEY:
   3051  1.60.18.1      phil 		error = ieee80211_ioctl_setkey(vap, ireq);
   3052       1.19    dyoung 		break;
   3053       1.19    dyoung 	case IEEE80211_IOC_DELKEY:
   3054  1.60.18.1      phil 		error = ieee80211_ioctl_delkey(vap, ireq);
   3055       1.19    dyoung 		break;
   3056       1.19    dyoung 	case IEEE80211_IOC_MLME:
   3057  1.60.18.1      phil 		error = ieee80211_ioctl_setmlme(vap, ireq);
   3058       1.19    dyoung 		break;
   3059       1.19    dyoung 	case IEEE80211_IOC_COUNTERMEASURES:
   3060       1.19    dyoung 		if (ireq->i_val) {
   3061  1.60.18.1      phil 			if ((vap->iv_flags & IEEE80211_F_WPA) == 0)
   3062  1.60.18.1      phil 				return EOPNOTSUPP;
   3063  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_COUNTERM;
   3064       1.19    dyoung 		} else
   3065  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_COUNTERM;
   3066  1.60.18.1      phil 		/* XXX ERESTART? */
   3067       1.19    dyoung 		break;
   3068       1.19    dyoung 	case IEEE80211_IOC_WPA:
   3069       1.19    dyoung 		if (ireq->i_val > 3)
   3070       1.19    dyoung 			return EINVAL;
   3071       1.19    dyoung 		/* XXX verify ciphers available */
   3072  1.60.18.1      phil 		flags = vap->iv_flags & ~IEEE80211_F_WPA;
   3073       1.19    dyoung 		switch (ireq->i_val) {
   3074  1.60.18.1      phil 		case 0:
   3075  1.60.18.1      phil 			/* wpa_supplicant calls this to clear the WPA config */
   3076  1.60.18.1      phil 			break;
   3077       1.19    dyoung 		case 1:
   3078  1.60.18.1      phil 			if (!(vap->iv_caps & IEEE80211_C_WPA1))
   3079  1.60.18.1      phil 				return EOPNOTSUPP;
   3080  1.60.18.1      phil 			flags |= IEEE80211_F_WPA1;
   3081        1.1    dyoung 			break;
   3082       1.19    dyoung 		case 2:
   3083  1.60.18.1      phil 			if (!(vap->iv_caps & IEEE80211_C_WPA2))
   3084  1.60.18.1      phil 				return EOPNOTSUPP;
   3085  1.60.18.1      phil 			flags |= IEEE80211_F_WPA2;
   3086        1.1    dyoung 			break;
   3087       1.19    dyoung 		case 3:
   3088  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_WPA) != IEEE80211_C_WPA)
   3089  1.60.18.1      phil 				return EOPNOTSUPP;
   3090  1.60.18.1      phil 			flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
   3091        1.1    dyoung 			break;
   3092  1.60.18.1      phil 		default:	/*  Can't set any -> error */
   3093  1.60.18.1      phil 			return EOPNOTSUPP;
   3094       1.19    dyoung 		}
   3095  1.60.18.1      phil 		vap->iv_flags = flags;
   3096  1.60.18.1      phil 		error = ERESTART;	/* NB: can change beacon frame */
   3097       1.19    dyoung 		break;
   3098       1.19    dyoung 	case IEEE80211_IOC_WME:
   3099       1.19    dyoung 		if (ireq->i_val) {
   3100  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_WME) == 0)
   3101  1.60.18.1      phil 				return EOPNOTSUPP;
   3102  1.60.18.1      phil 			ieee80211_syncflag(vap, IEEE80211_F_WME);
   3103       1.19    dyoung 		} else
   3104  1.60.18.1      phil 			ieee80211_syncflag(vap, -IEEE80211_F_WME);
   3105  1.60.18.1      phil 		error = ERESTART;	/* NB: can change beacon frame */
   3106       1.19    dyoung 		break;
   3107       1.19    dyoung 	case IEEE80211_IOC_HIDESSID:
   3108       1.19    dyoung 		if (ireq->i_val)
   3109  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_HIDESSID;
   3110       1.19    dyoung 		else
   3111  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_HIDESSID;
   3112  1.60.18.1      phil 		error = ERESTART;		/* XXX ENETRESET? */
   3113       1.19    dyoung 		break;
   3114       1.19    dyoung 	case IEEE80211_IOC_APBRIDGE:
   3115       1.19    dyoung 		if (ireq->i_val == 0)
   3116  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_NOBRIDGE;
   3117       1.19    dyoung 		else
   3118  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_NOBRIDGE;
   3119       1.19    dyoung 		break;
   3120  1.60.18.1      phil 	case IEEE80211_IOC_BSSID:
   3121  1.60.18.1      phil 		if (ireq->i_len != sizeof(tmpbssid))
   3122       1.19    dyoung 			return EINVAL;
   3123  1.60.18.1      phil 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
   3124  1.60.18.1      phil 		if (error)
   3125  1.60.18.1      phil 			break;
   3126  1.60.18.1      phil 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, tmpbssid);
   3127  1.60.18.1      phil 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
   3128  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
   3129  1.60.18.1      phil 		else
   3130  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_DESBSSID;
   3131  1.60.18.1      phil 		error = ENETRESET;
   3132       1.19    dyoung 		break;
   3133  1.60.18.1      phil 	case IEEE80211_IOC_CHANLIST:
   3134  1.60.18.1      phil 		error = ieee80211_ioctl_setchanlist(vap, ireq);
   3135       1.19    dyoung 		break;
   3136  1.60.18.1      phil #ifdef OLD_IEEE80211_IOC_SCAN_REQ
   3137  1.60.18.1      phil 	case OLD_IEEE80211_IOC_SCAN_REQ:
   3138  1.60.18.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
   3139  1.60.18.1      phil 			"%s: active scan request\n", __func__);
   3140  1.60.18.5      phil 		printf ("old scan req\n");
   3141       1.19    dyoung 		/*
   3142  1.60.18.1      phil 		 * If we are in INIT state then the driver has never
   3143  1.60.18.1      phil 		 * had a chance to setup hardware state to do a scan;
   3144  1.60.18.1      phil 		 * use the state machine to get us up the SCAN state.
   3145  1.60.18.1      phil 		 * Otherwise just invoke the scan machinery to start
   3146  1.60.18.1      phil 		 * a one-time scan.
   3147       1.19    dyoung 		 */
   3148  1.60.18.1      phil 		if (vap->iv_state == IEEE80211_S_INIT)
   3149  1.60.18.5      phil {  printf ("calling new_state\n");
   3150  1.60.18.1      phil 			ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
   3151  1.60.18.5      phil }
   3152  1.60.18.1      phil 		else
   3153  1.60.18.5      phil {  printf("calling _start_scan()\n");
   3154  1.60.18.1      phil 			(void) ieee80211_start_scan(vap,
   3155  1.60.18.1      phil 				IEEE80211_SCAN_ACTIVE |
   3156  1.60.18.1      phil 				IEEE80211_SCAN_NOPICK |
   3157  1.60.18.1      phil 				IEEE80211_SCAN_ONCE,
   3158  1.60.18.1      phil 				IEEE80211_SCAN_FOREVER, 0, 0,
   3159  1.60.18.1      phil 				/* XXX use ioctl params */
   3160  1.60.18.1      phil 				vap->iv_des_nssid, vap->iv_des_ssid);
   3161  1.60.18.5      phil }
   3162       1.19    dyoung 		break;
   3163  1.60.18.1      phil #endif /* OLD_IEEE80211_IOC_SCAN_REQ */
   3164  1.60.18.1      phil 	case IEEE80211_IOC_SCAN_REQ:
   3165  1.60.18.1      phil 		error = ieee80211_ioctl_scanreq(vap, ireq);
   3166  1.60.18.1      phil 		break;
   3167  1.60.18.1      phil 	case IEEE80211_IOC_SCAN_CANCEL:
   3168  1.60.18.1      phil 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SCAN,
   3169  1.60.18.1      phil 		    "%s: cancel scan\n", __func__);
   3170  1.60.18.4      phil #if __NetBSD__
   3171  1.60.18.4      phil 		/* Bug in FreeBSD? */
   3172  1.60.18.4      phil 		IEEE80211_LOCK(ic);
   3173  1.60.18.4      phil #endif
   3174  1.60.18.1      phil 		ieee80211_cancel_scan(vap);
   3175  1.60.18.4      phil #if __NetBSD__
   3176  1.60.18.4      phil 		IEEE80211_UNLOCK(ic);
   3177  1.60.18.4      phil #endif
   3178  1.60.18.1      phil 		break;
   3179  1.60.18.1      phil 	case IEEE80211_IOC_HTCONF:
   3180  1.60.18.1      phil 		if (ireq->i_val & 1)
   3181  1.60.18.1      phil 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_HT);
   3182  1.60.18.1      phil 		else
   3183  1.60.18.1      phil 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_HT);
   3184  1.60.18.1      phil 		if (ireq->i_val & 2)
   3185  1.60.18.1      phil 			ieee80211_syncflag_ht(vap, IEEE80211_FHT_USEHT40);
   3186  1.60.18.1      phil 		else
   3187  1.60.18.1      phil 			ieee80211_syncflag_ht(vap, -IEEE80211_FHT_USEHT40);
   3188  1.60.18.1      phil 		error = ENETRESET;
   3189  1.60.18.1      phil 		break;
   3190  1.60.18.1      phil 	case IEEE80211_IOC_ADDMAC:
   3191  1.60.18.1      phil 	case IEEE80211_IOC_DELMAC:
   3192  1.60.18.1      phil 		error = ieee80211_ioctl_macmac(vap, ireq);
   3193  1.60.18.1      phil 		break;
   3194  1.60.18.1      phil 	case IEEE80211_IOC_MACCMD:
   3195  1.60.18.1      phil 		error = ieee80211_ioctl_setmaccmd(vap, ireq);
   3196  1.60.18.1      phil 		break;
   3197  1.60.18.1      phil 	case IEEE80211_IOC_STA_STATS:
   3198  1.60.18.1      phil 		error = ieee80211_ioctl_setstastats(vap, ireq);
   3199  1.60.18.1      phil 		break;
   3200  1.60.18.1      phil 	case IEEE80211_IOC_STA_TXPOW:
   3201  1.60.18.1      phil 		error = ieee80211_ioctl_setstatxpow(vap, ireq);
   3202  1.60.18.1      phil 		break;
   3203  1.60.18.1      phil 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   3204  1.60.18.1      phil 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   3205  1.60.18.1      phil 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   3206  1.60.18.1      phil 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   3207  1.60.18.1      phil 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   3208  1.60.18.1      phil 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only) */
   3209  1.60.18.1      phil 		error = ieee80211_ioctl_setwmeparam(vap, ireq);
   3210  1.60.18.1      phil 		break;
   3211  1.60.18.1      phil 	case IEEE80211_IOC_DTIM_PERIOD:
   3212  1.60.18.1      phil 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
   3213  1.60.18.1      phil 		    vap->iv_opmode != IEEE80211_M_MBSS &&
   3214  1.60.18.1      phil 		    vap->iv_opmode != IEEE80211_M_IBSS)
   3215  1.60.18.1      phil 			return EINVAL;
   3216  1.60.18.1      phil 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
   3217  1.60.18.1      phil 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
   3218  1.60.18.1      phil 			vap->iv_dtim_period = ireq->i_val;
   3219  1.60.18.1      phil 			error = ENETRESET;		/* requires restart */
   3220  1.60.18.1      phil 		} else
   3221  1.60.18.1      phil 			error = EINVAL;
   3222  1.60.18.1      phil 		break;
   3223  1.60.18.1      phil 	case IEEE80211_IOC_BEACON_INTERVAL:
   3224  1.60.18.1      phil 		if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
   3225  1.60.18.1      phil 		    vap->iv_opmode != IEEE80211_M_MBSS &&
   3226  1.60.18.1      phil 		    vap->iv_opmode != IEEE80211_M_IBSS)
   3227  1.60.18.1      phil 			return EINVAL;
   3228  1.60.18.1      phil 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
   3229  1.60.18.1      phil 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
   3230  1.60.18.1      phil 			ic->ic_bintval = ireq->i_val;
   3231  1.60.18.1      phil 			error = ENETRESET;		/* requires restart */
   3232  1.60.18.1      phil 		} else
   3233  1.60.18.1      phil 			error = EINVAL;
   3234  1.60.18.1      phil 		break;
   3235  1.60.18.1      phil 	case IEEE80211_IOC_PUREG:
   3236  1.60.18.1      phil 		if (ireq->i_val)
   3237  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_PUREG;
   3238  1.60.18.1      phil 		else
   3239  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_PUREG;
   3240  1.60.18.1      phil 		/* NB: reset only if we're operating on an 11g channel */
   3241  1.60.18.1      phil 		if (isvap11g(vap))
   3242  1.60.18.1      phil 			error = ENETRESET;
   3243  1.60.18.1      phil 		break;
   3244  1.60.18.1      phil 	case IEEE80211_IOC_QUIET:
   3245  1.60.18.1      phil 		vap->iv_quiet= ireq->i_val;
   3246  1.60.18.1      phil 		break;
   3247  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_COUNT:
   3248  1.60.18.1      phil 		vap->iv_quiet_count=ireq->i_val;
   3249  1.60.18.1      phil 		break;
   3250  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_PERIOD:
   3251  1.60.18.1      phil 		vap->iv_quiet_period=ireq->i_val;
   3252  1.60.18.1      phil 		break;
   3253  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_OFFSET:
   3254  1.60.18.1      phil 		vap->iv_quiet_offset=ireq->i_val;
   3255  1.60.18.1      phil 		break;
   3256  1.60.18.1      phil 	case IEEE80211_IOC_QUIET_DUR:
   3257  1.60.18.1      phil 		if(ireq->i_val < vap->iv_bss->ni_intval)
   3258  1.60.18.1      phil 			vap->iv_quiet_duration = ireq->i_val;
   3259  1.60.18.1      phil 		else
   3260  1.60.18.1      phil 			error = EINVAL;
   3261  1.60.18.1      phil 		break;
   3262  1.60.18.1      phil 	case IEEE80211_IOC_BGSCAN:
   3263  1.60.18.1      phil 		if (ireq->i_val) {
   3264  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_BGSCAN) == 0)
   3265  1.60.18.1      phil 				return EOPNOTSUPP;
   3266  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_BGSCAN;
   3267  1.60.18.1      phil 		} else
   3268  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_BGSCAN;
   3269  1.60.18.1      phil 		break;
   3270  1.60.18.1      phil 	case IEEE80211_IOC_BGSCAN_IDLE:
   3271  1.60.18.1      phil 		if (ireq->i_val >= IEEE80211_BGSCAN_IDLE_MIN)
   3272  1.60.18.1      phil 			vap->iv_bgscanidle = ireq->i_val*hz/1000;
   3273  1.60.18.1      phil 		else
   3274  1.60.18.1      phil 			error = EINVAL;
   3275  1.60.18.1      phil 		break;
   3276  1.60.18.1      phil 	case IEEE80211_IOC_BGSCAN_INTERVAL:
   3277  1.60.18.1      phil 		if (ireq->i_val >= IEEE80211_BGSCAN_INTVAL_MIN)
   3278  1.60.18.1      phil 			vap->iv_bgscanintvl = ireq->i_val*hz;
   3279  1.60.18.1      phil 		else
   3280  1.60.18.1      phil 			error = EINVAL;
   3281  1.60.18.1      phil 		break;
   3282  1.60.18.1      phil 	case IEEE80211_IOC_SCANVALID:
   3283  1.60.18.1      phil 		if (ireq->i_val >= IEEE80211_SCAN_VALID_MIN)
   3284  1.60.18.1      phil 			vap->iv_scanvalid = ireq->i_val*hz;
   3285  1.60.18.1      phil 		else
   3286  1.60.18.1      phil 			error = EINVAL;
   3287  1.60.18.1      phil 		break;
   3288  1.60.18.1      phil 	case IEEE80211_IOC_FRAGTHRESHOLD:
   3289  1.60.18.1      phil 		if ((vap->iv_caps & IEEE80211_C_TXFRAG) == 0 &&
   3290  1.60.18.1      phil 		    ireq->i_val != IEEE80211_FRAG_MAX)
   3291  1.60.18.1      phil 			return EOPNOTSUPP;
   3292  1.60.18.1      phil 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
   3293  1.60.18.1      phil 		      ireq->i_val <= IEEE80211_FRAG_MAX))
   3294  1.60.18.1      phil 			return EINVAL;
   3295  1.60.18.1      phil 		vap->iv_fragthreshold = ireq->i_val;
   3296  1.60.18.1      phil 		error = ERESTART;
   3297  1.60.18.1      phil 		break;
   3298  1.60.18.1      phil 	case IEEE80211_IOC_BURST:
   3299  1.60.18.1      phil 		if (ireq->i_val) {
   3300  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_BURST) == 0)
   3301  1.60.18.1      phil 				return EOPNOTSUPP;
   3302  1.60.18.1      phil 			ieee80211_syncflag(vap, IEEE80211_F_BURST);
   3303  1.60.18.1      phil 		} else
   3304  1.60.18.1      phil 			ieee80211_syncflag(vap, -IEEE80211_F_BURST);
   3305  1.60.18.1      phil 		error = ERESTART;
   3306  1.60.18.1      phil 		break;
   3307  1.60.18.1      phil 	case IEEE80211_IOC_BMISSTHRESHOLD:
   3308  1.60.18.1      phil 		if (!(IEEE80211_HWBMISS_MIN <= ireq->i_val &&
   3309  1.60.18.1      phil 		      ireq->i_val <= IEEE80211_HWBMISS_MAX))
   3310  1.60.18.1      phil 			return EINVAL;
   3311  1.60.18.1      phil 		vap->iv_bmissthreshold = ireq->i_val;
   3312  1.60.18.1      phil 		error = ERESTART;
   3313  1.60.18.1      phil 		break;
   3314  1.60.18.1      phil 	case IEEE80211_IOC_CURCHAN:
   3315  1.60.18.1      phil 		error = ieee80211_ioctl_setcurchan(vap, ireq);
   3316  1.60.18.1      phil 		break;
   3317  1.60.18.1      phil 	case IEEE80211_IOC_SHORTGI:
   3318  1.60.18.1      phil 		if (ireq->i_val) {
   3319  1.60.18.1      phil #define	IEEE80211_HTCAP_SHORTGI \
   3320  1.60.18.1      phil 	(IEEE80211_HTCAP_SHORTGI20 | IEEE80211_HTCAP_SHORTGI40)
   3321  1.60.18.1      phil 			if (((ireq->i_val ^ vap->iv_htcaps) & IEEE80211_HTCAP_SHORTGI) != 0)
   3322  1.60.18.1      phil 				return EINVAL;
   3323  1.60.18.1      phil 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI20)
   3324  1.60.18.1      phil 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI20;
   3325  1.60.18.1      phil 			if (ireq->i_val & IEEE80211_HTCAP_SHORTGI40)
   3326  1.60.18.1      phil 				vap->iv_flags_ht |= IEEE80211_FHT_SHORTGI40;
   3327  1.60.18.1      phil #undef IEEE80211_HTCAP_SHORTGI
   3328  1.60.18.1      phil 		} else
   3329  1.60.18.1      phil 			vap->iv_flags_ht &=
   3330  1.60.18.1      phil 			    ~(IEEE80211_FHT_SHORTGI20 | IEEE80211_FHT_SHORTGI40);
   3331  1.60.18.1      phil 		error = ERESTART;
   3332  1.60.18.1      phil 		break;
   3333  1.60.18.1      phil 	case IEEE80211_IOC_AMPDU:
   3334  1.60.18.1      phil 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMPDU) == 0)
   3335  1.60.18.1      phil 			return EINVAL;
   3336  1.60.18.1      phil 		if (ireq->i_val & 1)
   3337  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_TX;
   3338  1.60.18.1      phil 		else
   3339  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_TX;
   3340  1.60.18.1      phil 		if (ireq->i_val & 2)
   3341  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_AMPDU_RX;
   3342  1.60.18.1      phil 		else
   3343  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
   3344  1.60.18.1      phil 		/* NB: reset only if we're operating on an 11n channel */
   3345  1.60.18.1      phil 		if (isvapht(vap))
   3346  1.60.18.1      phil 			error = ERESTART;
   3347  1.60.18.1      phil 		break;
   3348  1.60.18.1      phil 	case IEEE80211_IOC_AMPDU_LIMIT:
   3349  1.60.18.1      phil 		/* XXX TODO: figure out ampdu_limit versus ampdu_rxmax */
   3350  1.60.18.1      phil 		if (!(IEEE80211_HTCAP_MAXRXAMPDU_8K <= ireq->i_val &&
   3351  1.60.18.1      phil 		      ireq->i_val <= IEEE80211_HTCAP_MAXRXAMPDU_64K))
   3352  1.60.18.1      phil 			return EINVAL;
   3353  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_HOSTAP)
   3354  1.60.18.1      phil 			vap->iv_ampdu_rxmax = ireq->i_val;
   3355  1.60.18.1      phil 		else
   3356  1.60.18.1      phil 			vap->iv_ampdu_limit = ireq->i_val;
   3357  1.60.18.1      phil 		error = ERESTART;
   3358  1.60.18.1      phil 		break;
   3359  1.60.18.1      phil 	case IEEE80211_IOC_AMPDU_DENSITY:
   3360  1.60.18.1      phil 		if (!(IEEE80211_HTCAP_MPDUDENSITY_NA <= ireq->i_val &&
   3361  1.60.18.1      phil 		      ireq->i_val <= IEEE80211_HTCAP_MPDUDENSITY_16))
   3362       1.19    dyoung 			return EINVAL;
   3363  1.60.18.1      phil 		vap->iv_ampdu_density = ireq->i_val;
   3364  1.60.18.1      phil 		error = ERESTART;
   3365  1.60.18.1      phil 		break;
   3366  1.60.18.1      phil 	case IEEE80211_IOC_AMSDU:
   3367  1.60.18.1      phil 		if (ireq->i_val && (vap->iv_htcaps & IEEE80211_HTC_AMSDU) == 0)
   3368       1.19    dyoung 			return EINVAL;
   3369  1.60.18.1      phil 		if (ireq->i_val & 1)
   3370  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_TX;
   3371  1.60.18.1      phil 		else
   3372  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_TX;
   3373  1.60.18.1      phil 		if (ireq->i_val & 2)
   3374  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_AMSDU_RX;
   3375       1.19    dyoung 		else
   3376  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_AMSDU_RX;
   3377  1.60.18.1      phil 		/* NB: reset only if we're operating on an 11n channel */
   3378  1.60.18.1      phil 		if (isvapht(vap))
   3379  1.60.18.1      phil 			error = ERESTART;
   3380  1.60.18.1      phil 		break;
   3381  1.60.18.1      phil 	case IEEE80211_IOC_AMSDU_LIMIT:
   3382  1.60.18.1      phil 		/* XXX validate */
   3383  1.60.18.1      phil 		vap->iv_amsdu_limit = ireq->i_val;	/* XXX truncation? */
   3384  1.60.18.1      phil 		break;
   3385  1.60.18.1      phil 	case IEEE80211_IOC_PUREN:
   3386  1.60.18.1      phil 		if (ireq->i_val) {
   3387  1.60.18.1      phil 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
   3388  1.60.18.1      phil 				return EINVAL;
   3389  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_PUREN;
   3390  1.60.18.1      phil 		} else
   3391  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_PUREN;
   3392  1.60.18.1      phil 		/* NB: reset only if we're operating on an 11n channel */
   3393  1.60.18.1      phil 		if (isvapht(vap))
   3394  1.60.18.1      phil 			error = ERESTART;
   3395  1.60.18.1      phil 		break;
   3396  1.60.18.1      phil 	case IEEE80211_IOC_DOTH:
   3397  1.60.18.1      phil 		if (ireq->i_val) {
   3398  1.60.18.1      phil #if 0
   3399  1.60.18.1      phil 			/* XXX no capability */
   3400  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_DOTH) == 0)
   3401  1.60.18.1      phil 				return EOPNOTSUPP;
   3402  1.60.18.1      phil #endif
   3403  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_DOTH;
   3404  1.60.18.1      phil 		} else
   3405  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_DOTH;
   3406       1.19    dyoung 		error = ENETRESET;
   3407       1.19    dyoung 		break;
   3408  1.60.18.1      phil 	case IEEE80211_IOC_REGDOMAIN:
   3409  1.60.18.1      phil 		error = ieee80211_ioctl_setregdomain(vap, ireq);
   3410       1.19    dyoung 		break;
   3411  1.60.18.1      phil 	case IEEE80211_IOC_ROAM:
   3412  1.60.18.1      phil 		error = ieee80211_ioctl_setroam(vap, ireq);
   3413       1.19    dyoung 		break;
   3414  1.60.18.1      phil 	case IEEE80211_IOC_TXPARAMS:
   3415  1.60.18.1      phil 		error = ieee80211_ioctl_settxparams(vap, ireq);
   3416       1.19    dyoung 		break;
   3417  1.60.18.1      phil 	case IEEE80211_IOC_HTCOMPAT:
   3418  1.60.18.1      phil 		if (ireq->i_val) {
   3419  1.60.18.1      phil 			if ((vap->iv_flags_ht & IEEE80211_FHT_HT) == 0)
   3420  1.60.18.1      phil 				return EOPNOTSUPP;
   3421  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_HTCOMPAT;
   3422  1.60.18.1      phil 		} else
   3423  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_HTCOMPAT;
   3424  1.60.18.1      phil 		/* NB: reset only if we're operating on an 11n channel */
   3425  1.60.18.1      phil 		if (isvapht(vap))
   3426  1.60.18.1      phil 			error = ERESTART;
   3427       1.19    dyoung 		break;
   3428  1.60.18.1      phil 	case IEEE80211_IOC_DWDS:
   3429  1.60.18.1      phil 		if (ireq->i_val) {
   3430  1.60.18.1      phil 			/* NB: DWDS only makes sense for WDS-capable devices */
   3431  1.60.18.1      phil 			if ((ic->ic_caps & IEEE80211_C_WDS) == 0)
   3432  1.60.18.1      phil 				return EOPNOTSUPP;
   3433  1.60.18.1      phil 			/* NB: DWDS is used only with ap+sta vaps */
   3434  1.60.18.1      phil 			if (vap->iv_opmode != IEEE80211_M_HOSTAP &&
   3435  1.60.18.1      phil 			    vap->iv_opmode != IEEE80211_M_STA)
   3436  1.60.18.1      phil 				return EINVAL;
   3437  1.60.18.1      phil 			vap->iv_flags |= IEEE80211_F_DWDS;
   3438  1.60.18.1      phil 			if (vap->iv_opmode == IEEE80211_M_STA)
   3439  1.60.18.1      phil 				vap->iv_flags_ext |= IEEE80211_FEXT_4ADDR;
   3440  1.60.18.1      phil 		} else {
   3441  1.60.18.1      phil 			vap->iv_flags &= ~IEEE80211_F_DWDS;
   3442  1.60.18.1      phil 			if (vap->iv_opmode == IEEE80211_M_STA)
   3443  1.60.18.1      phil 				vap->iv_flags_ext &= ~IEEE80211_FEXT_4ADDR;
   3444  1.60.18.1      phil 		}
   3445       1.19    dyoung 		break;
   3446  1.60.18.1      phil 	case IEEE80211_IOC_INACTIVITY:
   3447  1.60.18.1      phil 		if (ireq->i_val)
   3448  1.60.18.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_INACT;
   3449  1.60.18.1      phil 		else
   3450  1.60.18.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_INACT;
   3451       1.19    dyoung 		break;
   3452  1.60.18.1      phil 	case IEEE80211_IOC_APPIE:
   3453  1.60.18.1      phil 		error = ieee80211_ioctl_setappie(vap, ireq);
   3454  1.60.18.1      phil 		break;
   3455  1.60.18.1      phil 	case IEEE80211_IOC_WPS:
   3456  1.60.18.1      phil 		if (ireq->i_val) {
   3457  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
   3458  1.60.18.1      phil 				return EOPNOTSUPP;
   3459  1.60.18.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_WPS;
   3460       1.19    dyoung 		} else
   3461  1.60.18.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_WPS;
   3462       1.19    dyoung 		break;
   3463  1.60.18.1      phil 	case IEEE80211_IOC_TSN:
   3464  1.60.18.1      phil 		if (ireq->i_val) {
   3465  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_WPA) == 0)
   3466  1.60.18.1      phil 				return EOPNOTSUPP;
   3467  1.60.18.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_TSN;
   3468       1.19    dyoung 		} else
   3469  1.60.18.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_TSN;
   3470       1.19    dyoung 		break;
   3471  1.60.18.1      phil 	case IEEE80211_IOC_CHANSWITCH:
   3472  1.60.18.1      phil 		error = ieee80211_ioctl_chanswitch(vap, ireq);
   3473  1.60.18.1      phil 		break;
   3474  1.60.18.1      phil 	case IEEE80211_IOC_DFS:
   3475  1.60.18.1      phil 		if (ireq->i_val) {
   3476  1.60.18.1      phil 			if ((vap->iv_caps & IEEE80211_C_DFS) == 0)
   3477  1.60.18.1      phil 				return EOPNOTSUPP;
   3478  1.60.18.1      phil 			/* NB: DFS requires 11h support */
   3479  1.60.18.1      phil 			if ((vap->iv_flags & IEEE80211_F_DOTH) == 0)
   3480  1.60.18.1      phil 				return EINVAL;
   3481  1.60.18.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_DFS;
   3482  1.60.18.1      phil 		} else
   3483  1.60.18.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DFS;
   3484  1.60.18.1      phil 		break;
   3485  1.60.18.1      phil 	case IEEE80211_IOC_DOTD:
   3486       1.21    dyoung 		if (ireq->i_val)
   3487  1.60.18.1      phil 			vap->iv_flags_ext |= IEEE80211_FEXT_DOTD;
   3488       1.21    dyoung 		else
   3489  1.60.18.1      phil 			vap->iv_flags_ext &= ~IEEE80211_FEXT_DOTD;
   3490  1.60.18.1      phil 		if (vap->iv_opmode == IEEE80211_M_STA)
   3491       1.21    dyoung 			error = ENETRESET;
   3492       1.21    dyoung 		break;
   3493  1.60.18.1      phil 	case IEEE80211_IOC_HTPROTMODE:
   3494  1.60.18.1      phil 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
   3495       1.26     skrll 			return EINVAL;
   3496  1.60.18.1      phil 		ic->ic_htprotmode = ireq->i_val ?
   3497  1.60.18.1      phil 		    IEEE80211_PROT_RTSCTS : IEEE80211_PROT_NONE;
   3498  1.60.18.1      phil 		/* NB: if not operating in 11n this can wait */
   3499  1.60.18.1      phil 		if (isvapht(vap))
   3500  1.60.18.1      phil 			error = ERESTART;
   3501  1.60.18.1      phil 		break;
   3502  1.60.18.1      phil 	case IEEE80211_IOC_STA_VLAN:
   3503  1.60.18.1      phil 		error = ieee80211_ioctl_setstavlan(vap, ireq);
   3504  1.60.18.1      phil 		break;
   3505  1.60.18.1      phil 	case IEEE80211_IOC_SMPS:
   3506  1.60.18.1      phil 		if ((ireq->i_val &~ IEEE80211_HTCAP_SMPS) != 0 ||
   3507  1.60.18.1      phil 		    ireq->i_val == 0x0008)	/* value of 2 is reserved */
   3508  1.60.18.1      phil 			return EINVAL;
   3509  1.60.18.1      phil 		if (ireq->i_val != IEEE80211_HTCAP_SMPS_OFF &&
   3510  1.60.18.1      phil 		    (vap->iv_htcaps & IEEE80211_HTC_SMPS) == 0)
   3511  1.60.18.1      phil 			return EOPNOTSUPP;
   3512  1.60.18.1      phil 		vap->iv_htcaps = (vap->iv_htcaps &~ IEEE80211_HTCAP_SMPS) |
   3513  1.60.18.1      phil 			ireq->i_val;
   3514  1.60.18.1      phil 		/* NB: if not operating in 11n this can wait */
   3515  1.60.18.1      phil 		if (isvapht(vap))
   3516  1.60.18.1      phil 			error = ERESTART;
   3517  1.60.18.1      phil 		break;
   3518  1.60.18.1      phil 	case IEEE80211_IOC_RIFS:
   3519  1.60.18.1      phil 		if (ireq->i_val != 0) {
   3520  1.60.18.1      phil 			if ((vap->iv_htcaps & IEEE80211_HTC_RIFS) == 0)
   3521  1.60.18.1      phil 				return EOPNOTSUPP;
   3522  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_RIFS;
   3523  1.60.18.1      phil 		} else
   3524  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_RIFS;
   3525  1.60.18.1      phil 		/* NB: if not operating in 11n this can wait */
   3526  1.60.18.1      phil 		if (isvapht(vap))
   3527  1.60.18.1      phil 			error = ERESTART;
   3528  1.60.18.1      phil 		break;
   3529  1.60.18.1      phil 	case IEEE80211_IOC_STBC:
   3530  1.60.18.1      phil 		/* Check if we can do STBC TX/RX before changing the setting */
   3531  1.60.18.1      phil 		if ((ireq->i_val & 1) &&
   3532  1.60.18.1      phil 		    ((vap->iv_htcaps & IEEE80211_HTCAP_TXSTBC) == 0))
   3533  1.60.18.1      phil 			return EOPNOTSUPP;
   3534  1.60.18.1      phil 		if ((ireq->i_val & 2) &&
   3535  1.60.18.1      phil 		    ((vap->iv_htcaps & IEEE80211_HTCAP_RXSTBC) == 0))
   3536  1.60.18.1      phil 			return EOPNOTSUPP;
   3537  1.60.18.1      phil 
   3538  1.60.18.1      phil 		/* TX */
   3539  1.60.18.1      phil 		if (ireq->i_val & 1)
   3540  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_TX;
   3541  1.60.18.1      phil 		else
   3542  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_TX;
   3543  1.60.18.1      phil 
   3544  1.60.18.1      phil 		/* RX */
   3545  1.60.18.1      phil 		if (ireq->i_val & 2)
   3546  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_STBC_RX;
   3547  1.60.18.1      phil 		else
   3548  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_STBC_RX;
   3549  1.60.18.1      phil 
   3550  1.60.18.1      phil 		/* NB: reset only if we're operating on an 11n channel */
   3551  1.60.18.1      phil 		if (isvapht(vap))
   3552  1.60.18.1      phil 			error = ERESTART;
   3553  1.60.18.1      phil 		break;
   3554  1.60.18.1      phil 	case IEEE80211_IOC_LDPC:
   3555  1.60.18.1      phil 		/* Check if we can do LDPC TX/RX before changing the setting */
   3556  1.60.18.1      phil 		if ((ireq->i_val & 1) &&
   3557  1.60.18.1      phil 		    (vap->iv_htcaps & IEEE80211_HTC_TXLDPC) == 0)
   3558  1.60.18.1      phil 			return EOPNOTSUPP;
   3559  1.60.18.1      phil 		if ((ireq->i_val & 2) &&
   3560  1.60.18.1      phil 		    (vap->iv_htcaps & IEEE80211_HTCAP_LDPC) == 0)
   3561  1.60.18.1      phil 			return EOPNOTSUPP;
   3562  1.60.18.1      phil 
   3563  1.60.18.1      phil 		/* TX */
   3564  1.60.18.1      phil 		if (ireq->i_val & 1)
   3565  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_TX;
   3566  1.60.18.1      phil 		else
   3567  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_TX;
   3568  1.60.18.1      phil 
   3569  1.60.18.1      phil 		/* RX */
   3570  1.60.18.1      phil 		if (ireq->i_val & 2)
   3571  1.60.18.1      phil 			vap->iv_flags_ht |= IEEE80211_FHT_LDPC_RX;
   3572  1.60.18.1      phil 		else
   3573  1.60.18.1      phil 			vap->iv_flags_ht &= ~IEEE80211_FHT_LDPC_RX;
   3574  1.60.18.1      phil 
   3575  1.60.18.1      phil 		/* NB: reset only if we're operating on an 11n channel */
   3576  1.60.18.1      phil 		if (isvapht(vap))
   3577  1.60.18.1      phil 			error = ERESTART;
   3578  1.60.18.1      phil 		break;
   3579  1.60.18.1      phil 
   3580  1.60.18.1      phil 	/* VHT */
   3581  1.60.18.1      phil 	case IEEE80211_IOC_VHTCONF:
   3582  1.60.18.1      phil 		if (ireq->i_val & 1)
   3583  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_VHT);
   3584  1.60.18.1      phil 		else
   3585  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_VHT);
   3586  1.60.18.1      phil 
   3587  1.60.18.1      phil 		if (ireq->i_val & 2)
   3588  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT40);
   3589  1.60.18.1      phil 		else
   3590  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT40);
   3591  1.60.18.1      phil 
   3592  1.60.18.1      phil 		if (ireq->i_val & 4)
   3593  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80);
   3594  1.60.18.1      phil 		else
   3595  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80);
   3596  1.60.18.1      phil 
   3597  1.60.18.1      phil 		if (ireq->i_val & 8)
   3598  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT80P80);
   3599  1.60.18.1      phil 		else
   3600  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT80P80);
   3601  1.60.18.1      phil 
   3602  1.60.18.1      phil 		if (ireq->i_val & 16)
   3603  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, IEEE80211_FVHT_USEVHT160);
   3604  1.60.18.1      phil 		else
   3605  1.60.18.1      phil 			ieee80211_syncflag_vht(vap, -IEEE80211_FVHT_USEVHT160);
   3606  1.60.18.1      phil 
   3607  1.60.18.1      phil 		error = ENETRESET;
   3608       1.26     skrll 		break;
   3609  1.60.18.1      phil 
   3610       1.19    dyoung 	default:
   3611  1.60.18.1      phil 		error = ieee80211_ioctl_setdefault(vap, ireq);
   3612       1.19    dyoung 		break;
   3613       1.19    dyoung 	}
   3614  1.60.18.1      phil 	/*
   3615  1.60.18.1      phil 	 * The convention is that ENETRESET means an operation
   3616  1.60.18.1      phil 	 * requires a complete re-initialization of the device (e.g.
   3617  1.60.18.1      phil 	 * changing something that affects the association state).
   3618  1.60.18.1      phil 	 * ERESTART means the request may be handled with only a
   3619  1.60.18.1      phil 	 * reload of the hardware state.  We hand ERESTART requests
   3620  1.60.18.1      phil 	 * to the iv_reset callback so the driver can decide.  If
   3621  1.60.18.1      phil 	 * a device does not fillin iv_reset then it defaults to one
   3622  1.60.18.1      phil 	 * that returns ENETRESET.  Otherwise a driver may return
   3623  1.60.18.1      phil 	 * ENETRESET (in which case a full reset will be done) or
   3624  1.60.18.1      phil 	 * 0 to mean there's no need to do anything (e.g. when the
   3625  1.60.18.1      phil 	 * change has no effect on the driver/device).
   3626  1.60.18.1      phil 	 */
   3627  1.60.18.1      phil 	if (error == ERESTART)
   3628  1.60.18.1      phil 		error = IFNET_IS_UP_RUNNING(vap->iv_ifp) ?
   3629  1.60.18.1      phil 		    vap->iv_reset(vap, ireq->i_type) : 0;
   3630  1.60.18.1      phil 	if (error == ENETRESET) {
   3631  1.60.18.1      phil 		/* XXX need to re-think AUTO handling */
   3632  1.60.18.1      phil 		if (IS_UP_AUTO(vap))
   3633  1.60.18.3      phil #if __FreeBSD__
   3634  1.60.18.1      phil 			ieee80211_init(vap);
   3635  1.60.18.3      phil #elif __NetBSD__
   3636  1.60.18.3      phil 			/* arg is a vap in 802.11 code */
   3637  1.60.18.3      phil 			ieee80211_init((struct ifnet *)vap);
   3638  1.60.18.3      phil #endif
   3639       1.19    dyoung 		error = 0;
   3640  1.60.18.1      phil 	}
   3641       1.19    dyoung 	return error;
   3642       1.19    dyoung }
   3643       1.19    dyoung 
   3644       1.19    dyoung int
   3645  1.60.18.1      phil ieee80211_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
   3646       1.19    dyoung {
   3647  1.60.18.1      phil 	struct ieee80211vap *vap = ifp->if_softc;
   3648  1.60.18.1      phil 	struct ieee80211com *ic = vap->iv_ic;
   3649  1.60.18.1      phil 	int error = 0, wait = 0;
   3650       1.19    dyoung 	struct ifreq *ifr;
   3651       1.19    dyoung 	struct ifaddr *ifa;			/* XXX */
   3652  1.60.18.4      phil #if __NetBSD__
   3653  1.60.18.4      phil 	struct ieee80211_nwid nwid;
   3654  1.60.18.4      phil 	//	struct ieee80211_nwkey *nwkey;
   3655  1.60.18.7      phil 	struct ieee80211_power *power;
   3656  1.60.18.7      phil 	struct ieee80211chanreq *chanreq;
   3657  1.60.18.6      phil 	struct ieee80211_bssid *bssid;
   3658  1.60.18.7      phil 	struct ieee80211_channel *chan;
   3659  1.60.18.7      phil 	struct ieee80211req ireq;
   3660  1.60.18.4      phil 
   3661  1.60.18.4      phil 	ifr = (struct ifreq *)data;
   3662  1.60.18.4      phil #endif
   3663  1.60.18.4      phil 
   3664       1.19    dyoung 	switch (cmd) {
   3665  1.60.18.1      phil 	case SIOCSIFFLAGS:
   3666  1.60.18.1      phil 		IEEE80211_LOCK(ic);
   3667  1.60.18.1      phil 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_PROMISC) {
   3668  1.60.18.4      phil 			printf ("    promisc mode\n");
   3669  1.60.18.1      phil 			/*
   3670  1.60.18.1      phil 			 * Enable promiscuous mode when:
   3671  1.60.18.1      phil 			 * 1. Interface is not a member of bridge, or
   3672  1.60.18.1      phil 			 * 2. Requested by user, or
   3673  1.60.18.1      phil 			 * 3. In monitor (or adhoc-demo) mode.
   3674  1.60.18.1      phil 			 */
   3675  1.60.18.1      phil 			if (ifp->if_bridge == NULL ||
   3676  1.60.18.1      phil 			    (ifp->if_flags & IFF_PPROMISC) != 0 ||
   3677  1.60.18.1      phil 			    vap->iv_opmode == IEEE80211_M_MONITOR ||
   3678  1.60.18.1      phil 			    (vap->iv_opmode == IEEE80211_M_AHDEMO &&
   3679  1.60.18.1      phil 			    (vap->iv_caps & IEEE80211_C_TDMA) == 0)) {
   3680  1.60.18.1      phil 				ieee80211_promisc(vap,
   3681  1.60.18.1      phil 				    ifp->if_flags & IFF_PROMISC);
   3682  1.60.18.1      phil 				vap->iv_ifflags ^= IFF_PROMISC;
   3683  1.60.18.1      phil 			}
   3684  1.60.18.1      phil 		}
   3685  1.60.18.1      phil 		if ((ifp->if_flags ^ vap->iv_ifflags) & IFF_ALLMULTI) {
   3686  1.60.18.4      phil 			printf ("    allmulti\n");
   3687  1.60.18.1      phil 			ieee80211_allmulti(vap, ifp->if_flags & IFF_ALLMULTI);
   3688  1.60.18.1      phil 			vap->iv_ifflags ^= IFF_ALLMULTI;
   3689  1.60.18.1      phil 		}
   3690  1.60.18.1      phil 		if (ifp->if_flags & IFF_UP) {
   3691  1.60.18.4      phil 			printf ("    up flag\n");
   3692  1.60.18.1      phil 			/*
   3693  1.60.18.1      phil 			 * Bring ourself up unless we're already operational.
   3694  1.60.18.1      phil 			 * If we're the first vap and the parent is not up
   3695  1.60.18.1      phil 			 * then it will automatically be brought up as a
   3696  1.60.18.1      phil 			 * side-effect of bringing ourself up.
   3697  1.60.18.1      phil 			 */
   3698  1.60.18.1      phil 			if (vap->iv_state == IEEE80211_S_INIT) {
   3699  1.60.18.1      phil 				if (ic->ic_nrunning == 0)
   3700  1.60.18.1      phil 					wait = 1;
   3701  1.60.18.4      phil 				printf ("Should call start_locked ...\n");
   3702  1.60.18.4      phil 				wait = 0;
   3703  1.60.18.4      phil 				// ieee80211_start_locked(vap);
   3704  1.60.18.1      phil 			}
   3705  1.60.18.2      phil #if __FreeBSD__
   3706  1.60.18.1      phil 		} else if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
   3707  1.60.18.2      phil #elif __NetBSD__
   3708  1.60.18.2      phil 		} else if (ifp->if_flags & IFF_RUNNING) {
   3709  1.60.18.2      phil #endif
   3710  1.60.18.1      phil 			/*
   3711  1.60.18.1      phil 			 * Stop ourself.  If we are the last vap to be
   3712  1.60.18.1      phil 			 * marked down the parent will also be taken down.
   3713  1.60.18.1      phil 			 */
   3714  1.60.18.1      phil 			if (ic->ic_nrunning == 1)
   3715  1.60.18.1      phil 				wait = 1;
   3716  1.60.18.1      phil 			ieee80211_stop_locked(vap);
   3717  1.60.18.1      phil 		}
   3718  1.60.18.1      phil 		IEEE80211_UNLOCK(ic);
   3719  1.60.18.1      phil 		/* Wait for parent ioctl handler if it was queued */
   3720  1.60.18.1      phil 		if (wait) {
   3721  1.60.18.1      phil 			ieee80211_waitfor_parent(ic);
   3722  1.60.18.1      phil 
   3723  1.60.18.1      phil 			/*
   3724  1.60.18.1      phil 			 * Check if the MAC address was changed
   3725  1.60.18.1      phil 			 * via SIOCSIFLLADDR ioctl.
   3726  1.60.18.1      phil 			 */
   3727  1.60.18.1      phil 			if_addr_rlock(ifp);
   3728  1.60.18.1      phil 			if ((ifp->if_flags & IFF_UP) == 0 &&
   3729  1.60.18.7      phil 			    !IEEE80211_ADDR_EQ(vap->iv_myaddr, IF_LLADDR(ifp))) {
   3730  1.60.18.1      phil 				IEEE80211_ADDR_COPY(vap->iv_myaddr,
   3731  1.60.18.1      phil 				    IF_LLADDR(ifp));
   3732  1.60.18.7      phil 				printf ("vap->iv_myaddr changed in ioctl to %s\n",
   3733  1.60.18.7      phil 					ether_sprintf(vap->iv_myaddr));
   3734  1.60.18.7      phil 			}
   3735  1.60.18.7      phil 
   3736  1.60.18.1      phil 			if_addr_runlock(ifp);
   3737  1.60.18.1      phil 		}
   3738  1.60.18.1      phil 		break;
   3739  1.60.18.1      phil 	case SIOCADDMULTI:
   3740  1.60.18.1      phil 	case SIOCDELMULTI:
   3741  1.60.18.1      phil 		ieee80211_runtask(ic, &ic->ic_mcast_task);
   3742  1.60.18.1      phil 		break;
   3743  1.60.18.7      phil #ifdef OSIOCSIFMEDIA
   3744  1.60.18.7      phil 	case OSIOCSIFMEDIA:
   3745  1.60.18.7      phil #endif
   3746       1.19    dyoung 	case SIOCSIFMEDIA:
   3747       1.19    dyoung 	case SIOCGIFMEDIA:
   3748  1.60.18.1      phil 		ifr = (struct ifreq *)data;
   3749  1.60.18.1      phil 		error = ifmedia_ioctl(ifp, ifr, &vap->iv_media, cmd);
   3750       1.19    dyoung 		break;
   3751       1.19    dyoung 	case SIOCG80211:
   3752  1.60.18.1      phil 		error = ieee80211_ioctl_get80211(vap, cmd,
   3753       1.19    dyoung 				(struct ieee80211req *) data);
   3754       1.19    dyoung 		break;
   3755       1.19    dyoung 	case SIOCS80211:
   3756  1.60.18.1      phil 		error = priv_check(curthread, PRIV_NET80211_MANAGE);
   3757       1.19    dyoung 		if (error == 0)
   3758  1.60.18.1      phil 			error = ieee80211_ioctl_set80211(vap, cmd,
   3759       1.19    dyoung 					(struct ieee80211req *) data);
   3760        1.1    dyoung 		break;
   3761  1.60.18.7      phil #ifdef SIOCG80211ZSTATS
   3762  1.60.18.7      phil 	case SIOCG80211ZSTATS:
   3763  1.60.18.7      phil #endif
   3764       1.19    dyoung 	case SIOCG80211STATS:
   3765       1.19    dyoung 		ifr = (struct ifreq *)data;
   3766  1.60.18.2      phil #if __FreeBSD__
   3767  1.60.18.1      phil 		copyout(&vap->iv_stats, ifr_data_get_ptr(ifr),
   3768  1.60.18.1      phil 		    sizeof (vap->iv_stats));
   3769  1.60.18.2      phil #elif__NetBSD__
   3770  1.60.18.7      phil 		copyout(&vap->iv_stats, ifr->ifr_buf,
   3771  1.60.18.7      phil 			sizeof (vap->iv_stats) <= ifr->ifr_buflen
   3772  1.60.18.7      phil 			? sizeof (vap->iv_stats) : ifr->ifr_buflen);
   3773  1.60.18.2      phil #endif
   3774       1.19    dyoung 		break;
   3775       1.19    dyoung 	case SIOCSIFMTU:
   3776       1.19    dyoung 		ifr = (struct ifreq *)data;
   3777       1.19    dyoung 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
   3778       1.19    dyoung 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
   3779       1.19    dyoung 			error = EINVAL;
   3780       1.19    dyoung 		else
   3781       1.19    dyoung 			ifp->if_mtu = ifr->ifr_mtu;
   3782       1.19    dyoung 		break;
   3783  1.60.18.1      phil 	case SIOCSIFADDR:
   3784  1.60.18.1      phil 		/*
   3785  1.60.18.1      phil 		 * XXX Handle this directly so we can suppress if_init calls.
   3786  1.60.18.1      phil 		 * XXX This should be done in ether_ioctl but for the moment
   3787  1.60.18.1      phil 		 * XXX there are too many other parts of the system that
   3788  1.60.18.1      phil 		 * XXX set IFF_UP and so suppress if_init being called when
   3789  1.60.18.1      phil 		 * XXX it should be.
   3790  1.60.18.1      phil 		 */
   3791  1.60.18.1      phil 		ifa = (struct ifaddr *) data;
   3792  1.60.18.1      phil 		switch (ifa->ifa_addr->sa_family) {
   3793  1.60.18.1      phil #ifdef INET
   3794  1.60.18.1      phil 		case AF_INET:
   3795  1.60.18.1      phil 			if ((ifp->if_flags & IFF_UP) == 0) {
   3796  1.60.18.1      phil 				ifp->if_flags |= IFF_UP;
   3797  1.60.18.6      phil #if __FreeBSD__
   3798  1.60.18.1      phil 				ifp->if_init(ifp->if_softc);
   3799  1.60.18.6      phil #elif __NetBSD__
   3800  1.60.18.6      phil 				ifp->if_init(ifp);
   3801  1.60.18.6      phil #endif
   3802        1.2    dyoung 			}
   3803  1.60.18.1      phil 			arp_ifinit(ifp, ifa);
   3804        1.2    dyoung 			break;
   3805  1.60.18.1      phil #endif
   3806        1.2    dyoung 		default:
   3807  1.60.18.1      phil 			if ((ifp->if_flags & IFF_UP) == 0) {
   3808  1.60.18.1      phil 				ifp->if_flags |= IFF_UP;
   3809  1.60.18.6      phil #if __FreeBSD__
   3810  1.60.18.1      phil 				ifp->if_init(ifp->if_softc);
   3811  1.60.18.6      phil #elif __NetBSD__
   3812  1.60.18.6      phil 				ifp->if_init(ifp);
   3813  1.60.18.6      phil #endif
   3814        1.2    dyoung 			}
   3815        1.2    dyoung 			break;
   3816        1.2    dyoung 		}
   3817        1.2    dyoung 		break;
   3818  1.60.18.4      phil #if __NetBSD__
   3819  1.60.18.4      phil 	case SIOCS80211NWID:
   3820  1.60.18.4      phil 		if ((error = copyin(ifr->ifr_data, &nwid, sizeof(nwid))) != 0)
   3821  1.60.18.4      phil 			break;
   3822  1.60.18.4      phil 		if (nwid.i_len > IEEE80211_NWID_LEN) {
   3823  1.60.18.4      phil 			error = EINVAL;
   3824  1.60.18.4      phil 			break;
   3825  1.60.18.4      phil 		}
   3826  1.60.18.4      phil 		memset(vap->iv_des_ssid, 0, IEEE80211_NWID_LEN);
   3827  1.60.18.4      phil 		vap->iv_des_ssid[0].len = nwid.i_len;
   3828  1.60.18.4      phil 		memcpy(vap->iv_des_ssid[0].ssid, nwid.i_nwid, nwid.i_len);
   3829  1.60.18.4      phil 		error = ENETRESET;
   3830  1.60.18.4      phil 		break;
   3831  1.60.18.4      phil 	case SIOCG80211NWID:
   3832  1.60.18.4      phil 		memset(&nwid, 0, sizeof(nwid));
   3833  1.60.18.4      phil 		switch (vap->iv_state) {
   3834  1.60.18.4      phil 		case IEEE80211_S_INIT:
   3835  1.60.18.4      phil 		case IEEE80211_S_SCAN:
   3836  1.60.18.4      phil 			nwid.i_len  = vap->iv_des_ssid[0].len;
   3837  1.60.18.4      phil 			memcpy(nwid.i_nwid, vap->iv_des_ssid[0].ssid,
   3838  1.60.18.4      phil 			    nwid.i_len);
   3839  1.60.18.4      phil 			break;
   3840  1.60.18.4      phil 		default:
   3841  1.60.18.4      phil 			nwid.i_len = vap->iv_bss->ni_esslen;
   3842  1.60.18.4      phil 			memcpy(nwid.i_nwid, vap->iv_bss->ni_essid, nwid.i_len);
   3843  1.60.18.4      phil 			break;
   3844  1.60.18.4      phil 		}
   3845  1.60.18.4      phil 		error = copyout(&nwid, ifr->ifr_data, sizeof(nwid));
   3846  1.60.18.4      phil 		break;
   3847  1.60.18.4      phil 	case SIOCS80211NWKEY:
   3848  1.60.18.4      phil 	case SIOCG80211NWKEY:
   3849  1.60.18.6      phil 		printf ("NetBSD NWKEY ioctl\n"); // NNN
   3850  1.60.18.4      phil 		error = ENOTTY;
   3851  1.60.18.4      phil 	        break;
   3852  1.60.18.4      phil 	case SIOCS80211POWER:
   3853  1.60.18.7      phil 		power = (struct ieee80211_power *)data;
   3854  1.60.18.7      phil 		ic->ic_lintval = power->i_maxsleep;
   3855  1.60.18.7      phil 		if (power->i_enabled != 0) {
   3856  1.60.18.7      phil 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
   3857  1.60.18.7      phil 				error = EINVAL;
   3858  1.60.18.7      phil 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   3859  1.60.18.7      phil 				ic->ic_flags |= IEEE80211_F_PMGTON;
   3860  1.60.18.7      phil 				error = ENETRESET;
   3861  1.60.18.7      phil 			}
   3862  1.60.18.7      phil 		} else {
   3863  1.60.18.7      phil 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   3864  1.60.18.7      phil 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   3865  1.60.18.7      phil 				error = ENETRESET;
   3866  1.60.18.7      phil 			}
   3867  1.60.18.7      phil 		}
   3868  1.60.18.7      phil 		break;
   3869  1.60.18.7      phil 	case SIOCG80211POWER:
   3870  1.60.18.7      phil 		power = (struct ieee80211_power *)data;
   3871  1.60.18.7      phil 		power->i_enabled = (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0;
   3872  1.60.18.7      phil 		power->i_maxsleep = ic->ic_lintval;
   3873  1.60.18.4      phil 		break;
   3874  1.60.18.6      phil 	case SIOCS80211BSSID:
   3875  1.60.18.6      phil 		bssid = (struct ieee80211_bssid *)data;
   3876  1.60.18.6      phil 		IEEE80211_ADDR_COPY(vap->iv_des_bssid, bssid->i_bssid);
   3877  1.60.18.6      phil 		if (IEEE80211_ADDR_EQ(vap->iv_des_bssid, zerobssid))
   3878  1.60.18.6      phil 			vap->iv_flags &= ~IEEE80211_F_DESBSSID;
   3879  1.60.18.6      phil 		else
   3880  1.60.18.6      phil 			vap->iv_flags |= IEEE80211_F_DESBSSID;
   3881  1.60.18.6      phil 		error = ENETRESET;
   3882  1.60.18.6      phil 		break;
   3883  1.60.18.6      phil 	case SIOCG80211BSSID:
   3884  1.60.18.6      phil 		bssid = (struct ieee80211_bssid *)data;
   3885  1.60.18.6      phil 		switch (vap->iv_state) {
   3886  1.60.18.6      phil 		case IEEE80211_S_RUN:
   3887  1.60.18.6      phil 		case IEEE80211_S_SLEEP:
   3888  1.60.18.6      phil 			IEEE80211_ADDR_COPY(bssid->i_bssid,
   3889  1.60.18.6      phil 			    vap->iv_opmode == IEEE80211_M_WDS ?
   3890  1.60.18.6      phil 			    vap->iv_bss->ni_macaddr : vap->iv_bss->ni_bssid);
   3891  1.60.18.6      phil 			break;
   3892  1.60.18.6      phil 		case IEEE80211_S_INIT:
   3893  1.60.18.6      phil 		case IEEE80211_S_SCAN:
   3894  1.60.18.6      phil 			if (vap->iv_opmode == IEEE80211_M_HOSTAP)
   3895  1.60.18.6      phil 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   3896  1.60.18.6      phil 				    vap->iv_myaddr);
   3897  1.60.18.6      phil 			else if (vap->iv_flags & IEEE80211_F_DESBSSID)
   3898  1.60.18.6      phil 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   3899  1.60.18.6      phil 				    vap->iv_des_bssid);
   3900  1.60.18.6      phil 			else
   3901  1.60.18.6      phil 				memset(bssid->i_bssid, 0, IEEE80211_ADDR_LEN);
   3902  1.60.18.6      phil 
   3903  1.60.18.6      phil 			break;
   3904  1.60.18.6      phil 		default:
   3905  1.60.18.6      phil 			memset(bssid->i_bssid, 0, IEEE80211_ADDR_LEN);
   3906  1.60.18.6      phil 		}
   3907  1.60.18.6      phil 		break;
   3908  1.60.18.7      phil 	case SIOCS80211CHANNEL:
   3909  1.60.18.7      phil 		/* Use deprecated ieee80211_ioctl_setchannel code. NNN? */
   3910  1.60.18.7      phil 		chanreq = (struct ieee80211chanreq *)data;
   3911  1.60.18.7      phil 		ireq.i_val = chanreq->i_channel;
   3912  1.60.18.7      phil 		error = ieee80211_ioctl_setchannel(vap, &ireq);
   3913  1.60.18.7      phil 		break;
   3914  1.60.18.7      phil 	case SIOCG80211CHANNEL:
   3915  1.60.18.7      phil 		chanreq = (struct ieee80211chanreq *)data;
   3916  1.60.18.7      phil 		switch (vap->iv_state) {
   3917  1.60.18.7      phil 		case IEEE80211_S_INIT:
   3918  1.60.18.7      phil 		case IEEE80211_S_SCAN:
   3919  1.60.18.7      phil 			if (vap->iv_opmode == IEEE80211_M_STA)
   3920  1.60.18.7      phil 				chan = vap->iv_des_chan;
   3921  1.60.18.7      phil 			else
   3922  1.60.18.7      phil 				chan = ic->ic_bsschan;  // NNN ibss chan?
   3923  1.60.18.7      phil 			break;
   3924  1.60.18.7      phil 		default:
   3925  1.60.18.7      phil 			chan = ic->ic_curchan;
   3926  1.60.18.7      phil 			break;
   3927  1.60.18.7      phil 		}
   3928  1.60.18.7      phil 		chanreq->i_channel = ieee80211_chan2ieee(ic, chan);
   3929  1.60.18.7      phil 		break;
   3930  1.60.18.4      phil #endif
   3931        1.2    dyoung 	default:
   3932  1.60.18.1      phil 		/*
   3933  1.60.18.1      phil 		 * Pass unknown ioctls first to the driver, and if it
   3934  1.60.18.1      phil 		 * returns ENOTTY, then to the generic Ethernet handler.
   3935  1.60.18.1      phil 		 */
   3936  1.60.18.1      phil 		if (ic->ic_ioctl != NULL &&
   3937  1.60.18.1      phil 		    (error = ic->ic_ioctl(ic, cmd, data)) != ENOTTY)
   3938  1.60.18.1      phil 			break;
   3939        1.2    dyoung 		error = ether_ioctl(ifp, cmd, data);
   3940  1.60.18.7      phil 		if (error == ENOTTY) {
   3941  1.60.18.7      phil 			printf ("ieee80211_ioctl: cmd is 0x%lx. ('%c', %ld)\n",
   3942  1.60.18.7      phil 				cmd, (char) ((cmd>>8) & 0xff), cmd & 0xff );
   3943  1.60.18.4      phil 			printf ("Unknown 802.11 IOCTL.\n"); /* NNN */
   3944  1.60.18.7      phil 		}
   3945        1.2    dyoung 		break;
   3946        1.2    dyoung 	}
   3947  1.60.18.1      phil 	return (error);
   3948        1.2    dyoung }
   3949