Home | History | Annotate | Line # | Download | only in net80211
ieee80211_ioctl.c revision 1.16.2.7
      1  1.16.2.7  skrll /*	$NetBSD: ieee80211_ioctl.c,v 1.16.2.7 2005/11/10 14:10:51 skrll Exp $	*/
      2  1.16.2.2  skrll /*-
      3  1.16.2.2  skrll  * Copyright (c) 2001 Atsushi Onoe
      4  1.16.2.7  skrll  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
      5  1.16.2.2  skrll  * All rights reserved.
      6  1.16.2.2  skrll  *
      7  1.16.2.2  skrll  * Redistribution and use in source and binary forms, with or without
      8  1.16.2.2  skrll  * modification, are permitted provided that the following conditions
      9  1.16.2.2  skrll  * are met:
     10  1.16.2.2  skrll  * 1. Redistributions of source code must retain the above copyright
     11  1.16.2.2  skrll  *    notice, this list of conditions and the following disclaimer.
     12  1.16.2.2  skrll  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.16.2.2  skrll  *    notice, this list of conditions and the following disclaimer in the
     14  1.16.2.2  skrll  *    documentation and/or other materials provided with the distribution.
     15  1.16.2.2  skrll  * 3. The name of the author may not be used to endorse or promote products
     16  1.16.2.2  skrll  *    derived from this software without specific prior written permission.
     17  1.16.2.2  skrll  *
     18  1.16.2.2  skrll  * Alternatively, this software may be distributed under the terms of the
     19  1.16.2.2  skrll  * GNU General Public License ("GPL") version 2 as published by the Free
     20  1.16.2.2  skrll  * Software Foundation.
     21  1.16.2.2  skrll  *
     22  1.16.2.2  skrll  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.16.2.2  skrll  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.16.2.2  skrll  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.16.2.2  skrll  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.16.2.2  skrll  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.16.2.2  skrll  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.16.2.2  skrll  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.16.2.2  skrll  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.16.2.2  skrll  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.16.2.2  skrll  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.16.2.2  skrll  */
     33  1.16.2.2  skrll 
     34  1.16.2.2  skrll #include <sys/cdefs.h>
     35  1.16.2.2  skrll #ifdef __FreeBSD__
     36  1.16.2.2  skrll __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_ioctl.c,v 1.13 2004/03/30 22:57:57 sam Exp $");
     37  1.16.2.2  skrll #else
     38  1.16.2.7  skrll __KERNEL_RCSID(0, "$NetBSD: ieee80211_ioctl.c,v 1.16.2.7 2005/11/10 14:10:51 skrll Exp $");
     39  1.16.2.2  skrll #endif
     40  1.16.2.2  skrll 
     41  1.16.2.2  skrll /*
     42  1.16.2.2  skrll  * IEEE 802.11 ioctl support (FreeBSD-specific)
     43  1.16.2.2  skrll  */
     44  1.16.2.2  skrll 
     45  1.16.2.2  skrll #include "opt_inet.h"
     46  1.16.2.2  skrll 
     47  1.16.2.2  skrll #include <sys/endian.h>
     48  1.16.2.2  skrll #include <sys/param.h>
     49  1.16.2.2  skrll #include <sys/kernel.h>
     50  1.16.2.2  skrll #include <sys/socket.h>
     51  1.16.2.2  skrll #include <sys/sockio.h>
     52  1.16.2.2  skrll #include <sys/systm.h>
     53  1.16.2.2  skrll #include <sys/proc.h>
     54  1.16.2.7  skrll 
     55  1.16.2.2  skrll #include <net/if.h>
     56  1.16.2.2  skrll #include <net/if_arp.h>
     57  1.16.2.2  skrll #include <net/if_media.h>
     58  1.16.2.2  skrll #include <net/if_ether.h>
     59  1.16.2.2  skrll 
     60  1.16.2.2  skrll #ifdef INET
     61  1.16.2.2  skrll #include <netinet/in.h>
     62  1.16.2.2  skrll #include <netinet/if_inarp.h>
     63  1.16.2.2  skrll #endif
     64  1.16.2.2  skrll 
     65  1.16.2.2  skrll #include <net80211/ieee80211_var.h>
     66  1.16.2.2  skrll #include <net80211/ieee80211_ioctl.h>
     67  1.16.2.2  skrll 
     68  1.16.2.2  skrll #include <dev/ic/wi_ieee.h>
     69  1.16.2.7  skrll 
     70  1.16.2.7  skrll #define	IS_UP(_ic) \
     71  1.16.2.7  skrll 	(((_ic)->ic_ifp->if_flags & (IFF_RUNNING|IFF_UP)) == (IFF_RUNNING|IFF_UP))
     72  1.16.2.7  skrll #define	IS_UP_AUTO(_ic) \
     73  1.16.2.7  skrll 	(IS_UP(_ic) && (_ic)->ic_roaming == IEEE80211_ROAMING_AUTO)
     74  1.16.2.2  skrll 
     75  1.16.2.2  skrll /*
     76  1.16.2.2  skrll  * XXX
     77  1.16.2.2  skrll  * Wireless LAN specific configuration interface, which is compatible
     78  1.16.2.2  skrll  * with wicontrol(8).
     79  1.16.2.2  skrll  */
     80  1.16.2.2  skrll 
     81  1.16.2.7  skrll struct wi_read_ap_args {
     82  1.16.2.7  skrll 	int	i;		/* result count */
     83  1.16.2.7  skrll 	struct wi_apinfo *ap;	/* current entry in result buffer */
     84  1.16.2.7  skrll 	caddr_t	max;		/* result buffer bound */
     85  1.16.2.7  skrll };
     86  1.16.2.7  skrll 
     87  1.16.2.7  skrll static void
     88  1.16.2.7  skrll wi_read_ap_result(void *arg, struct ieee80211_node *ni)
     89  1.16.2.7  skrll {
     90  1.16.2.7  skrll 	struct ieee80211com *ic = ni->ni_ic;
     91  1.16.2.7  skrll 	struct wi_read_ap_args *sa = arg;
     92  1.16.2.7  skrll 	struct wi_apinfo *ap = sa->ap;
     93  1.16.2.7  skrll 	struct ieee80211_rateset *rs;
     94  1.16.2.7  skrll 	int j;
     95  1.16.2.7  skrll 
     96  1.16.2.7  skrll 	if ((caddr_t)(ap + 1) > sa->max)
     97  1.16.2.7  skrll 		return;
     98  1.16.2.7  skrll 	memset(ap, 0, sizeof(struct wi_apinfo));
     99  1.16.2.7  skrll 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
    100  1.16.2.7  skrll 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_macaddr);
    101  1.16.2.7  skrll 		ap->namelen = ic->ic_des_esslen;
    102  1.16.2.7  skrll 		if (ic->ic_des_esslen)
    103  1.16.2.7  skrll 			memcpy(ap->name, ic->ic_des_essid,
    104  1.16.2.7  skrll 			    ic->ic_des_esslen);
    105  1.16.2.7  skrll 	} else {
    106  1.16.2.7  skrll 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_bssid);
    107  1.16.2.7  skrll 		ap->namelen = ni->ni_esslen;
    108  1.16.2.7  skrll 		if (ni->ni_esslen)
    109  1.16.2.7  skrll 			memcpy(ap->name, ni->ni_essid,
    110  1.16.2.7  skrll 			    ni->ni_esslen);
    111  1.16.2.7  skrll 	}
    112  1.16.2.7  skrll 	ap->channel = ieee80211_chan2ieee(ic, ni->ni_chan);
    113  1.16.2.7  skrll 	ap->signal = ic->ic_node_getrssi(ni);
    114  1.16.2.7  skrll 	ap->capinfo = ni->ni_capinfo;
    115  1.16.2.7  skrll 	ap->interval = ni->ni_intval;
    116  1.16.2.7  skrll 	rs = &ni->ni_rates;
    117  1.16.2.7  skrll 	for (j = 0; j < rs->rs_nrates; j++) {
    118  1.16.2.7  skrll 		if (rs->rs_rates[j] & IEEE80211_RATE_BASIC) {
    119  1.16.2.7  skrll 			ap->rate = (rs->rs_rates[j] &
    120  1.16.2.7  skrll 			    IEEE80211_RATE_VAL) * 5; /* XXX */
    121  1.16.2.7  skrll 		}
    122  1.16.2.7  skrll 	}
    123  1.16.2.7  skrll 	sa->i++;
    124  1.16.2.7  skrll 	sa->ap++;
    125  1.16.2.7  skrll }
    126  1.16.2.7  skrll 
    127  1.16.2.7  skrll struct wi_read_prism2_args {
    128  1.16.2.7  skrll 	int	i;		/* result count */
    129  1.16.2.7  skrll 	struct wi_scan_res *res;/* current entry in result buffer */
    130  1.16.2.7  skrll 	caddr_t	max;		/* result buffer bound */
    131  1.16.2.7  skrll };
    132  1.16.2.7  skrll 
    133  1.16.2.7  skrll #if 0
    134  1.16.2.7  skrll static void
    135  1.16.2.7  skrll wi_read_prism2_result(void *arg, struct ieee80211_node *ni)
    136  1.16.2.7  skrll {
    137  1.16.2.7  skrll 	struct ieee80211com *ic = ni->ni_ic;
    138  1.16.2.7  skrll 	struct wi_read_prism2_args *sa = arg;
    139  1.16.2.7  skrll 	struct wi_scan_res *res = sa->res;
    140  1.16.2.7  skrll 
    141  1.16.2.7  skrll 	if ((caddr_t)(res + 1) > sa->max)
    142  1.16.2.7  skrll 		return;
    143  1.16.2.7  skrll 	res->wi_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
    144  1.16.2.7  skrll 	res->wi_noise = 0;
    145  1.16.2.7  skrll 	res->wi_signal = ic->ic_node_getrssi(ni);
    146  1.16.2.7  skrll 	IEEE80211_ADDR_COPY(res->wi_bssid, ni->ni_bssid);
    147  1.16.2.7  skrll 	res->wi_interval = ni->ni_intval;
    148  1.16.2.7  skrll 	res->wi_capinfo = ni->ni_capinfo;
    149  1.16.2.7  skrll 	res->wi_ssid_len = ni->ni_esslen;
    150  1.16.2.7  skrll 	memcpy(res->wi_ssid, ni->ni_essid, IEEE80211_NWID_LEN);
    151  1.16.2.7  skrll 	/* NB: assumes wi_srates holds <= ni->ni_rates */
    152  1.16.2.7  skrll 	memcpy(res->wi_srates, ni->ni_rates.rs_rates,
    153  1.16.2.7  skrll 		sizeof(res->wi_srates));
    154  1.16.2.7  skrll 	if (ni->ni_rates.rs_nrates < 10)
    155  1.16.2.7  skrll 		res->wi_srates[ni->ni_rates.rs_nrates] = 0;
    156  1.16.2.7  skrll 	res->wi_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
    157  1.16.2.7  skrll 	res->wi_rsvd = 0;
    158  1.16.2.7  skrll 
    159  1.16.2.7  skrll 	sa->i++;
    160  1.16.2.7  skrll 	sa->res++;
    161  1.16.2.7  skrll }
    162  1.16.2.7  skrll 
    163  1.16.2.7  skrll struct wi_read_sigcache_args {
    164  1.16.2.7  skrll 	int	i;		/* result count */
    165  1.16.2.7  skrll 	struct wi_sigcache *wsc;/* current entry in result buffer */
    166  1.16.2.7  skrll 	caddr_t	max;		/* result buffer bound */
    167  1.16.2.7  skrll };
    168  1.16.2.7  skrll 
    169  1.16.2.7  skrll static void
    170  1.16.2.7  skrll wi_read_sigcache(void *arg, struct ieee80211_node *ni)
    171  1.16.2.7  skrll {
    172  1.16.2.7  skrll 	struct ieee80211com *ic = ni->ni_ic;
    173  1.16.2.7  skrll 	struct wi_read_sigcache_args *sa = arg;
    174  1.16.2.7  skrll 	struct wi_sigcache *wsc = sa->wsc;
    175  1.16.2.7  skrll 
    176  1.16.2.7  skrll 	if ((caddr_t)(wsc + 1) > sa->max)
    177  1.16.2.7  skrll 		return;
    178  1.16.2.7  skrll 	memset(wsc, 0, sizeof(struct wi_sigcache));
    179  1.16.2.7  skrll 	IEEE80211_ADDR_COPY(wsc->macsrc, ni->ni_macaddr);
    180  1.16.2.7  skrll 	wsc->signal = ic->ic_node_getrssi(ni);
    181  1.16.2.7  skrll 
    182  1.16.2.7  skrll 	sa->wsc++;
    183  1.16.2.7  skrll 	sa->i++;
    184  1.16.2.7  skrll }
    185  1.16.2.7  skrll #endif
    186  1.16.2.7  skrll 
    187  1.16.2.2  skrll int
    188  1.16.2.7  skrll ieee80211_cfgget(struct ieee80211com *ic, u_long cmd, caddr_t data)
    189  1.16.2.2  skrll {
    190  1.16.2.7  skrll 	struct ifnet *ifp = ic->ic_ifp;
    191  1.16.2.2  skrll 	int i, j, error;
    192  1.16.2.2  skrll 	struct ifreq *ifr = (struct ifreq *)data;
    193  1.16.2.2  skrll 	struct wi_req wreq;
    194  1.16.2.2  skrll 	struct wi_ltv_keys *keys;
    195  1.16.2.2  skrll 
    196  1.16.2.2  skrll 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
    197  1.16.2.2  skrll 	if (error)
    198  1.16.2.2  skrll 		return error;
    199  1.16.2.2  skrll 	wreq.wi_len = 0;
    200  1.16.2.2  skrll 	switch (wreq.wi_type) {
    201  1.16.2.2  skrll 	case WI_RID_SERIALNO:
    202  1.16.2.2  skrll 	case WI_RID_STA_IDENTITY:
    203  1.16.2.2  skrll 		/* nothing appropriate */
    204  1.16.2.2  skrll 		break;
    205  1.16.2.2  skrll 	case WI_RID_NODENAME:
    206  1.16.2.2  skrll 		strlcpy((char *)&wreq.wi_val[1], hostname,
    207  1.16.2.2  skrll 		    sizeof(wreq.wi_val) - sizeof(wreq.wi_val[0]));
    208  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(strlen(hostname));
    209  1.16.2.2  skrll 		wreq.wi_len = (1 + strlen(hostname) + 1) / 2;
    210  1.16.2.2  skrll 		break;
    211  1.16.2.2  skrll 	case WI_RID_CURRENT_SSID:
    212  1.16.2.2  skrll 		if (ic->ic_state != IEEE80211_S_RUN) {
    213  1.16.2.2  skrll 			wreq.wi_val[0] = 0;
    214  1.16.2.2  skrll 			wreq.wi_len = 1;
    215  1.16.2.2  skrll 			break;
    216  1.16.2.2  skrll 		}
    217  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_esslen);
    218  1.16.2.2  skrll 		memcpy(&wreq.wi_val[1], ic->ic_bss->ni_essid,
    219  1.16.2.2  skrll 		    ic->ic_bss->ni_esslen);
    220  1.16.2.2  skrll 		wreq.wi_len = (1 + ic->ic_bss->ni_esslen + 1) / 2;
    221  1.16.2.2  skrll 		break;
    222  1.16.2.2  skrll 	case WI_RID_OWN_SSID:
    223  1.16.2.2  skrll 	case WI_RID_DESIRED_SSID:
    224  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(ic->ic_des_esslen);
    225  1.16.2.2  skrll 		memcpy(&wreq.wi_val[1], ic->ic_des_essid, ic->ic_des_esslen);
    226  1.16.2.2  skrll 		wreq.wi_len = (1 + ic->ic_des_esslen + 1) / 2;
    227  1.16.2.2  skrll 		break;
    228  1.16.2.2  skrll 	case WI_RID_CURRENT_BSSID:
    229  1.16.2.2  skrll 		if (ic->ic_state == IEEE80211_S_RUN)
    230  1.16.2.2  skrll 			IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_bss->ni_bssid);
    231  1.16.2.2  skrll 		else
    232  1.16.2.2  skrll 			memset(wreq.wi_val, 0, IEEE80211_ADDR_LEN);
    233  1.16.2.2  skrll 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
    234  1.16.2.2  skrll 		break;
    235  1.16.2.2  skrll 	case WI_RID_CHANNEL_LIST:
    236  1.16.2.2  skrll 		memset(wreq.wi_val, 0, sizeof(wreq.wi_val));
    237  1.16.2.2  skrll 		/*
    238  1.16.2.2  skrll 		 * Since channel 0 is not available for DS, channel 1
    239  1.16.2.2  skrll 		 * is assigned to LSB on WaveLAN.
    240  1.16.2.2  skrll 		 */
    241  1.16.2.2  skrll 		if (ic->ic_phytype == IEEE80211_T_DS)
    242  1.16.2.2  skrll 			i = 1;
    243  1.16.2.2  skrll 		else
    244  1.16.2.2  skrll 			i = 0;
    245  1.16.2.2  skrll 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++)
    246  1.16.2.2  skrll 			if (isset(ic->ic_chan_active, i)) {
    247  1.16.2.2  skrll 				setbit((u_int8_t *)wreq.wi_val, j);
    248  1.16.2.2  skrll 				wreq.wi_len = j / 16 + 1;
    249  1.16.2.2  skrll 			}
    250  1.16.2.2  skrll 		break;
    251  1.16.2.2  skrll 	case WI_RID_OWN_CHNL:
    252  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(
    253  1.16.2.2  skrll 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
    254  1.16.2.2  skrll 		wreq.wi_len = 1;
    255  1.16.2.2  skrll 		break;
    256  1.16.2.2  skrll 	case WI_RID_CURRENT_CHAN:
    257  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(
    258  1.16.2.2  skrll 			ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan));
    259  1.16.2.2  skrll 		wreq.wi_len = 1;
    260  1.16.2.2  skrll 		break;
    261  1.16.2.2  skrll 	case WI_RID_COMMS_QUALITY:
    262  1.16.2.2  skrll 		wreq.wi_val[0] = 0;				/* quality */
    263  1.16.2.7  skrll 		wreq.wi_val[1] = htole16(ic->ic_node_getrssi(ic->ic_bss));
    264  1.16.2.2  skrll 		wreq.wi_val[2] = 0;				/* noise */
    265  1.16.2.2  skrll 		wreq.wi_len = 3;
    266  1.16.2.2  skrll 		break;
    267  1.16.2.2  skrll 	case WI_RID_PROMISC:
    268  1.16.2.2  skrll 		wreq.wi_val[0] = htole16((ifp->if_flags & IFF_PROMISC) ? 1 : 0);
    269  1.16.2.2  skrll 		wreq.wi_len = 1;
    270  1.16.2.2  skrll 		break;
    271  1.16.2.2  skrll 	case WI_RID_PORTTYPE:
    272  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(ic->ic_opmode);
    273  1.16.2.2  skrll 		wreq.wi_len = 1;
    274  1.16.2.2  skrll 		break;
    275  1.16.2.2  skrll 	case WI_RID_MAC_NODE:
    276  1.16.2.2  skrll 		IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_myaddr);
    277  1.16.2.2  skrll 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
    278  1.16.2.2  skrll 		break;
    279  1.16.2.2  skrll 	case WI_RID_TX_RATE:
    280  1.16.2.2  skrll 		if (ic->ic_fixed_rate == -1)
    281  1.16.2.2  skrll 			wreq.wi_val[0] = 0;	/* auto */
    282  1.16.2.2  skrll 		else
    283  1.16.2.2  skrll 			wreq.wi_val[0] = htole16(
    284  1.16.2.2  skrll 			    (ic->ic_sup_rates[ic->ic_curmode].rs_rates[ic->ic_fixed_rate] &
    285  1.16.2.2  skrll 			    IEEE80211_RATE_VAL) / 2);
    286  1.16.2.2  skrll 		wreq.wi_len = 1;
    287  1.16.2.2  skrll 		break;
    288  1.16.2.2  skrll 	case WI_RID_CUR_TX_RATE:
    289  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(
    290  1.16.2.2  skrll 		    (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
    291  1.16.2.2  skrll 		    IEEE80211_RATE_VAL) / 2);
    292  1.16.2.2  skrll 		wreq.wi_len = 1;
    293  1.16.2.2  skrll 		break;
    294  1.16.2.2  skrll 	case WI_RID_FRAG_THRESH:
    295  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
    296  1.16.2.2  skrll 		wreq.wi_len = 1;
    297  1.16.2.2  skrll 		break;
    298  1.16.2.2  skrll 	case WI_RID_RTS_THRESH:
    299  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
    300  1.16.2.2  skrll 		wreq.wi_len = 1;
    301  1.16.2.2  skrll 		break;
    302  1.16.2.2  skrll 	case WI_RID_CREATE_IBSS:
    303  1.16.2.2  skrll 		wreq.wi_val[0] =
    304  1.16.2.2  skrll 		    htole16((ic->ic_flags & IEEE80211_F_IBSSON) ? 1 : 0);
    305  1.16.2.2  skrll 		wreq.wi_len = 1;
    306  1.16.2.2  skrll 		break;
    307  1.16.2.2  skrll 	case WI_RID_MICROWAVE_OVEN:
    308  1.16.2.2  skrll 		wreq.wi_val[0] = 0;	/* no ... not supported */
    309  1.16.2.2  skrll 		wreq.wi_len = 1;
    310  1.16.2.2  skrll 		break;
    311  1.16.2.2  skrll 	case WI_RID_ROAMING_MODE:
    312  1.16.2.7  skrll 		wreq.wi_val[0] = htole16(ic->ic_roaming);	/* XXX map */
    313  1.16.2.2  skrll 		wreq.wi_len = 1;
    314  1.16.2.2  skrll 		break;
    315  1.16.2.2  skrll 	case WI_RID_SYSTEM_SCALE:
    316  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(1);	/* low density ... not supp */
    317  1.16.2.2  skrll 		wreq.wi_len = 1;
    318  1.16.2.2  skrll 		break;
    319  1.16.2.2  skrll 	case WI_RID_PM_ENABLED:
    320  1.16.2.2  skrll 		wreq.wi_val[0] =
    321  1.16.2.2  skrll 		    htole16((ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
    322  1.16.2.2  skrll 		wreq.wi_len = 1;
    323  1.16.2.2  skrll 		break;
    324  1.16.2.2  skrll 	case WI_RID_MAX_SLEEP:
    325  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(ic->ic_lintval);
    326  1.16.2.2  skrll 		wreq.wi_len = 1;
    327  1.16.2.2  skrll 		break;
    328  1.16.2.2  skrll 	case WI_RID_CUR_BEACON_INT:
    329  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_intval);
    330  1.16.2.2  skrll 		wreq.wi_len = 1;
    331  1.16.2.2  skrll 		break;
    332  1.16.2.2  skrll 	case WI_RID_WEP_AVAIL:
    333  1.16.2.7  skrll 		wreq.wi_val[0] = htole16(1);	/* always available */
    334  1.16.2.2  skrll 		wreq.wi_len = 1;
    335  1.16.2.2  skrll 		break;
    336  1.16.2.2  skrll 	case WI_RID_CNFAUTHMODE:
    337  1.16.2.2  skrll 		wreq.wi_val[0] = htole16(1);	/* TODO: open system only */
    338  1.16.2.2  skrll 		wreq.wi_len = 1;
    339  1.16.2.2  skrll 		break;
    340  1.16.2.2  skrll 	case WI_RID_ENCRYPTION:
    341  1.16.2.2  skrll 		wreq.wi_val[0] =
    342  1.16.2.2  skrll 		    htole16((ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0);
    343  1.16.2.2  skrll 		wreq.wi_len = 1;
    344  1.16.2.2  skrll 		break;
    345  1.16.2.2  skrll 	case WI_RID_TX_CRYPT_KEY:
    346  1.16.2.7  skrll 		wreq.wi_val[0] = htole16(ic->ic_def_txkey);
    347  1.16.2.2  skrll 		wreq.wi_len = 1;
    348  1.16.2.2  skrll 		break;
    349  1.16.2.2  skrll 	case WI_RID_DEFLT_CRYPT_KEYS:
    350  1.16.2.2  skrll 		keys = (struct wi_ltv_keys *)&wreq;
    351  1.16.2.2  skrll 		/* do not show keys to non-root user */
    352  1.16.2.2  skrll 		error = suser(curproc->p_ucred, &curproc->p_acflag);
    353  1.16.2.2  skrll 		if (error) {
    354  1.16.2.2  skrll 			memset(keys, 0, sizeof(*keys));
    355  1.16.2.2  skrll 			error = 0;
    356  1.16.2.2  skrll 			break;
    357  1.16.2.2  skrll 		}
    358  1.16.2.2  skrll 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    359  1.16.2.2  skrll 			keys->wi_keys[i].wi_keylen =
    360  1.16.2.7  skrll 			    htole16(ic->ic_nw_keys[i].wk_keylen);
    361  1.16.2.2  skrll 			memcpy(keys->wi_keys[i].wi_keydat,
    362  1.16.2.7  skrll 			    ic->ic_nw_keys[i].wk_key,
    363  1.16.2.7  skrll 			    ic->ic_nw_keys[i].wk_keylen);
    364  1.16.2.2  skrll 		}
    365  1.16.2.2  skrll 		wreq.wi_len = sizeof(*keys) / 2;
    366  1.16.2.2  skrll 		break;
    367  1.16.2.2  skrll 	case WI_RID_MAX_DATALEN:
    368  1.16.2.7  skrll 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
    369  1.16.2.2  skrll 		wreq.wi_len = 1;
    370  1.16.2.2  skrll 		break;
    371  1.16.2.2  skrll 	case WI_RID_DBM_ADJUST:
    372  1.16.2.2  skrll 		/* not supported, we just pass rssi value from driver. */
    373  1.16.2.2  skrll 		break;
    374  1.16.2.2  skrll 	case WI_RID_IFACE_STATS:
    375  1.16.2.2  skrll 		/* XXX: should be implemented in lower drivers */
    376  1.16.2.2  skrll 		break;
    377  1.16.2.2  skrll 	case WI_RID_READ_APS:
    378  1.16.2.7  skrll 		/*
    379  1.16.2.7  skrll 		 * Don't return results until active scan completes.
    380  1.16.2.7  skrll 		 */
    381  1.16.2.7  skrll 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
    382  1.16.2.7  skrll 			struct wi_read_ap_args args;
    383  1.16.2.7  skrll 
    384  1.16.2.7  skrll 			args.i = 0;
    385  1.16.2.7  skrll 			args.ap = (void *)((char *)wreq.wi_val + sizeof(i));
    386  1.16.2.7  skrll 			args.max = (void *)(&wreq + 1);
    387  1.16.2.7  skrll 			ieee80211_iterate_nodes(&ic->ic_scan,
    388  1.16.2.7  skrll 				wi_read_ap_result, &args);
    389  1.16.2.7  skrll 			memcpy(wreq.wi_val, &args.i, sizeof(args.i));
    390  1.16.2.7  skrll 			wreq.wi_len = (sizeof(int) +
    391  1.16.2.7  skrll 				sizeof(struct wi_apinfo) * args.i) / 2;
    392  1.16.2.7  skrll 		} else
    393  1.16.2.7  skrll 			error = EINPROGRESS;
    394  1.16.2.2  skrll 		break;
    395  1.16.2.2  skrll #if 0
    396  1.16.2.2  skrll 	case WI_RID_SCAN_RES:			/* compatibility interface */
    397  1.16.2.7  skrll 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
    398  1.16.2.7  skrll 			struct wi_read_prism2_args args;
    399  1.16.2.7  skrll 			struct wi_scan_p2_hdr *p2;
    400  1.16.2.7  skrll 
    401  1.16.2.7  skrll 			/* NB: use Prism2 format so we can include rate info */
    402  1.16.2.7  skrll 			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
    403  1.16.2.7  skrll 			args.i = 0;
    404  1.16.2.7  skrll 			args.res = (void *)&p2[1];
    405  1.16.2.7  skrll 			args.max = (void *)(&wreq + 1);
    406  1.16.2.7  skrll 			ieee80211_iterate_nodes(&ic->ic_scan,
    407  1.16.2.7  skrll 				wi_read_prism2_result, &args);
    408  1.16.2.7  skrll 			p2->wi_rsvd = 0;
    409  1.16.2.7  skrll 			p2->wi_reason = args.i;
    410  1.16.2.7  skrll 			wreq.wi_len = (sizeof(*p2) +
    411  1.16.2.7  skrll 				sizeof(struct wi_scan_res) * args.i) / 2;
    412  1.16.2.7  skrll 		} else
    413  1.16.2.2  skrll 			error = EINPROGRESS;
    414  1.16.2.2  skrll 		break;
    415  1.16.2.7  skrll 	case WI_RID_READ_CACHE: {
    416  1.16.2.7  skrll 		struct wi_read_sigcache_args args;
    417  1.16.2.7  skrll 		args.i = 0;
    418  1.16.2.7  skrll 		args.wsc = (struct wi_sigcache *) wreq.wi_val;
    419  1.16.2.7  skrll 		args.max = (void *)(&wreq + 1);
    420  1.16.2.7  skrll 		ieee80211_iterate_nodes(&ic->ic_scan, wi_read_sigcache, &args);
    421  1.16.2.7  skrll 		wreq.wi_len = sizeof(struct wi_sigcache) * args.i / 2;
    422  1.16.2.2  skrll 		break;
    423  1.16.2.7  skrll 	}
    424  1.16.2.7  skrll #endif
    425  1.16.2.2  skrll 	default:
    426  1.16.2.2  skrll 		error = EINVAL;
    427  1.16.2.2  skrll 		break;
    428  1.16.2.2  skrll 	}
    429  1.16.2.2  skrll 	if (error == 0) {
    430  1.16.2.2  skrll 		wreq.wi_len++;
    431  1.16.2.2  skrll 		error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
    432  1.16.2.2  skrll 	}
    433  1.16.2.2  skrll 	return error;
    434  1.16.2.2  skrll }
    435  1.16.2.2  skrll 
    436  1.16.2.2  skrll static int
    437  1.16.2.2  skrll findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
    438  1.16.2.2  skrll {
    439  1.16.2.2  skrll #define	IEEERATE(_ic,_m,_i) \
    440  1.16.2.2  skrll 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
    441  1.16.2.2  skrll 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
    442  1.16.2.2  skrll 	for (i = 0; i < nrates; i++)
    443  1.16.2.2  skrll 		if (IEEERATE(ic, mode, i) == rate)
    444  1.16.2.2  skrll 			return i;
    445  1.16.2.2  skrll 	return -1;
    446  1.16.2.2  skrll #undef IEEERATE
    447  1.16.2.2  skrll }
    448  1.16.2.2  skrll 
    449  1.16.2.2  skrll /*
    450  1.16.2.2  skrll  * Prepare to do a user-initiated scan for AP's.  If no
    451  1.16.2.2  skrll  * current/default channel is setup or the current channel
    452  1.16.2.2  skrll  * is invalid then pick the first available channel from
    453  1.16.2.2  skrll  * the active list as the place to start the scan.
    454  1.16.2.2  skrll  */
    455  1.16.2.2  skrll static int
    456  1.16.2.7  skrll ieee80211_setupscan(struct ieee80211com *ic, const u_int8_t chanlist[])
    457  1.16.2.2  skrll {
    458  1.16.2.2  skrll 	int i;
    459  1.16.2.2  skrll 
    460  1.16.2.7  skrll 	/*
    461  1.16.2.7  skrll 	 * XXX don't permit a scan to be started unless we
    462  1.16.2.7  skrll 	 * know the device is ready.  For the moment this means
    463  1.16.2.7  skrll 	 * the device is marked up as this is the required to
    464  1.16.2.7  skrll 	 * initialize the hardware.  It would be better to permit
    465  1.16.2.7  skrll 	 * scanning prior to being up but that'll require some
    466  1.16.2.7  skrll 	 * changes to the infrastructure.
    467  1.16.2.7  skrll 	 */
    468  1.16.2.7  skrll 	if (!IS_UP(ic))
    469  1.16.2.7  skrll 		return EINVAL;
    470  1.16.2.2  skrll 	if (ic->ic_ibss_chan == NULL ||
    471  1.16.2.2  skrll 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
    472  1.16.2.2  skrll 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
    473  1.16.2.2  skrll 			if (isset(chanlist, i)) {
    474  1.16.2.2  skrll 				ic->ic_ibss_chan = &ic->ic_channels[i];
    475  1.16.2.2  skrll 				goto found;
    476  1.16.2.2  skrll 			}
    477  1.16.2.2  skrll 		return EINVAL;			/* no active channels */
    478  1.16.2.2  skrll found:
    479  1.16.2.2  skrll 		;
    480  1.16.2.2  skrll 	}
    481  1.16.2.2  skrll 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC ||
    482  1.16.2.2  skrll 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
    483  1.16.2.2  skrll 		ic->ic_bss->ni_chan = ic->ic_ibss_chan;
    484  1.16.2.7  skrll 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
    485  1.16.2.2  skrll 	/*
    486  1.16.2.7  skrll 	 * We force the state to INIT before calling ieee80211_new_state
    487  1.16.2.7  skrll 	 * to get ieee80211_begin_scan called.  We really want to scan w/o
    488  1.16.2.7  skrll 	 * altering the current state but that's not possible right now.
    489  1.16.2.2  skrll 	 */
    490  1.16.2.7  skrll 	/* XXX handle proberequest case */
    491  1.16.2.7  skrll 	ic->ic_state = IEEE80211_S_INIT;	/* XXX bypass state machine */
    492  1.16.2.7  skrll 	return 0;
    493  1.16.2.2  skrll }
    494  1.16.2.2  skrll 
    495  1.16.2.2  skrll int
    496  1.16.2.7  skrll ieee80211_cfgset(struct ieee80211com *ic, u_long cmd, caddr_t data)
    497  1.16.2.2  skrll {
    498  1.16.2.7  skrll 	struct ifnet *ifp = ic->ic_ifp;
    499  1.16.2.2  skrll 	int i, j, len, error, rate;
    500  1.16.2.2  skrll 	struct ifreq *ifr = (struct ifreq *)data;
    501  1.16.2.2  skrll 	struct wi_ltv_keys *keys;
    502  1.16.2.2  skrll 	struct wi_req wreq;
    503  1.16.2.2  skrll 	u_char chanlist[roundup(IEEE80211_CHAN_MAX, NBBY)];
    504  1.16.2.2  skrll 
    505  1.16.2.2  skrll 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
    506  1.16.2.2  skrll 	if (error)
    507  1.16.2.2  skrll 		return error;
    508  1.16.2.2  skrll 	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
    509  1.16.2.2  skrll 	switch (wreq.wi_type) {
    510  1.16.2.2  skrll 	case WI_RID_SERIALNO:
    511  1.16.2.2  skrll 	case WI_RID_NODENAME:
    512  1.16.2.2  skrll 		return EPERM;
    513  1.16.2.2  skrll 	case WI_RID_CURRENT_SSID:
    514  1.16.2.2  skrll 		return EPERM;
    515  1.16.2.2  skrll 	case WI_RID_OWN_SSID:
    516  1.16.2.2  skrll 	case WI_RID_DESIRED_SSID:
    517  1.16.2.2  skrll 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
    518  1.16.2.2  skrll 		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
    519  1.16.2.2  skrll 			error = ENOSPC;
    520  1.16.2.2  skrll 			break;
    521  1.16.2.2  skrll 		}
    522  1.16.2.2  skrll 		memset(ic->ic_des_essid, 0, sizeof(ic->ic_des_essid));
    523  1.16.2.2  skrll 		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
    524  1.16.2.2  skrll 		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
    525  1.16.2.2  skrll 		error = ENETRESET;
    526  1.16.2.2  skrll 		break;
    527  1.16.2.2  skrll 	case WI_RID_CURRENT_BSSID:
    528  1.16.2.2  skrll 		return EPERM;
    529  1.16.2.2  skrll 	case WI_RID_OWN_CHNL:
    530  1.16.2.2  skrll 		if (len != 2)
    531  1.16.2.2  skrll 			return EINVAL;
    532  1.16.2.2  skrll 		i = le16toh(wreq.wi_val[0]);
    533  1.16.2.2  skrll 		if (i < 0 ||
    534  1.16.2.2  skrll 		    i > IEEE80211_CHAN_MAX ||
    535  1.16.2.2  skrll 		    isclr(ic->ic_chan_active, i))
    536  1.16.2.2  skrll 			return EINVAL;
    537  1.16.2.2  skrll 		ic->ic_ibss_chan = &ic->ic_channels[i];
    538  1.16.2.7  skrll 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
    539  1.16.2.7  skrll 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    540  1.16.2.7  skrll 		else
    541  1.16.2.2  skrll 			error = ENETRESET;
    542  1.16.2.2  skrll 		break;
    543  1.16.2.2  skrll 	case WI_RID_CURRENT_CHAN:
    544  1.16.2.2  skrll 		return EPERM;
    545  1.16.2.2  skrll 	case WI_RID_COMMS_QUALITY:
    546  1.16.2.2  skrll 		return EPERM;
    547  1.16.2.2  skrll 	case WI_RID_PROMISC:
    548  1.16.2.2  skrll 		if (len != 2)
    549  1.16.2.2  skrll 			return EINVAL;
    550  1.16.2.2  skrll 		if (ifp->if_flags & IFF_PROMISC) {
    551  1.16.2.2  skrll 			if (wreq.wi_val[0] == 0) {
    552  1.16.2.2  skrll 				ifp->if_flags &= ~IFF_PROMISC;
    553  1.16.2.2  skrll 				error = ENETRESET;
    554  1.16.2.2  skrll 			}
    555  1.16.2.2  skrll 		} else {
    556  1.16.2.2  skrll 			if (wreq.wi_val[0] != 0) {
    557  1.16.2.2  skrll 				ifp->if_flags |= IFF_PROMISC;
    558  1.16.2.2  skrll 				error = ENETRESET;
    559  1.16.2.2  skrll 			}
    560  1.16.2.2  skrll 		}
    561  1.16.2.2  skrll 		break;
    562  1.16.2.2  skrll 	case WI_RID_PORTTYPE:
    563  1.16.2.2  skrll 		if (len != 2)
    564  1.16.2.2  skrll 			return EINVAL;
    565  1.16.2.2  skrll 		switch (le16toh(wreq.wi_val[0])) {
    566  1.16.2.2  skrll 		case IEEE80211_M_STA:
    567  1.16.2.2  skrll 			break;
    568  1.16.2.2  skrll 		case IEEE80211_M_IBSS:
    569  1.16.2.2  skrll 			if (!(ic->ic_caps & IEEE80211_C_IBSS))
    570  1.16.2.2  skrll 				return EINVAL;
    571  1.16.2.2  skrll 			break;
    572  1.16.2.2  skrll 		case IEEE80211_M_AHDEMO:
    573  1.16.2.2  skrll 			if (ic->ic_phytype != IEEE80211_T_DS ||
    574  1.16.2.2  skrll 			    !(ic->ic_caps & IEEE80211_C_AHDEMO))
    575  1.16.2.2  skrll 				return EINVAL;
    576  1.16.2.2  skrll 			break;
    577  1.16.2.2  skrll 		case IEEE80211_M_HOSTAP:
    578  1.16.2.2  skrll 			if (!(ic->ic_caps & IEEE80211_C_HOSTAP))
    579  1.16.2.2  skrll 				return EINVAL;
    580  1.16.2.2  skrll 			break;
    581  1.16.2.2  skrll 		default:
    582  1.16.2.2  skrll 			return EINVAL;
    583  1.16.2.2  skrll 		}
    584  1.16.2.2  skrll 		if (le16toh(wreq.wi_val[0]) != ic->ic_opmode) {
    585  1.16.2.2  skrll 			ic->ic_opmode = le16toh(wreq.wi_val[0]);
    586  1.16.2.7  skrll 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    587  1.16.2.2  skrll 		}
    588  1.16.2.2  skrll 		break;
    589  1.16.2.2  skrll #if 0
    590  1.16.2.2  skrll 	case WI_RID_MAC_NODE:
    591  1.16.2.2  skrll 		if (len != IEEE80211_ADDR_LEN)
    592  1.16.2.2  skrll 			return EINVAL;
    593  1.16.2.2  skrll 		IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), wreq.wi_val);
    594  1.16.2.2  skrll 		/* if_init will copy lladdr into ic_myaddr */
    595  1.16.2.2  skrll 		error = ENETRESET;
    596  1.16.2.2  skrll 		break;
    597  1.16.2.2  skrll #endif
    598  1.16.2.2  skrll 	case WI_RID_TX_RATE:
    599  1.16.2.2  skrll 		if (len != 2)
    600  1.16.2.2  skrll 			return EINVAL;
    601  1.16.2.2  skrll 		if (wreq.wi_val[0] == 0) {
    602  1.16.2.2  skrll 			/* auto */
    603  1.16.2.2  skrll 			ic->ic_fixed_rate = -1;
    604  1.16.2.2  skrll 			break;
    605  1.16.2.2  skrll 		}
    606  1.16.2.2  skrll 		rate = 2 * le16toh(wreq.wi_val[0]);
    607  1.16.2.2  skrll 		if (ic->ic_curmode == IEEE80211_MODE_AUTO) {
    608  1.16.2.2  skrll 			/*
    609  1.16.2.2  skrll 			 * In autoselect mode search for the rate.  We take
    610  1.16.2.2  skrll 			 * the first instance which may not be right, but we
    611  1.16.2.2  skrll 			 * are limited by the interface.  Note that we also
    612  1.16.2.2  skrll 			 * lock the mode to insure the rate is meaningful
    613  1.16.2.2  skrll 			 * when it is used.
    614  1.16.2.2  skrll 			 */
    615  1.16.2.2  skrll 			for (j = IEEE80211_MODE_11A;
    616  1.16.2.2  skrll 			     j < IEEE80211_MODE_MAX; j++) {
    617  1.16.2.2  skrll 				if ((ic->ic_modecaps & (1<<j)) == 0)
    618  1.16.2.2  skrll 					continue;
    619  1.16.2.2  skrll 				i = findrate(ic, j, rate);
    620  1.16.2.2  skrll 				if (i != -1) {
    621  1.16.2.2  skrll 					/* lock mode too */
    622  1.16.2.2  skrll 					ic->ic_curmode = j;
    623  1.16.2.2  skrll 					goto setrate;
    624  1.16.2.2  skrll 				}
    625  1.16.2.2  skrll 			}
    626  1.16.2.2  skrll 		} else {
    627  1.16.2.2  skrll 			i = findrate(ic, ic->ic_curmode, rate);
    628  1.16.2.2  skrll 			if (i != -1)
    629  1.16.2.2  skrll 				goto setrate;
    630  1.16.2.2  skrll 		}
    631  1.16.2.2  skrll 		return EINVAL;
    632  1.16.2.2  skrll 	setrate:
    633  1.16.2.2  skrll 		ic->ic_fixed_rate = i;
    634  1.16.2.7  skrll 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    635  1.16.2.2  skrll 		break;
    636  1.16.2.2  skrll 	case WI_RID_CUR_TX_RATE:
    637  1.16.2.2  skrll 		return EPERM;
    638  1.16.2.2  skrll 	case WI_RID_FRAG_THRESH:
    639  1.16.2.2  skrll 		if (len != 2)
    640  1.16.2.2  skrll 			return EINVAL;
    641  1.16.2.2  skrll 		ic->ic_fragthreshold = le16toh(wreq.wi_val[0]);
    642  1.16.2.2  skrll 		error = ENETRESET;
    643  1.16.2.2  skrll 		break;
    644  1.16.2.2  skrll 	case WI_RID_RTS_THRESH:
    645  1.16.2.2  skrll 		if (len != 2)
    646  1.16.2.2  skrll 			return EINVAL;
    647  1.16.2.2  skrll 		ic->ic_rtsthreshold = le16toh(wreq.wi_val[0]);
    648  1.16.2.2  skrll 		error = ENETRESET;
    649  1.16.2.2  skrll 		break;
    650  1.16.2.2  skrll 	case WI_RID_CREATE_IBSS:
    651  1.16.2.2  skrll 		if (len != 2)
    652  1.16.2.2  skrll 			return EINVAL;
    653  1.16.2.2  skrll 		if (wreq.wi_val[0] != 0) {
    654  1.16.2.2  skrll 			if ((ic->ic_caps & IEEE80211_C_IBSS) == 0)
    655  1.16.2.2  skrll 				return EINVAL;
    656  1.16.2.2  skrll 			if ((ic->ic_flags & IEEE80211_F_IBSSON) == 0) {
    657  1.16.2.2  skrll 				ic->ic_flags |= IEEE80211_F_IBSSON;
    658  1.16.2.2  skrll 				if (ic->ic_opmode == IEEE80211_M_IBSS &&
    659  1.16.2.2  skrll 				    ic->ic_state == IEEE80211_S_SCAN)
    660  1.16.2.7  skrll 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
    661  1.16.2.2  skrll 			}
    662  1.16.2.2  skrll 		} else {
    663  1.16.2.2  skrll 			if (ic->ic_flags & IEEE80211_F_IBSSON) {
    664  1.16.2.2  skrll 				ic->ic_flags &= ~IEEE80211_F_IBSSON;
    665  1.16.2.2  skrll 				if (ic->ic_flags & IEEE80211_F_SIBSS) {
    666  1.16.2.2  skrll 					ic->ic_flags &= ~IEEE80211_F_SIBSS;
    667  1.16.2.7  skrll 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
    668  1.16.2.2  skrll 				}
    669  1.16.2.2  skrll 			}
    670  1.16.2.2  skrll 		}
    671  1.16.2.2  skrll 		break;
    672  1.16.2.2  skrll 	case WI_RID_MICROWAVE_OVEN:
    673  1.16.2.2  skrll 		if (len != 2)
    674  1.16.2.2  skrll 			return EINVAL;
    675  1.16.2.2  skrll 		if (wreq.wi_val[0] != 0)
    676  1.16.2.2  skrll 			return EINVAL;		/* not supported */
    677  1.16.2.2  skrll 		break;
    678  1.16.2.2  skrll 	case WI_RID_ROAMING_MODE:
    679  1.16.2.2  skrll 		if (len != 2)
    680  1.16.2.2  skrll 			return EINVAL;
    681  1.16.2.7  skrll 		i = le16toh(wreq.wi_val[0]);
    682  1.16.2.7  skrll 		if (i > IEEE80211_ROAMING_MANUAL)
    683  1.16.2.2  skrll 			return EINVAL;		/* not supported */
    684  1.16.2.7  skrll 		ic->ic_roaming = i;
    685  1.16.2.2  skrll 		break;
    686  1.16.2.2  skrll 	case WI_RID_SYSTEM_SCALE:
    687  1.16.2.2  skrll 		if (len != 2)
    688  1.16.2.2  skrll 			return EINVAL;
    689  1.16.2.2  skrll 		if (le16toh(wreq.wi_val[0]) != 1)
    690  1.16.2.2  skrll 			return EINVAL;		/* not supported */
    691  1.16.2.2  skrll 		break;
    692  1.16.2.2  skrll 	case WI_RID_PM_ENABLED:
    693  1.16.2.2  skrll 		if (len != 2)
    694  1.16.2.2  skrll 			return EINVAL;
    695  1.16.2.2  skrll 		if (wreq.wi_val[0] != 0) {
    696  1.16.2.2  skrll 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
    697  1.16.2.2  skrll 				return EINVAL;
    698  1.16.2.2  skrll 			if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
    699  1.16.2.2  skrll 				ic->ic_flags |= IEEE80211_F_PMGTON;
    700  1.16.2.7  skrll 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    701  1.16.2.2  skrll 			}
    702  1.16.2.2  skrll 		} else {
    703  1.16.2.2  skrll 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
    704  1.16.2.2  skrll 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
    705  1.16.2.7  skrll 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    706  1.16.2.2  skrll 			}
    707  1.16.2.2  skrll 		}
    708  1.16.2.2  skrll 		break;
    709  1.16.2.2  skrll 	case WI_RID_MAX_SLEEP:
    710  1.16.2.2  skrll 		if (len != 2)
    711  1.16.2.2  skrll 			return EINVAL;
    712  1.16.2.2  skrll 		ic->ic_lintval = le16toh(wreq.wi_val[0]);
    713  1.16.2.2  skrll 		if (ic->ic_flags & IEEE80211_F_PMGTON)
    714  1.16.2.7  skrll 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    715  1.16.2.2  skrll 		break;
    716  1.16.2.2  skrll 	case WI_RID_CUR_BEACON_INT:
    717  1.16.2.2  skrll 		return EPERM;
    718  1.16.2.2  skrll 	case WI_RID_WEP_AVAIL:
    719  1.16.2.2  skrll 		return EPERM;
    720  1.16.2.2  skrll 	case WI_RID_CNFAUTHMODE:
    721  1.16.2.2  skrll 		if (len != 2)
    722  1.16.2.2  skrll 			return EINVAL;
    723  1.16.2.7  skrll 		i = le16toh(wreq.wi_val[0]);
    724  1.16.2.7  skrll 		if (i > IEEE80211_AUTH_WPA)
    725  1.16.2.7  skrll 			return EINVAL;
    726  1.16.2.7  skrll 		ic->ic_bss->ni_authmode = i;		/* XXX ENETRESET? */
    727  1.16.2.7  skrll 		error = ENETRESET;
    728  1.16.2.2  skrll 		break;
    729  1.16.2.2  skrll 	case WI_RID_ENCRYPTION:
    730  1.16.2.2  skrll 		if (len != 2)
    731  1.16.2.2  skrll 			return EINVAL;
    732  1.16.2.2  skrll 		if (wreq.wi_val[0] != 0) {
    733  1.16.2.2  skrll 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
    734  1.16.2.2  skrll 				return EINVAL;
    735  1.16.2.2  skrll 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
    736  1.16.2.2  skrll 				ic->ic_flags |= IEEE80211_F_PRIVACY;
    737  1.16.2.2  skrll 				error = ENETRESET;
    738  1.16.2.2  skrll 			}
    739  1.16.2.2  skrll 		} else {
    740  1.16.2.2  skrll 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
    741  1.16.2.2  skrll 				ic->ic_flags &= ~IEEE80211_F_PRIVACY;
    742  1.16.2.2  skrll 				error = ENETRESET;
    743  1.16.2.2  skrll 			}
    744  1.16.2.2  skrll 		}
    745  1.16.2.2  skrll 		break;
    746  1.16.2.2  skrll 	case WI_RID_TX_CRYPT_KEY:
    747  1.16.2.2  skrll 		if (len != 2)
    748  1.16.2.2  skrll 			return EINVAL;
    749  1.16.2.2  skrll 		i = le16toh(wreq.wi_val[0]);
    750  1.16.2.2  skrll 		if (i >= IEEE80211_WEP_NKID)
    751  1.16.2.2  skrll 			return EINVAL;
    752  1.16.2.7  skrll 		ic->ic_def_txkey = i;
    753  1.16.2.7  skrll 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    754  1.16.2.2  skrll 		break;
    755  1.16.2.2  skrll 	case WI_RID_DEFLT_CRYPT_KEYS:
    756  1.16.2.2  skrll 		if (len != sizeof(struct wi_ltv_keys))
    757  1.16.2.2  skrll 			return EINVAL;
    758  1.16.2.2  skrll 		keys = (struct wi_ltv_keys *)&wreq;
    759  1.16.2.2  skrll 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    760  1.16.2.2  skrll 			len = le16toh(keys->wi_keys[i].wi_keylen);
    761  1.16.2.2  skrll 			if (len != 0 && len < IEEE80211_WEP_KEYLEN)
    762  1.16.2.2  skrll 				return EINVAL;
    763  1.16.2.7  skrll 			if (len > IEEE80211_KEYBUF_SIZE)
    764  1.16.2.2  skrll 				return EINVAL;
    765  1.16.2.2  skrll 		}
    766  1.16.2.2  skrll 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
    767  1.16.2.7  skrll 			struct ieee80211_key *k = &ic->ic_nw_keys[i];
    768  1.16.2.7  skrll 
    769  1.16.2.2  skrll 			len = le16toh(keys->wi_keys[i].wi_keylen);
    770  1.16.2.7  skrll 			k->wk_keylen = len;
    771  1.16.2.7  skrll 			k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
    772  1.16.2.7  skrll 			memset(k->wk_key, 0, sizeof(k->wk_key));
    773  1.16.2.7  skrll 			memcpy(k->wk_key, keys->wi_keys[i].wi_keydat, len);
    774  1.16.2.7  skrll #if 0
    775  1.16.2.7  skrll 			k->wk_type = IEEE80211_CIPHER_WEP;
    776  1.16.2.7  skrll #endif
    777  1.16.2.2  skrll 		}
    778  1.16.2.2  skrll 		error = ENETRESET;
    779  1.16.2.2  skrll 		break;
    780  1.16.2.2  skrll 	case WI_RID_MAX_DATALEN:
    781  1.16.2.2  skrll 		if (len != 2)
    782  1.16.2.2  skrll 			return EINVAL;
    783  1.16.2.2  skrll 		len = le16toh(wreq.wi_val[0]);
    784  1.16.2.2  skrll 		if (len < 350 /* ? */ || len > IEEE80211_MAX_LEN)
    785  1.16.2.2  skrll 			return EINVAL;
    786  1.16.2.2  skrll 		ic->ic_fragthreshold = len;
    787  1.16.2.7  skrll 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
    788  1.16.2.2  skrll 		break;
    789  1.16.2.2  skrll 	case WI_RID_IFACE_STATS:
    790  1.16.2.2  skrll 		error = EPERM;
    791  1.16.2.2  skrll 		break;
    792  1.16.2.2  skrll 	case WI_RID_SCAN_REQ:			/* XXX wicontrol */
    793  1.16.2.2  skrll 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    794  1.16.2.2  skrll 			break;
    795  1.16.2.7  skrll 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
    796  1.16.2.2  skrll 		if (error == 0)
    797  1.16.2.2  skrll 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    798  1.16.2.2  skrll 		break;
    799  1.16.2.2  skrll 	case WI_RID_SCAN_APS:
    800  1.16.2.2  skrll 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
    801  1.16.2.2  skrll 			break;
    802  1.16.2.2  skrll 		len--;			/* XXX: tx rate? */
    803  1.16.2.2  skrll 		/* FALLTHRU */
    804  1.16.2.2  skrll 	case WI_RID_CHANNEL_LIST:
    805  1.16.2.2  skrll 		memset(chanlist, 0, sizeof(chanlist));
    806  1.16.2.2  skrll 		/*
    807  1.16.2.2  skrll 		 * Since channel 0 is not available for DS, channel 1
    808  1.16.2.2  skrll 		 * is assigned to LSB on WaveLAN.
    809  1.16.2.2  skrll 		 */
    810  1.16.2.2  skrll 		if (ic->ic_phytype == IEEE80211_T_DS)
    811  1.16.2.2  skrll 			i = 1;
    812  1.16.2.2  skrll 		else
    813  1.16.2.2  skrll 			i = 0;
    814  1.16.2.2  skrll 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
    815  1.16.2.2  skrll 			if ((j / 8) >= len)
    816  1.16.2.2  skrll 				break;
    817  1.16.2.2  skrll 			if (isclr((u_int8_t *)wreq.wi_val, j))
    818  1.16.2.2  skrll 				continue;
    819  1.16.2.2  skrll 			if (isclr(ic->ic_chan_active, i)) {
    820  1.16.2.2  skrll 				if (wreq.wi_type != WI_RID_CHANNEL_LIST)
    821  1.16.2.2  skrll 					continue;
    822  1.16.2.2  skrll 				if (isclr(ic->ic_chan_avail, i))
    823  1.16.2.2  skrll 					return EPERM;
    824  1.16.2.2  skrll 			}
    825  1.16.2.2  skrll 			setbit(chanlist, i);
    826  1.16.2.2  skrll 		}
    827  1.16.2.7  skrll 		error = ieee80211_setupscan(ic, chanlist);
    828  1.16.2.2  skrll 		if (wreq.wi_type == WI_RID_CHANNEL_LIST) {
    829  1.16.2.2  skrll 			/* NB: ignore error from ieee80211_setupscan */
    830  1.16.2.2  skrll 			error = ENETRESET;
    831  1.16.2.2  skrll 		} else if (error == 0)
    832  1.16.2.2  skrll 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
    833  1.16.2.2  skrll 		break;
    834  1.16.2.2  skrll 	default:
    835  1.16.2.2  skrll 		error = EINVAL;
    836  1.16.2.2  skrll 		break;
    837  1.16.2.2  skrll 	}
    838  1.16.2.7  skrll 	if (error == ENETRESET && !IS_UP_AUTO(ic))
    839  1.16.2.7  skrll 		error = 0;
    840  1.16.2.2  skrll 	return error;
    841  1.16.2.2  skrll }
    842  1.16.2.2  skrll 
    843  1.16.2.2  skrll #ifdef __FreeBSD__
    844  1.16.2.7  skrll static struct ieee80211_channel *
    845  1.16.2.7  skrll getcurchan(struct ieee80211com *ic)
    846  1.16.2.7  skrll {
    847  1.16.2.7  skrll 	switch (ic->ic_state) {
    848  1.16.2.7  skrll 	case IEEE80211_S_INIT:
    849  1.16.2.7  skrll 	case IEEE80211_S_SCAN:
    850  1.16.2.7  skrll 		return ic->ic_des_chan;
    851  1.16.2.7  skrll 	default:
    852  1.16.2.7  skrll 		return ic->ic_ibss_chan;
    853  1.16.2.7  skrll 	}
    854  1.16.2.7  skrll }
    855  1.16.2.7  skrll #endif /* __FreeBSD__ */
    856  1.16.2.7  skrll 
    857  1.16.2.7  skrll static int
    858  1.16.2.7  skrll cap2cipher(int flag)
    859  1.16.2.7  skrll {
    860  1.16.2.7  skrll 	switch (flag) {
    861  1.16.2.7  skrll 	case IEEE80211_C_WEP:		return IEEE80211_CIPHER_WEP;
    862  1.16.2.7  skrll 	case IEEE80211_C_AES:		return IEEE80211_CIPHER_AES_OCB;
    863  1.16.2.7  skrll 	case IEEE80211_C_AES_CCM:	return IEEE80211_CIPHER_AES_CCM;
    864  1.16.2.7  skrll 	case IEEE80211_C_CKIP:		return IEEE80211_CIPHER_CKIP;
    865  1.16.2.7  skrll 	case IEEE80211_C_TKIP:		return IEEE80211_CIPHER_TKIP;
    866  1.16.2.7  skrll 	}
    867  1.16.2.7  skrll 	return -1;
    868  1.16.2.7  skrll }
    869  1.16.2.7  skrll 
    870  1.16.2.7  skrll static int
    871  1.16.2.7  skrll ieee80211_ioctl_getkey(struct ieee80211com *ic, struct ieee80211req *ireq)
    872  1.16.2.7  skrll {
    873  1.16.2.7  skrll 	struct ieee80211_node *ni;
    874  1.16.2.7  skrll 	struct ieee80211req_key ik;
    875  1.16.2.7  skrll 	struct ieee80211_key *wk;
    876  1.16.2.7  skrll 	const struct ieee80211_cipher *cip;
    877  1.16.2.7  skrll 	u_int kid;
    878  1.16.2.7  skrll 	int error;
    879  1.16.2.7  skrll 
    880  1.16.2.7  skrll 	if (ireq->i_len != sizeof(ik))
    881  1.16.2.7  skrll 		return EINVAL;
    882  1.16.2.7  skrll 	error = copyin(ireq->i_data, &ik, sizeof(ik));
    883  1.16.2.7  skrll 	if (error)
    884  1.16.2.7  skrll 		return error;
    885  1.16.2.7  skrll 	kid = ik.ik_keyix;
    886  1.16.2.7  skrll 	if (kid == IEEE80211_KEYIX_NONE) {
    887  1.16.2.7  skrll 		ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
    888  1.16.2.7  skrll 		if (ni == NULL)
    889  1.16.2.7  skrll 			return EINVAL;		/* XXX */
    890  1.16.2.7  skrll 		wk = &ni->ni_ucastkey;
    891  1.16.2.7  skrll 	} else {
    892  1.16.2.7  skrll 		if (kid >= IEEE80211_WEP_NKID)
    893  1.16.2.7  skrll 			return EINVAL;
    894  1.16.2.7  skrll 		wk = &ic->ic_nw_keys[kid];
    895  1.16.2.7  skrll 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, ic->ic_bss->ni_macaddr);
    896  1.16.2.7  skrll 		ni = NULL;
    897  1.16.2.7  skrll 	}
    898  1.16.2.7  skrll 	cip = wk->wk_cipher;
    899  1.16.2.7  skrll 	ik.ik_type = cip->ic_cipher;
    900  1.16.2.7  skrll 	ik.ik_keylen = wk->wk_keylen;
    901  1.16.2.7  skrll 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
    902  1.16.2.7  skrll 	if (wk->wk_keyix == ic->ic_def_txkey)
    903  1.16.2.7  skrll 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
    904  1.16.2.7  skrll 	if (suser(curproc->p_ucred, &curproc->p_acflag) == 0) {
    905  1.16.2.7  skrll 		/* NB: only root can read key data */
    906  1.16.2.7  skrll 		ik.ik_keyrsc = wk->wk_keyrsc;
    907  1.16.2.7  skrll 		ik.ik_keytsc = wk->wk_keytsc;
    908  1.16.2.7  skrll 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
    909  1.16.2.7  skrll 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
    910  1.16.2.7  skrll 			memcpy(ik.ik_keydata+wk->wk_keylen,
    911  1.16.2.7  skrll 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
    912  1.16.2.7  skrll 				IEEE80211_MICBUF_SIZE);
    913  1.16.2.7  skrll 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
    914  1.16.2.7  skrll 		}
    915  1.16.2.7  skrll 	} else {
    916  1.16.2.7  skrll 		ik.ik_keyrsc = 0;
    917  1.16.2.7  skrll 		ik.ik_keytsc = 0;
    918  1.16.2.7  skrll 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
    919  1.16.2.7  skrll 	}
    920  1.16.2.7  skrll 	if (ni != NULL)
    921  1.16.2.7  skrll 		ieee80211_free_node(ni);
    922  1.16.2.7  skrll 	return copyout(&ik, ireq->i_data, sizeof(ik));
    923  1.16.2.7  skrll }
    924  1.16.2.7  skrll 
    925  1.16.2.7  skrll static int
    926  1.16.2.7  skrll ieee80211_ioctl_getchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
    927  1.16.2.7  skrll {
    928  1.16.2.7  skrll 
    929  1.16.2.7  skrll 	if (sizeof(ic->ic_chan_active) > ireq->i_len)
    930  1.16.2.7  skrll 		ireq->i_len = sizeof(ic->ic_chan_active);
    931  1.16.2.7  skrll 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
    932  1.16.2.7  skrll }
    933  1.16.2.7  skrll 
    934  1.16.2.7  skrll static int
    935  1.16.2.7  skrll ieee80211_ioctl_getchaninfo(struct ieee80211com *ic, struct ieee80211req *ireq)
    936  1.16.2.7  skrll {
    937  1.16.2.7  skrll 	struct ieee80211req_chaninfo chans;	/* XXX off stack? */
    938  1.16.2.7  skrll 	int i, space;
    939  1.16.2.7  skrll 
    940  1.16.2.7  skrll 	/*
    941  1.16.2.7  skrll 	 * Since channel 0 is not available for DS, channel 1
    942  1.16.2.7  skrll 	 * is assigned to LSB on WaveLAN.
    943  1.16.2.7  skrll 	 */
    944  1.16.2.7  skrll 	if (ic->ic_phytype == IEEE80211_T_DS)
    945  1.16.2.7  skrll 		i = 1;
    946  1.16.2.7  skrll 	else
    947  1.16.2.7  skrll 		i = 0;
    948  1.16.2.7  skrll 	memset(&chans, 0, sizeof(chans));
    949  1.16.2.7  skrll 	for (; i <= IEEE80211_CHAN_MAX; i++)
    950  1.16.2.7  skrll 		if (isset(ic->ic_chan_avail, i)) {
    951  1.16.2.7  skrll 			struct ieee80211_channel *c = &ic->ic_channels[i];
    952  1.16.2.7  skrll 			chans.ic_chans[chans.ic_nchans].ic_freq = c->ic_freq;
    953  1.16.2.7  skrll 			chans.ic_chans[chans.ic_nchans].ic_flags = c->ic_flags;
    954  1.16.2.7  skrll 			chans.ic_nchans++;
    955  1.16.2.7  skrll 		}
    956  1.16.2.7  skrll 	space = __offsetof(struct ieee80211req_chaninfo,
    957  1.16.2.7  skrll 			ic_chans[chans.ic_nchans]);
    958  1.16.2.7  skrll 	if (space > ireq->i_len)
    959  1.16.2.7  skrll 		space = ireq->i_len;
    960  1.16.2.7  skrll 	return copyout(&chans, ireq->i_data, space);
    961  1.16.2.7  skrll }
    962  1.16.2.7  skrll 
    963  1.16.2.7  skrll static int
    964  1.16.2.7  skrll ieee80211_ioctl_getwpaie(struct ieee80211com *ic, struct ieee80211req *ireq)
    965  1.16.2.7  skrll {
    966  1.16.2.7  skrll 	struct ieee80211_node *ni;
    967  1.16.2.7  skrll 	struct ieee80211req_wpaie wpaie;
    968  1.16.2.7  skrll 	int error;
    969  1.16.2.7  skrll 
    970  1.16.2.7  skrll 	if (ireq->i_len < IEEE80211_ADDR_LEN)
    971  1.16.2.7  skrll 		return EINVAL;
    972  1.16.2.7  skrll 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
    973  1.16.2.7  skrll 	if (error != 0)
    974  1.16.2.7  skrll 		return error;
    975  1.16.2.7  skrll 	ni = ieee80211_find_node(&ic->ic_sta, wpaie.wpa_macaddr);
    976  1.16.2.7  skrll 	if (ni == NULL)
    977  1.16.2.7  skrll 		return EINVAL;		/* XXX */
    978  1.16.2.7  skrll 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
    979  1.16.2.7  skrll 	if (ni->ni_wpa_ie != NULL) {
    980  1.16.2.7  skrll 		int ielen = ni->ni_wpa_ie[1] + 2;
    981  1.16.2.7  skrll 		if (ielen > sizeof(wpaie.wpa_ie))
    982  1.16.2.7  skrll 			ielen = sizeof(wpaie.wpa_ie);
    983  1.16.2.7  skrll 		memcpy(wpaie.wpa_ie, ni->ni_wpa_ie, ielen);
    984  1.16.2.7  skrll 	}
    985  1.16.2.7  skrll 	ieee80211_free_node(ni);
    986  1.16.2.7  skrll 	if (ireq->i_len > sizeof(wpaie))
    987  1.16.2.7  skrll 		ireq->i_len = sizeof(wpaie);
    988  1.16.2.7  skrll 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
    989  1.16.2.7  skrll }
    990  1.16.2.7  skrll 
    991  1.16.2.7  skrll static int
    992  1.16.2.7  skrll ieee80211_ioctl_getstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
    993  1.16.2.7  skrll {
    994  1.16.2.7  skrll 	struct ieee80211_node *ni;
    995  1.16.2.7  skrll 	u_int8_t macaddr[IEEE80211_ADDR_LEN];
    996  1.16.2.7  skrll 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
    997  1.16.2.7  skrll 	int error;
    998  1.16.2.7  skrll 
    999  1.16.2.7  skrll 	if (ireq->i_len < off)
   1000  1.16.2.7  skrll 		return EINVAL;
   1001  1.16.2.7  skrll 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
   1002  1.16.2.7  skrll 	if (error != 0)
   1003  1.16.2.7  skrll 		return error;
   1004  1.16.2.7  skrll 	ni = ieee80211_find_node(&ic->ic_sta, macaddr);
   1005  1.16.2.7  skrll 	if (ni == NULL)
   1006  1.16.2.7  skrll 		return EINVAL;		/* XXX */
   1007  1.16.2.7  skrll 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
   1008  1.16.2.7  skrll 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
   1009  1.16.2.7  skrll 	/* NB: copy out only the statistics */
   1010  1.16.2.7  skrll 	error = copyout(&ni->ni_stats, (u_int8_t *) ireq->i_data + off,
   1011  1.16.2.7  skrll 			ireq->i_len - off);
   1012  1.16.2.7  skrll 	ieee80211_free_node(ni);
   1013  1.16.2.7  skrll 	return error;
   1014  1.16.2.7  skrll }
   1015  1.16.2.7  skrll 
   1016  1.16.2.7  skrll static void
   1017  1.16.2.7  skrll get_scan_result(struct ieee80211req_scan_result *sr,
   1018  1.16.2.7  skrll 	const struct ieee80211_node *ni)
   1019  1.16.2.7  skrll {
   1020  1.16.2.7  skrll 	struct ieee80211com *ic = ni->ni_ic;
   1021  1.16.2.7  skrll 
   1022  1.16.2.7  skrll 	memset(sr, 0, sizeof(*sr));
   1023  1.16.2.7  skrll 	sr->isr_ssid_len = ni->ni_esslen;
   1024  1.16.2.7  skrll 	if (ni->ni_wpa_ie != NULL)
   1025  1.16.2.7  skrll 		sr->isr_ie_len += 2+ni->ni_wpa_ie[1];
   1026  1.16.2.7  skrll 	if (ni->ni_wme_ie != NULL)
   1027  1.16.2.7  skrll 		sr->isr_ie_len += 2+ni->ni_wme_ie[1];
   1028  1.16.2.7  skrll 	sr->isr_len = sizeof(*sr) + sr->isr_ssid_len + sr->isr_ie_len;
   1029  1.16.2.7  skrll 	sr->isr_len = roundup(sr->isr_len, sizeof(u_int32_t));
   1030  1.16.2.7  skrll 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
   1031  1.16.2.7  skrll 		sr->isr_freq = ni->ni_chan->ic_freq;
   1032  1.16.2.7  skrll 		sr->isr_flags = ni->ni_chan->ic_flags;
   1033  1.16.2.7  skrll 	}
   1034  1.16.2.7  skrll 	sr->isr_rssi = ic->ic_node_getrssi(ni);
   1035  1.16.2.7  skrll 	sr->isr_intval = ni->ni_intval;
   1036  1.16.2.7  skrll 	sr->isr_capinfo = ni->ni_capinfo;
   1037  1.16.2.7  skrll 	sr->isr_erp = ni->ni_erp;
   1038  1.16.2.7  skrll 	IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
   1039  1.16.2.7  skrll 	sr->isr_nrates = ni->ni_rates.rs_nrates;
   1040  1.16.2.7  skrll 	if (sr->isr_nrates > 15)
   1041  1.16.2.7  skrll 		sr->isr_nrates = 15;
   1042  1.16.2.7  skrll 	memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
   1043  1.16.2.7  skrll }
   1044  1.16.2.7  skrll 
   1045  1.16.2.7  skrll static int
   1046  1.16.2.7  skrll ieee80211_ioctl_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
   1047  1.16.2.7  skrll {
   1048  1.16.2.7  skrll 	union {
   1049  1.16.2.7  skrll 		struct ieee80211req_scan_result res;
   1050  1.16.2.7  skrll 		char data[512];		/* XXX shrink? */
   1051  1.16.2.7  skrll 	} u;
   1052  1.16.2.7  skrll 	struct ieee80211req_scan_result *sr = &u.res;
   1053  1.16.2.7  skrll 	struct ieee80211_node_table *nt;
   1054  1.16.2.7  skrll 	struct ieee80211_node *ni;
   1055  1.16.2.7  skrll 	int error, space;
   1056  1.16.2.7  skrll 	u_int8_t *p, *cp;
   1057  1.16.2.7  skrll 
   1058  1.16.2.7  skrll 	p = ireq->i_data;
   1059  1.16.2.7  skrll 	space = ireq->i_len;
   1060  1.16.2.7  skrll 	error = 0;
   1061  1.16.2.7  skrll 	/* XXX locking */
   1062  1.16.2.7  skrll 	nt =  &ic->ic_scan;
   1063  1.16.2.7  skrll 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1064  1.16.2.7  skrll 		/* NB: skip pre-scan node state */
   1065  1.16.2.7  skrll 		if (ni->ni_chan == IEEE80211_CHAN_ANYC)
   1066  1.16.2.7  skrll 			continue;
   1067  1.16.2.7  skrll 		get_scan_result(sr, ni);
   1068  1.16.2.7  skrll 		if (sr->isr_len > sizeof(u))
   1069  1.16.2.7  skrll 			continue;		/* XXX */
   1070  1.16.2.7  skrll 		if (space < sr->isr_len)
   1071  1.16.2.7  skrll 			break;
   1072  1.16.2.7  skrll 		cp = (u_int8_t *)(sr+1);
   1073  1.16.2.7  skrll 		memcpy(cp, ni->ni_essid, ni->ni_esslen);
   1074  1.16.2.7  skrll 		cp += ni->ni_esslen;
   1075  1.16.2.7  skrll 		if (ni->ni_wpa_ie != NULL) {
   1076  1.16.2.7  skrll 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
   1077  1.16.2.7  skrll 			cp += 2+ni->ni_wpa_ie[1];
   1078  1.16.2.7  skrll 		}
   1079  1.16.2.7  skrll 		if (ni->ni_wme_ie != NULL) {
   1080  1.16.2.7  skrll 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
   1081  1.16.2.7  skrll 			cp += 2+ni->ni_wme_ie[1];
   1082  1.16.2.7  skrll 		}
   1083  1.16.2.7  skrll 		error = copyout(sr, p, sr->isr_len);
   1084  1.16.2.7  skrll 		if (error)
   1085  1.16.2.7  skrll 			break;
   1086  1.16.2.7  skrll 		p += sr->isr_len;
   1087  1.16.2.7  skrll 		space -= sr->isr_len;
   1088  1.16.2.7  skrll 	}
   1089  1.16.2.7  skrll 	ireq->i_len -= space;
   1090  1.16.2.7  skrll 	return error;
   1091  1.16.2.7  skrll }
   1092  1.16.2.7  skrll 
   1093  1.16.2.7  skrll static void
   1094  1.16.2.7  skrll get_sta_info(struct ieee80211req_sta_info *si, const struct ieee80211_node *ni)
   1095  1.16.2.7  skrll {
   1096  1.16.2.7  skrll 	struct ieee80211com *ic = ni->ni_ic;
   1097  1.16.2.7  skrll 
   1098  1.16.2.7  skrll 	si->isi_ie_len = 0;
   1099  1.16.2.7  skrll 	if (ni->ni_wpa_ie != NULL)
   1100  1.16.2.7  skrll 		si->isi_ie_len += 2+ni->ni_wpa_ie[1];
   1101  1.16.2.7  skrll 	if (ni->ni_wme_ie != NULL)
   1102  1.16.2.7  skrll 		si->isi_ie_len += 2+ni->ni_wme_ie[1];
   1103  1.16.2.7  skrll 	si->isi_len = sizeof(*si) + si->isi_ie_len, sizeof(u_int32_t);
   1104  1.16.2.7  skrll 	si->isi_len = roundup(si->isi_len, sizeof(u_int32_t));
   1105  1.16.2.7  skrll 	si->isi_freq = ni->ni_chan->ic_freq;
   1106  1.16.2.7  skrll 	si->isi_flags = ni->ni_chan->ic_flags;
   1107  1.16.2.7  skrll 	si->isi_state = ni->ni_flags;
   1108  1.16.2.7  skrll 	si->isi_authmode = ni->ni_authmode;
   1109  1.16.2.7  skrll 	si->isi_rssi = ic->ic_node_getrssi(ni);
   1110  1.16.2.7  skrll 	si->isi_capinfo = ni->ni_capinfo;
   1111  1.16.2.7  skrll 	si->isi_erp = ni->ni_erp;
   1112  1.16.2.7  skrll 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
   1113  1.16.2.7  skrll 	si->isi_nrates = ni->ni_rates.rs_nrates;
   1114  1.16.2.7  skrll 	if (si->isi_nrates > 15)
   1115  1.16.2.7  skrll 		si->isi_nrates = 15;
   1116  1.16.2.7  skrll 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
   1117  1.16.2.7  skrll 	si->isi_txrate = ni->ni_txrate;
   1118  1.16.2.7  skrll 	si->isi_associd = ni->ni_associd;
   1119  1.16.2.7  skrll 	si->isi_txpower = ni->ni_txpower;
   1120  1.16.2.7  skrll 	si->isi_vlan = ni->ni_vlan;
   1121  1.16.2.7  skrll 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
   1122  1.16.2.7  skrll 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
   1123  1.16.2.7  skrll 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
   1124  1.16.2.7  skrll 	} else {
   1125  1.16.2.7  skrll 		si->isi_txseqs[0] = ni->ni_txseqs[0];
   1126  1.16.2.7  skrll 		si->isi_rxseqs[0] = ni->ni_rxseqs[0];
   1127  1.16.2.7  skrll 	}
   1128  1.16.2.7  skrll 	if (ic->ic_opmode == IEEE80211_M_IBSS || ni->ni_associd != 0)
   1129  1.16.2.7  skrll 		si->isi_inact = ic->ic_inact_run;
   1130  1.16.2.7  skrll 	else if (ieee80211_node_is_authorized(ni))
   1131  1.16.2.7  skrll 		si->isi_inact = ic->ic_inact_auth;
   1132  1.16.2.7  skrll 	else
   1133  1.16.2.7  skrll 		si->isi_inact = ic->ic_inact_init;
   1134  1.16.2.7  skrll 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
   1135  1.16.2.7  skrll }
   1136  1.16.2.7  skrll 
   1137  1.16.2.7  skrll static int
   1138  1.16.2.7  skrll ieee80211_ioctl_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
   1139  1.16.2.7  skrll {
   1140  1.16.2.7  skrll 	union {
   1141  1.16.2.7  skrll 		struct ieee80211req_sta_info info;
   1142  1.16.2.7  skrll 		char data[512];		/* XXX shrink? */
   1143  1.16.2.7  skrll 	} u;
   1144  1.16.2.7  skrll 	struct ieee80211req_sta_info *si = &u.info;
   1145  1.16.2.7  skrll 	struct ieee80211_node_table *nt;
   1146  1.16.2.7  skrll 	struct ieee80211_node *ni;
   1147  1.16.2.7  skrll 	int error, space;
   1148  1.16.2.7  skrll 	u_int8_t *p, *cp;
   1149  1.16.2.7  skrll 
   1150  1.16.2.7  skrll 	nt = &ic->ic_sta;
   1151  1.16.2.7  skrll 	p = ireq->i_data;
   1152  1.16.2.7  skrll 	space = ireq->i_len;
   1153  1.16.2.7  skrll 	error = 0;
   1154  1.16.2.7  skrll 	/* XXX locking */
   1155  1.16.2.7  skrll 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
   1156  1.16.2.7  skrll 		get_sta_info(si, ni);
   1157  1.16.2.7  skrll 		if (si->isi_len > sizeof(u))
   1158  1.16.2.7  skrll 			continue;		/* XXX */
   1159  1.16.2.7  skrll 		if (space < si->isi_len)
   1160  1.16.2.7  skrll 			break;
   1161  1.16.2.7  skrll 		cp = (u_int8_t *)(si+1);
   1162  1.16.2.7  skrll 		if (ni->ni_wpa_ie != NULL) {
   1163  1.16.2.7  skrll 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
   1164  1.16.2.7  skrll 			cp += 2+ni->ni_wpa_ie[1];
   1165  1.16.2.7  skrll 		}
   1166  1.16.2.7  skrll 		if (ni->ni_wme_ie != NULL) {
   1167  1.16.2.7  skrll 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
   1168  1.16.2.7  skrll 			cp += 2+ni->ni_wme_ie[1];
   1169  1.16.2.7  skrll 		}
   1170  1.16.2.7  skrll 		error = copyout(si, p, si->isi_len);
   1171  1.16.2.7  skrll 		if (error)
   1172  1.16.2.7  skrll 			break;
   1173  1.16.2.7  skrll 		p += si->isi_len;
   1174  1.16.2.7  skrll 		space -= si->isi_len;
   1175  1.16.2.7  skrll 	}
   1176  1.16.2.7  skrll 	ireq->i_len -= space;
   1177  1.16.2.7  skrll 	return error;
   1178  1.16.2.7  skrll }
   1179  1.16.2.7  skrll 
   1180  1.16.2.7  skrll static int
   1181  1.16.2.7  skrll ieee80211_ioctl_getstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
   1182  1.16.2.7  skrll {
   1183  1.16.2.7  skrll 	struct ieee80211_node *ni;
   1184  1.16.2.7  skrll 	struct ieee80211req_sta_txpow txpow;
   1185  1.16.2.7  skrll 	int error;
   1186  1.16.2.7  skrll 
   1187  1.16.2.7  skrll 	if (ireq->i_len != sizeof(txpow))
   1188  1.16.2.7  skrll 		return EINVAL;
   1189  1.16.2.7  skrll 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
   1190  1.16.2.7  skrll 	if (error != 0)
   1191  1.16.2.7  skrll 		return error;
   1192  1.16.2.7  skrll 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
   1193  1.16.2.7  skrll 	if (ni == NULL)
   1194  1.16.2.7  skrll 		return EINVAL;		/* XXX */
   1195  1.16.2.7  skrll 	txpow.it_txpow = ni->ni_txpower;
   1196  1.16.2.7  skrll 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
   1197  1.16.2.7  skrll 	ieee80211_free_node(ni);
   1198  1.16.2.7  skrll 	return error;
   1199  1.16.2.7  skrll }
   1200  1.16.2.7  skrll 
   1201  1.16.2.7  skrll static int
   1202  1.16.2.7  skrll ieee80211_ioctl_getwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
   1203  1.16.2.7  skrll {
   1204  1.16.2.7  skrll 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1205  1.16.2.7  skrll 	struct wmeParams *wmep;
   1206  1.16.2.7  skrll 	int ac;
   1207  1.16.2.7  skrll 
   1208  1.16.2.7  skrll 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   1209  1.16.2.7  skrll 		return EINVAL;
   1210  1.16.2.7  skrll 
   1211  1.16.2.7  skrll 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
   1212  1.16.2.7  skrll 	if (ac >= WME_NUM_AC)
   1213  1.16.2.7  skrll 		ac = WME_AC_BE;
   1214  1.16.2.7  skrll 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
   1215  1.16.2.7  skrll 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1216  1.16.2.7  skrll 	else
   1217  1.16.2.7  skrll 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1218  1.16.2.7  skrll 	switch (ireq->i_type) {
   1219  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1220  1.16.2.7  skrll 		ireq->i_val = wmep->wmep_logcwmin;
   1221  1.16.2.7  skrll 		break;
   1222  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1223  1.16.2.7  skrll 		ireq->i_val = wmep->wmep_logcwmax;
   1224  1.16.2.7  skrll 		break;
   1225  1.16.2.7  skrll 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1226  1.16.2.7  skrll 		ireq->i_val = wmep->wmep_aifsn;
   1227  1.16.2.7  skrll 		break;
   1228  1.16.2.7  skrll 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1229  1.16.2.7  skrll 		ireq->i_val = wmep->wmep_txopLimit;
   1230  1.16.2.7  skrll 		break;
   1231  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1232  1.16.2.7  skrll 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1233  1.16.2.7  skrll 		ireq->i_val = wmep->wmep_acm;
   1234  1.16.2.7  skrll 		break;
   1235  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
   1236  1.16.2.7  skrll 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1237  1.16.2.7  skrll 		ireq->i_val = !wmep->wmep_noackPolicy;
   1238  1.16.2.7  skrll 		break;
   1239  1.16.2.7  skrll 	}
   1240  1.16.2.7  skrll 	return 0;
   1241  1.16.2.7  skrll }
   1242  1.16.2.7  skrll 
   1243  1.16.2.7  skrll /*
   1244  1.16.2.7  skrll  * When building the kernel with -O2 on the i386 architecture, gcc
   1245  1.16.2.7  skrll  * seems to want to inline this function into ieee80211_ioctl()
   1246  1.16.2.7  skrll  * (which is the only routine that calls it). When this happens,
   1247  1.16.2.7  skrll  * ieee80211_ioctl() ends up consuming an additional 2K of stack
   1248  1.16.2.7  skrll  * space. (Exactly why it needs so much is unclear.) The problem
   1249  1.16.2.7  skrll  * is that it's possible for ieee80211_ioctl() to invoke other
   1250  1.16.2.7  skrll  * routines (including driver init functions) which could then find
   1251  1.16.2.7  skrll  * themselves perilously close to exhausting the stack.
   1252  1.16.2.7  skrll  *
   1253  1.16.2.7  skrll  * To avoid this, we deliberately prevent gcc from inlining this
   1254  1.16.2.7  skrll  * routine. Another way to avoid this is to use less agressive
   1255  1.16.2.7  skrll  * optimization when compiling this file (i.e. -O instead of -O2)
   1256  1.16.2.7  skrll  * but special-casing the compilation of this one module in the
   1257  1.16.2.7  skrll  * build system would be awkward.
   1258  1.16.2.7  skrll  */
   1259  1.16.2.7  skrll #ifdef __GNUC__
   1260  1.16.2.7  skrll __attribute__ ((noinline))
   1261  1.16.2.7  skrll #endif
   1262  1.16.2.7  skrll static int
   1263  1.16.2.7  skrll ieee80211_ioctl_get80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
   1264  1.16.2.2  skrll {
   1265  1.16.2.7  skrll 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
   1266  1.16.2.2  skrll 	int error = 0;
   1267  1.16.2.7  skrll #ifdef __FreeBSD__
   1268  1.16.2.2  skrll 	u_int kid, len;
   1269  1.16.2.2  skrll 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
   1270  1.16.2.2  skrll 	char tmpssid[IEEE80211_NWID_LEN];
   1271  1.16.2.7  skrll #endif /* __FreeBSD__ */
   1272  1.16.2.7  skrll 	u_int m;
   1273  1.16.2.2  skrll 
   1274  1.16.2.7  skrll 	switch (ireq->i_type) {
   1275  1.16.2.7  skrll #ifdef __FreeBSD__
   1276  1.16.2.7  skrll 	case IEEE80211_IOC_SSID:
   1277  1.16.2.7  skrll 		switch (ic->ic_state) {
   1278  1.16.2.7  skrll 		case IEEE80211_S_INIT:
   1279  1.16.2.7  skrll 		case IEEE80211_S_SCAN:
   1280  1.16.2.7  skrll 			ireq->i_len = ic->ic_des_esslen;
   1281  1.16.2.7  skrll 			memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
   1282  1.16.2.2  skrll 			break;
   1283  1.16.2.7  skrll 		default:
   1284  1.16.2.7  skrll 			ireq->i_len = ic->ic_bss->ni_esslen;
   1285  1.16.2.7  skrll 			memcpy(tmpssid, ic->ic_bss->ni_essid,
   1286  1.16.2.7  skrll 				ireq->i_len);
   1287  1.16.2.2  skrll 			break;
   1288  1.16.2.7  skrll 		}
   1289  1.16.2.7  skrll 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
   1290  1.16.2.7  skrll 		break;
   1291  1.16.2.7  skrll 	case IEEE80211_IOC_NUMSSIDS:
   1292  1.16.2.7  skrll 		ireq->i_val = 1;
   1293  1.16.2.7  skrll 		break;
   1294  1.16.2.7  skrll 	case IEEE80211_IOC_WEP:
   1295  1.16.2.7  skrll 		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
   1296  1.16.2.7  skrll 			ireq->i_val = IEEE80211_WEP_OFF;
   1297  1.16.2.7  skrll 		else if (ic->ic_flags & IEEE80211_F_DROPUNENC)
   1298  1.16.2.7  skrll 			ireq->i_val = IEEE80211_WEP_ON;
   1299  1.16.2.7  skrll 		else
   1300  1.16.2.7  skrll 			ireq->i_val = IEEE80211_WEP_MIXED;
   1301  1.16.2.7  skrll 		break;
   1302  1.16.2.7  skrll 	case IEEE80211_IOC_WEPKEY:
   1303  1.16.2.7  skrll 		kid = (u_int) ireq->i_val;
   1304  1.16.2.7  skrll 		if (kid >= IEEE80211_WEP_NKID)
   1305  1.16.2.7  skrll 			return EINVAL;
   1306  1.16.2.7  skrll 		len = (u_int) ic->ic_nw_keys[kid].wk_keylen;
   1307  1.16.2.7  skrll 		/* NB: only root can read WEP keys */
   1308  1.16.2.7  skrll 		if (suser(curproc->p_ucred, &curproc->p_acflag) == 0) {
   1309  1.16.2.7  skrll 			bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
   1310  1.16.2.7  skrll 		} else {
   1311  1.16.2.7  skrll 			bzero(tmpkey, len);
   1312  1.16.2.7  skrll 		}
   1313  1.16.2.7  skrll 		ireq->i_len = len;
   1314  1.16.2.7  skrll 		error = copyout(tmpkey, ireq->i_data, len);
   1315  1.16.2.7  skrll 		break;
   1316  1.16.2.7  skrll 	case IEEE80211_IOC_NUMWEPKEYS:
   1317  1.16.2.7  skrll 		ireq->i_val = IEEE80211_WEP_NKID;
   1318  1.16.2.7  skrll 		break;
   1319  1.16.2.7  skrll 	case IEEE80211_IOC_WEPTXKEY:
   1320  1.16.2.7  skrll 		ireq->i_val = ic->ic_def_txkey;
   1321  1.16.2.7  skrll 		break;
   1322  1.16.2.7  skrll #endif /* __FreeBSD__ */
   1323  1.16.2.7  skrll 	case IEEE80211_IOC_AUTHMODE:
   1324  1.16.2.7  skrll 		if (ic->ic_flags & IEEE80211_F_WPA)
   1325  1.16.2.7  skrll 			ireq->i_val = IEEE80211_AUTH_WPA;
   1326  1.16.2.7  skrll 		else
   1327  1.16.2.7  skrll 			ireq->i_val = ic->ic_bss->ni_authmode;
   1328  1.16.2.7  skrll 		break;
   1329  1.16.2.7  skrll #ifdef __FreeBSD__
   1330  1.16.2.7  skrll 	case IEEE80211_IOC_CHANNEL:
   1331  1.16.2.7  skrll 		ireq->i_val = ieee80211_chan2ieee(ic, getcurchan(ic));
   1332  1.16.2.7  skrll 		break;
   1333  1.16.2.7  skrll 	case IEEE80211_IOC_POWERSAVE:
   1334  1.16.2.7  skrll 		if (ic->ic_flags & IEEE80211_F_PMGTON)
   1335  1.16.2.7  skrll 			ireq->i_val = IEEE80211_POWERSAVE_ON;
   1336  1.16.2.7  skrll 		else
   1337  1.16.2.7  skrll 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
   1338  1.16.2.7  skrll 		break;
   1339  1.16.2.7  skrll 	case IEEE80211_IOC_POWERSAVESLEEP:
   1340  1.16.2.7  skrll 		ireq->i_val = ic->ic_lintval;
   1341  1.16.2.7  skrll 		break;
   1342  1.16.2.7  skrll #endif /* __FreeBSD__ */
   1343  1.16.2.7  skrll 	case IEEE80211_IOC_RTSTHRESHOLD:
   1344  1.16.2.7  skrll 		ireq->i_val = ic->ic_rtsthreshold;
   1345  1.16.2.7  skrll 		break;
   1346  1.16.2.7  skrll 	case IEEE80211_IOC_PROTMODE:
   1347  1.16.2.7  skrll 		ireq->i_val = ic->ic_protmode;
   1348  1.16.2.7  skrll 		break;
   1349  1.16.2.7  skrll 	case IEEE80211_IOC_TXPOWER:
   1350  1.16.2.7  skrll 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
   1351  1.16.2.7  skrll 			return EINVAL;
   1352  1.16.2.7  skrll 		ireq->i_val = ic->ic_txpowlimit;
   1353  1.16.2.7  skrll 		break;
   1354  1.16.2.7  skrll 	case IEEE80211_IOC_MCASTCIPHER:
   1355  1.16.2.7  skrll 		ireq->i_val = rsn->rsn_mcastcipher;
   1356  1.16.2.7  skrll 		break;
   1357  1.16.2.7  skrll 	case IEEE80211_IOC_MCASTKEYLEN:
   1358  1.16.2.7  skrll 		ireq->i_val = rsn->rsn_mcastkeylen;
   1359  1.16.2.7  skrll 		break;
   1360  1.16.2.7  skrll 	case IEEE80211_IOC_UCASTCIPHERS:
   1361  1.16.2.7  skrll 		ireq->i_val = 0;
   1362  1.16.2.7  skrll 		for (m = 0x1; m != 0; m <<= 1)
   1363  1.16.2.7  skrll 			if (rsn->rsn_ucastcipherset & m)
   1364  1.16.2.7  skrll 				ireq->i_val |= 1<<cap2cipher(m);
   1365  1.16.2.7  skrll 		break;
   1366  1.16.2.7  skrll 	case IEEE80211_IOC_UCASTCIPHER:
   1367  1.16.2.7  skrll 		ireq->i_val = rsn->rsn_ucastcipher;
   1368  1.16.2.7  skrll 		break;
   1369  1.16.2.7  skrll 	case IEEE80211_IOC_UCASTKEYLEN:
   1370  1.16.2.7  skrll 		ireq->i_val = rsn->rsn_ucastkeylen;
   1371  1.16.2.7  skrll 		break;
   1372  1.16.2.7  skrll 	case IEEE80211_IOC_KEYMGTALGS:
   1373  1.16.2.7  skrll 		ireq->i_val = rsn->rsn_keymgmtset;
   1374  1.16.2.7  skrll 		break;
   1375  1.16.2.7  skrll 	case IEEE80211_IOC_RSNCAPS:
   1376  1.16.2.7  skrll 		ireq->i_val = rsn->rsn_caps;
   1377  1.16.2.7  skrll 		break;
   1378  1.16.2.7  skrll 	case IEEE80211_IOC_WPA:
   1379  1.16.2.7  skrll 		switch (ic->ic_flags & IEEE80211_F_WPA) {
   1380  1.16.2.7  skrll 		case IEEE80211_F_WPA1:
   1381  1.16.2.7  skrll 			ireq->i_val = 1;
   1382  1.16.2.2  skrll 			break;
   1383  1.16.2.7  skrll 		case IEEE80211_F_WPA2:
   1384  1.16.2.7  skrll 			ireq->i_val = 2;
   1385  1.16.2.2  skrll 			break;
   1386  1.16.2.7  skrll 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
   1387  1.16.2.7  skrll 			ireq->i_val = 3;
   1388  1.16.2.2  skrll 			break;
   1389  1.16.2.2  skrll 		default:
   1390  1.16.2.7  skrll 			ireq->i_val = 0;
   1391  1.16.2.2  skrll 			break;
   1392  1.16.2.2  skrll 		}
   1393  1.16.2.2  skrll 		break;
   1394  1.16.2.7  skrll 	case IEEE80211_IOC_CHANLIST:
   1395  1.16.2.7  skrll 		error = ieee80211_ioctl_getchanlist(ic, ireq);
   1396  1.16.2.7  skrll 		break;
   1397  1.16.2.7  skrll 	case IEEE80211_IOC_ROAMING:
   1398  1.16.2.7  skrll 		ireq->i_val = ic->ic_roaming;
   1399  1.16.2.7  skrll 		break;
   1400  1.16.2.7  skrll 	case IEEE80211_IOC_PRIVACY:
   1401  1.16.2.7  skrll 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PRIVACY) != 0;
   1402  1.16.2.7  skrll 		break;
   1403  1.16.2.7  skrll 	case IEEE80211_IOC_DROPUNENCRYPTED:
   1404  1.16.2.7  skrll 		ireq->i_val = (ic->ic_flags & IEEE80211_F_DROPUNENC) != 0;
   1405  1.16.2.7  skrll 		break;
   1406  1.16.2.7  skrll 	case IEEE80211_IOC_COUNTERMEASURES:
   1407  1.16.2.7  skrll 		ireq->i_val = (ic->ic_flags & IEEE80211_F_COUNTERM) != 0;
   1408  1.16.2.7  skrll 		break;
   1409  1.16.2.7  skrll 	case IEEE80211_IOC_DRIVER_CAPS:
   1410  1.16.2.7  skrll 		ireq->i_val = ic->ic_caps>>16;
   1411  1.16.2.7  skrll 		ireq->i_len = ic->ic_caps&0xffff;
   1412  1.16.2.7  skrll 		break;
   1413  1.16.2.7  skrll 	case IEEE80211_IOC_WME:
   1414  1.16.2.7  skrll 		ireq->i_val = (ic->ic_flags & IEEE80211_F_WME) != 0;
   1415  1.16.2.7  skrll 		break;
   1416  1.16.2.7  skrll 	case IEEE80211_IOC_HIDESSID:
   1417  1.16.2.7  skrll 		ireq->i_val = (ic->ic_flags & IEEE80211_F_HIDESSID) != 0;
   1418  1.16.2.7  skrll 		break;
   1419  1.16.2.7  skrll 	case IEEE80211_IOC_APBRIDGE:
   1420  1.16.2.7  skrll 		ireq->i_val = (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0;
   1421  1.16.2.7  skrll 		break;
   1422  1.16.2.7  skrll 	case IEEE80211_IOC_OPTIE:
   1423  1.16.2.7  skrll 		if (ic->ic_opt_ie == NULL)
   1424  1.16.2.7  skrll 			return EINVAL;
   1425  1.16.2.7  skrll 		/* NB: truncate, caller can check length */
   1426  1.16.2.7  skrll 		if (ireq->i_len > ic->ic_opt_ie_len)
   1427  1.16.2.7  skrll 			ireq->i_len = ic->ic_opt_ie_len;
   1428  1.16.2.7  skrll 		error = copyout(ic->ic_opt_ie, ireq->i_data, ireq->i_len);
   1429  1.16.2.7  skrll 		break;
   1430  1.16.2.7  skrll 	case IEEE80211_IOC_WPAKEY:
   1431  1.16.2.7  skrll 		error = ieee80211_ioctl_getkey(ic, ireq);
   1432  1.16.2.7  skrll 		break;
   1433  1.16.2.7  skrll 	case IEEE80211_IOC_CHANINFO:
   1434  1.16.2.7  skrll 		error = ieee80211_ioctl_getchaninfo(ic, ireq);
   1435  1.16.2.7  skrll 		break;
   1436  1.16.2.7  skrll #ifdef __FreeBSD__
   1437  1.16.2.7  skrll 	case IEEE80211_IOC_BSSID:
   1438  1.16.2.7  skrll 		if (ireq->i_len != IEEE80211_ADDR_LEN)
   1439  1.16.2.7  skrll 			return EINVAL;
   1440  1.16.2.7  skrll 		error = copyout(ic->ic_state == IEEE80211_S_RUN ?
   1441  1.16.2.7  skrll 					ic->ic_bss->ni_bssid :
   1442  1.16.2.7  skrll 					ic->ic_des_bssid,
   1443  1.16.2.7  skrll 				ireq->i_data, ireq->i_len);
   1444  1.16.2.7  skrll 		break;
   1445  1.16.2.7  skrll #endif /* __FreeBSD__ */
   1446  1.16.2.7  skrll 	case IEEE80211_IOC_WPAIE:
   1447  1.16.2.7  skrll 		error = ieee80211_ioctl_getwpaie(ic, ireq);
   1448  1.16.2.7  skrll 		break;
   1449  1.16.2.7  skrll 	case IEEE80211_IOC_SCAN_RESULTS:
   1450  1.16.2.7  skrll 		error = ieee80211_ioctl_getscanresults(ic, ireq);
   1451  1.16.2.7  skrll 		break;
   1452  1.16.2.7  skrll 	case IEEE80211_IOC_STA_STATS:
   1453  1.16.2.7  skrll 		error = ieee80211_ioctl_getstastats(ic, ireq);
   1454  1.16.2.7  skrll 		break;
   1455  1.16.2.7  skrll 	case IEEE80211_IOC_TXPOWMAX:
   1456  1.16.2.7  skrll 		ireq->i_val = ic->ic_bss->ni_txpower;
   1457  1.16.2.7  skrll 		break;
   1458  1.16.2.7  skrll 	case IEEE80211_IOC_STA_TXPOW:
   1459  1.16.2.7  skrll 		error = ieee80211_ioctl_getstatxpow(ic, ireq);
   1460  1.16.2.7  skrll 		break;
   1461  1.16.2.7  skrll 	case IEEE80211_IOC_STA_INFO:
   1462  1.16.2.7  skrll 		error = ieee80211_ioctl_getstainfo(ic, ireq);
   1463  1.16.2.7  skrll 		break;
   1464  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1465  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1466  1.16.2.7  skrll 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1467  1.16.2.7  skrll 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1468  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1469  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
   1470  1.16.2.7  skrll 		error = ieee80211_ioctl_getwmeparam(ic, ireq);
   1471  1.16.2.7  skrll 		break;
   1472  1.16.2.7  skrll 	case IEEE80211_IOC_DTIM_PERIOD:
   1473  1.16.2.7  skrll 		ireq->i_val = ic->ic_dtim_period;
   1474  1.16.2.7  skrll 		break;
   1475  1.16.2.7  skrll 	case IEEE80211_IOC_BEACON_INTERVAL:
   1476  1.16.2.7  skrll 		/* NB: get from ic_bss for station mode */
   1477  1.16.2.7  skrll 		ireq->i_val = ic->ic_bss->ni_intval;
   1478  1.16.2.7  skrll 		break;
   1479  1.16.2.7  skrll 	case IEEE80211_IOC_PUREG:
   1480  1.16.2.7  skrll 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PUREG) != 0;
   1481  1.16.2.7  skrll 		break;
   1482  1.16.2.7  skrll 	default:
   1483  1.16.2.7  skrll 		error = EINVAL;
   1484  1.16.2.7  skrll 		break;
   1485  1.16.2.7  skrll 	}
   1486  1.16.2.7  skrll 	return error;
   1487  1.16.2.7  skrll }
   1488  1.16.2.7  skrll 
   1489  1.16.2.7  skrll static int
   1490  1.16.2.7  skrll ieee80211_ioctl_setoptie(struct ieee80211com *ic, struct ieee80211req *ireq)
   1491  1.16.2.7  skrll {
   1492  1.16.2.7  skrll 	int error;
   1493  1.16.2.7  skrll 	void *ie;
   1494  1.16.2.7  skrll 
   1495  1.16.2.7  skrll 	/*
   1496  1.16.2.7  skrll 	 * NB: Doing this for ap operation could be useful (e.g. for
   1497  1.16.2.7  skrll 	 *     WPA and/or WME) except that it typically is worthless
   1498  1.16.2.7  skrll 	 *     without being able to intervene when processing
   1499  1.16.2.7  skrll 	 *     association response frames--so disallow it for now.
   1500  1.16.2.7  skrll 	 */
   1501  1.16.2.7  skrll 	if (ic->ic_opmode != IEEE80211_M_STA)
   1502  1.16.2.7  skrll 		return EINVAL;
   1503  1.16.2.7  skrll 	if (ireq->i_len > IEEE80211_MAX_OPT_IE)
   1504  1.16.2.7  skrll 		return EINVAL;
   1505  1.16.2.7  skrll 	/* NB: data.length is validated by the wireless extensions code */
   1506  1.16.2.7  skrll 	MALLOC(ie, void *, (u_long)ireq->i_len, M_DEVBUF, M_WAITOK);
   1507  1.16.2.7  skrll 	if (ie == NULL)
   1508  1.16.2.7  skrll 		return ENOMEM;
   1509  1.16.2.7  skrll 	error = copyin(ireq->i_data, ie, ireq->i_len);
   1510  1.16.2.7  skrll 	/* XXX sanity check data? */
   1511  1.16.2.7  skrll 	if (ic->ic_opt_ie != NULL)
   1512  1.16.2.7  skrll 		FREE(ic->ic_opt_ie, M_DEVBUF);
   1513  1.16.2.7  skrll 	ic->ic_opt_ie = ie;
   1514  1.16.2.7  skrll 	ic->ic_opt_ie_len = ireq->i_len;
   1515  1.16.2.7  skrll 	return 0;
   1516  1.16.2.7  skrll }
   1517  1.16.2.7  skrll 
   1518  1.16.2.7  skrll static int
   1519  1.16.2.7  skrll ieee80211_ioctl_setkey(struct ieee80211com *ic, struct ieee80211req *ireq)
   1520  1.16.2.7  skrll {
   1521  1.16.2.7  skrll 	struct ieee80211req_key ik;
   1522  1.16.2.7  skrll 	struct ieee80211_node *ni;
   1523  1.16.2.7  skrll 	struct ieee80211_key *wk;
   1524  1.16.2.7  skrll 	u_int16_t kid;
   1525  1.16.2.7  skrll 	int error;
   1526  1.16.2.7  skrll 
   1527  1.16.2.7  skrll 	if (ireq->i_len != sizeof(ik))
   1528  1.16.2.7  skrll 		return EINVAL;
   1529  1.16.2.7  skrll 	error = copyin(ireq->i_data, &ik, sizeof(ik));
   1530  1.16.2.7  skrll 	if (error)
   1531  1.16.2.7  skrll 		return error;
   1532  1.16.2.7  skrll 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
   1533  1.16.2.7  skrll 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
   1534  1.16.2.7  skrll 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
   1535  1.16.2.7  skrll 		return E2BIG;
   1536  1.16.2.7  skrll 	kid = ik.ik_keyix;
   1537  1.16.2.7  skrll 	if (kid == IEEE80211_KEYIX_NONE) {
   1538  1.16.2.7  skrll 		/* XXX unicast keys currently must be tx/rx */
   1539  1.16.2.7  skrll 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
   1540  1.16.2.7  skrll 			return EINVAL;
   1541  1.16.2.7  skrll 		if (ic->ic_opmode == IEEE80211_M_STA) {
   1542  1.16.2.7  skrll 			ni = ieee80211_ref_node(ic->ic_bss);
   1543  1.16.2.7  skrll 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
   1544  1.16.2.7  skrll 				ieee80211_free_node(ni);
   1545  1.16.2.7  skrll 				return EADDRNOTAVAIL;
   1546  1.16.2.2  skrll 			}
   1547  1.16.2.7  skrll 		} else {
   1548  1.16.2.7  skrll 			ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
   1549  1.16.2.7  skrll 			if (ni == NULL)
   1550  1.16.2.7  skrll 				return ENOENT;
   1551  1.16.2.7  skrll 		}
   1552  1.16.2.7  skrll 		wk = &ni->ni_ucastkey;
   1553  1.16.2.7  skrll 	} else {
   1554  1.16.2.7  skrll 		if (kid >= IEEE80211_WEP_NKID)
   1555  1.16.2.7  skrll 			return EINVAL;
   1556  1.16.2.7  skrll 		wk = &ic->ic_nw_keys[kid];
   1557  1.16.2.7  skrll 		ni = NULL;
   1558  1.16.2.7  skrll 	}
   1559  1.16.2.7  skrll 	error = 0;
   1560  1.16.2.7  skrll 	ieee80211_key_update_begin(ic);
   1561  1.16.2.7  skrll 	if (ieee80211_crypto_newkey(ic, ik.ik_type, ik.ik_flags, wk)) {
   1562  1.16.2.7  skrll 		wk->wk_keylen = ik.ik_keylen;
   1563  1.16.2.7  skrll 		/* NB: MIC presence is implied by cipher type */
   1564  1.16.2.7  skrll 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
   1565  1.16.2.7  skrll 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
   1566  1.16.2.7  skrll 		wk->wk_keyrsc = ik.ik_keyrsc;
   1567  1.16.2.7  skrll 		wk->wk_keytsc = 0;			/* new key, reset */
   1568  1.16.2.7  skrll 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
   1569  1.16.2.7  skrll 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
   1570  1.16.2.7  skrll 		if (!ieee80211_crypto_setkey(ic, wk,
   1571  1.16.2.7  skrll 		    ni != NULL ? ni->ni_macaddr : ik.ik_macaddr))
   1572  1.16.2.7  skrll 			error = EIO;
   1573  1.16.2.7  skrll 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
   1574  1.16.2.7  skrll 			ic->ic_def_txkey = kid;
   1575  1.16.2.7  skrll 	} else
   1576  1.16.2.7  skrll 		error = ENXIO;
   1577  1.16.2.7  skrll 	ieee80211_key_update_end(ic);
   1578  1.16.2.7  skrll 	if (ni != NULL)
   1579  1.16.2.7  skrll 		ieee80211_free_node(ni);
   1580  1.16.2.7  skrll 	return error;
   1581  1.16.2.7  skrll }
   1582  1.16.2.7  skrll 
   1583  1.16.2.7  skrll static int
   1584  1.16.2.7  skrll ieee80211_ioctl_delkey(struct ieee80211com *ic, struct ieee80211req *ireq)
   1585  1.16.2.7  skrll {
   1586  1.16.2.7  skrll 	struct ieee80211req_del_key dk;
   1587  1.16.2.7  skrll 	int kid, error;
   1588  1.16.2.7  skrll 
   1589  1.16.2.7  skrll 	if (ireq->i_len != sizeof(dk))
   1590  1.16.2.7  skrll 		return EINVAL;
   1591  1.16.2.7  skrll 	error = copyin(ireq->i_data, &dk, sizeof(dk));
   1592  1.16.2.7  skrll 	if (error)
   1593  1.16.2.7  skrll 		return error;
   1594  1.16.2.7  skrll 	kid = dk.idk_keyix;
   1595  1.16.2.7  skrll 	/* XXX u_int8_t -> u_int16_t */
   1596  1.16.2.7  skrll 	if (dk.idk_keyix == (u_int8_t) IEEE80211_KEYIX_NONE) {
   1597  1.16.2.7  skrll 		struct ieee80211_node *ni;
   1598  1.16.2.7  skrll 
   1599  1.16.2.7  skrll 		if (ic->ic_opmode == IEEE80211_M_STA) {
   1600  1.16.2.7  skrll 			ni = ieee80211_ref_node(ic->ic_bss);
   1601  1.16.2.7  skrll 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
   1602  1.16.2.7  skrll 				ieee80211_free_node(ni);
   1603  1.16.2.7  skrll 				return EADDRNOTAVAIL;
   1604  1.16.2.7  skrll 			}
   1605  1.16.2.7  skrll 		} else {
   1606  1.16.2.7  skrll 			ni = ieee80211_find_node(&ic->ic_sta, dk.idk_macaddr);
   1607  1.16.2.7  skrll 			if (ni == NULL)
   1608  1.16.2.7  skrll 				return ENOENT;
   1609  1.16.2.7  skrll 		}
   1610  1.16.2.7  skrll 		/* XXX error return */
   1611  1.16.2.7  skrll 		ieee80211_crypto_delkey(ic, &ni->ni_ucastkey);
   1612  1.16.2.7  skrll 		ieee80211_free_node(ni);
   1613  1.16.2.7  skrll 	} else {
   1614  1.16.2.7  skrll 		if (kid >= IEEE80211_WEP_NKID)
   1615  1.16.2.7  skrll 			return EINVAL;
   1616  1.16.2.7  skrll 		/* XXX error return */
   1617  1.16.2.7  skrll 		ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[kid]);
   1618  1.16.2.7  skrll 	}
   1619  1.16.2.7  skrll 	return 0;
   1620  1.16.2.7  skrll }
   1621  1.16.2.7  skrll 
   1622  1.16.2.7  skrll #ifndef IEEE80211_NO_HOSTAP
   1623  1.16.2.7  skrll static void
   1624  1.16.2.7  skrll domlme(void *arg, struct ieee80211_node *ni)
   1625  1.16.2.7  skrll {
   1626  1.16.2.7  skrll 	struct ieee80211com *ic = ni->ni_ic;
   1627  1.16.2.7  skrll 	struct ieee80211req_mlme *mlme = arg;
   1628  1.16.2.7  skrll 
   1629  1.16.2.7  skrll 	if (ni->ni_associd != 0) {
   1630  1.16.2.7  skrll 		IEEE80211_SEND_MGMT(ic, ni,
   1631  1.16.2.7  skrll 			mlme->im_op == IEEE80211_MLME_DEAUTH ?
   1632  1.16.2.7  skrll 				IEEE80211_FC0_SUBTYPE_DEAUTH :
   1633  1.16.2.7  skrll 				IEEE80211_FC0_SUBTYPE_DISASSOC,
   1634  1.16.2.7  skrll 			mlme->im_reason);
   1635  1.16.2.7  skrll 	}
   1636  1.16.2.7  skrll 	ieee80211_node_leave(ic, ni);
   1637  1.16.2.7  skrll }
   1638  1.16.2.7  skrll #endif /* !IEEE80211_NO_HOSTAP */
   1639  1.16.2.7  skrll 
   1640  1.16.2.7  skrll static int
   1641  1.16.2.7  skrll ieee80211_ioctl_setmlme(struct ieee80211com *ic, struct ieee80211req *ireq)
   1642  1.16.2.7  skrll {
   1643  1.16.2.7  skrll 	struct ieee80211req_mlme mlme;
   1644  1.16.2.7  skrll 	struct ieee80211_node *ni;
   1645  1.16.2.7  skrll 	int error;
   1646  1.16.2.7  skrll 
   1647  1.16.2.7  skrll 	if (ireq->i_len != sizeof(mlme))
   1648  1.16.2.7  skrll 		return EINVAL;
   1649  1.16.2.7  skrll 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
   1650  1.16.2.7  skrll 	if (error)
   1651  1.16.2.7  skrll 		return error;
   1652  1.16.2.7  skrll 	switch (mlme.im_op) {
   1653  1.16.2.7  skrll 	case IEEE80211_MLME_ASSOC:
   1654  1.16.2.7  skrll 		if (ic->ic_opmode != IEEE80211_M_STA)
   1655  1.16.2.7  skrll 			return EINVAL;
   1656  1.16.2.7  skrll 		/* XXX must be in S_SCAN state? */
   1657  1.16.2.7  skrll 
   1658  1.16.2.7  skrll 		if (mlme.im_ssid_len != 0) {
   1659  1.16.2.2  skrll 			/*
   1660  1.16.2.7  skrll 			 * Desired ssid specified; must match both bssid and
   1661  1.16.2.7  skrll 			 * ssid to distinguish ap advertising multiple ssid's.
   1662  1.16.2.2  skrll 			 */
   1663  1.16.2.7  skrll 			ni = ieee80211_find_node_with_ssid(&ic->ic_scan,
   1664  1.16.2.7  skrll 				mlme.im_macaddr,
   1665  1.16.2.7  skrll 				mlme.im_ssid_len, mlme.im_ssid);
   1666  1.16.2.7  skrll 		} else {
   1667  1.16.2.7  skrll 			/*
   1668  1.16.2.7  skrll 			 * Normal case; just match bssid.
   1669  1.16.2.7  skrll 			 */
   1670  1.16.2.7  skrll 			ni = ieee80211_find_node(&ic->ic_scan, mlme.im_macaddr);
   1671  1.16.2.7  skrll 		}
   1672  1.16.2.7  skrll 		if (ni == NULL)
   1673  1.16.2.7  skrll 			return EINVAL;
   1674  1.16.2.7  skrll 		if (!ieee80211_sta_join(ic, ni)) {
   1675  1.16.2.7  skrll 			ieee80211_free_node(ni);
   1676  1.16.2.7  skrll 			return EINVAL;
   1677  1.16.2.7  skrll 		}
   1678  1.16.2.7  skrll 		break;
   1679  1.16.2.7  skrll 	case IEEE80211_MLME_DISASSOC:
   1680  1.16.2.7  skrll 	case IEEE80211_MLME_DEAUTH:
   1681  1.16.2.7  skrll 		switch (ic->ic_opmode) {
   1682  1.16.2.7  skrll 		case IEEE80211_M_STA:
   1683  1.16.2.7  skrll 			/* XXX not quite right */
   1684  1.16.2.7  skrll 			ieee80211_new_state(ic, IEEE80211_S_INIT,
   1685  1.16.2.7  skrll 				mlme.im_reason);
   1686  1.16.2.7  skrll 			break;
   1687  1.16.2.7  skrll 		case IEEE80211_M_HOSTAP:
   1688  1.16.2.7  skrll #ifndef IEEE80211_NO_HOSTAP
   1689  1.16.2.7  skrll 			/* NB: the broadcast address means do 'em all */
   1690  1.16.2.7  skrll 			if (!IEEE80211_ADDR_EQ(mlme.im_macaddr, ic->ic_ifp->if_broadcastaddr)) {
   1691  1.16.2.7  skrll 				if ((ni = ieee80211_find_node(&ic->ic_sta,
   1692  1.16.2.7  skrll 						mlme.im_macaddr)) == NULL)
   1693  1.16.2.7  skrll 					return EINVAL;
   1694  1.16.2.7  skrll 				domlme(&mlme, ni);
   1695  1.16.2.7  skrll 				ieee80211_free_node(ni);
   1696  1.16.2.2  skrll 			} else {
   1697  1.16.2.7  skrll 				ieee80211_iterate_nodes(&ic->ic_sta,
   1698  1.16.2.7  skrll 						domlme, &mlme);
   1699  1.16.2.2  skrll 			}
   1700  1.16.2.7  skrll #endif /* !IEEE80211_NO_HOSTAP */
   1701  1.16.2.2  skrll 			break;
   1702  1.16.2.7  skrll 		default:
   1703  1.16.2.7  skrll 			return EINVAL;
   1704  1.16.2.7  skrll 		}
   1705  1.16.2.7  skrll 		break;
   1706  1.16.2.7  skrll 	case IEEE80211_MLME_AUTHORIZE:
   1707  1.16.2.7  skrll 	case IEEE80211_MLME_UNAUTHORIZE:
   1708  1.16.2.7  skrll 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
   1709  1.16.2.7  skrll 			return EINVAL;
   1710  1.16.2.7  skrll 		ni = ieee80211_find_node(&ic->ic_sta, mlme.im_macaddr);
   1711  1.16.2.7  skrll 		if (ni == NULL)
   1712  1.16.2.7  skrll 			return EINVAL;
   1713  1.16.2.7  skrll 		if (mlme.im_op == IEEE80211_MLME_AUTHORIZE)
   1714  1.16.2.7  skrll 			ieee80211_node_authorize(ic, ni);
   1715  1.16.2.7  skrll 		else
   1716  1.16.2.7  skrll 			ieee80211_node_unauthorize(ic, ni);
   1717  1.16.2.7  skrll 		ieee80211_free_node(ni);
   1718  1.16.2.7  skrll 		break;
   1719  1.16.2.7  skrll 	default:
   1720  1.16.2.7  skrll 		return EINVAL;
   1721  1.16.2.7  skrll 	}
   1722  1.16.2.7  skrll 	return 0;
   1723  1.16.2.7  skrll }
   1724  1.16.2.7  skrll 
   1725  1.16.2.7  skrll static int
   1726  1.16.2.7  skrll ieee80211_ioctl_macmac(struct ieee80211com *ic, struct ieee80211req *ireq)
   1727  1.16.2.7  skrll {
   1728  1.16.2.7  skrll 	u_int8_t mac[IEEE80211_ADDR_LEN];
   1729  1.16.2.7  skrll 	const struct ieee80211_aclator *acl = ic->ic_acl;
   1730  1.16.2.7  skrll 	int error;
   1731  1.16.2.7  skrll 
   1732  1.16.2.7  skrll 	if (ireq->i_len != sizeof(mac))
   1733  1.16.2.7  skrll 		return EINVAL;
   1734  1.16.2.7  skrll 	error = copyin(ireq->i_data, mac, ireq->i_len);
   1735  1.16.2.7  skrll 	if (error)
   1736  1.16.2.7  skrll 		return error;
   1737  1.16.2.7  skrll 	if (acl == NULL) {
   1738  1.16.2.7  skrll 		acl = ieee80211_aclator_get("mac");
   1739  1.16.2.7  skrll 		if (acl == NULL || !acl->iac_attach(ic))
   1740  1.16.2.7  skrll 			return EINVAL;
   1741  1.16.2.7  skrll 		ic->ic_acl = acl;
   1742  1.16.2.7  skrll 	}
   1743  1.16.2.7  skrll 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
   1744  1.16.2.7  skrll 		acl->iac_add(ic, mac);
   1745  1.16.2.7  skrll 	else
   1746  1.16.2.7  skrll 		acl->iac_remove(ic, mac);
   1747  1.16.2.7  skrll 	return 0;
   1748  1.16.2.7  skrll }
   1749  1.16.2.7  skrll 
   1750  1.16.2.7  skrll static int
   1751  1.16.2.7  skrll ieee80211_ioctl_maccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
   1752  1.16.2.7  skrll {
   1753  1.16.2.7  skrll 	const struct ieee80211_aclator *acl = ic->ic_acl;
   1754  1.16.2.7  skrll 
   1755  1.16.2.7  skrll 	switch (ireq->i_val) {
   1756  1.16.2.7  skrll 	case IEEE80211_MACCMD_POLICY_OPEN:
   1757  1.16.2.7  skrll 	case IEEE80211_MACCMD_POLICY_ALLOW:
   1758  1.16.2.7  skrll 	case IEEE80211_MACCMD_POLICY_DENY:
   1759  1.16.2.7  skrll 		if (acl == NULL) {
   1760  1.16.2.7  skrll 			acl = ieee80211_aclator_get("mac");
   1761  1.16.2.7  skrll 			if (acl == NULL || !acl->iac_attach(ic))
   1762  1.16.2.7  skrll 				return EINVAL;
   1763  1.16.2.7  skrll 			ic->ic_acl = acl;
   1764  1.16.2.7  skrll 		}
   1765  1.16.2.7  skrll 		acl->iac_setpolicy(ic, ireq->i_val);
   1766  1.16.2.7  skrll 		break;
   1767  1.16.2.7  skrll 	case IEEE80211_MACCMD_FLUSH:
   1768  1.16.2.7  skrll 		if (acl != NULL)
   1769  1.16.2.7  skrll 			acl->iac_flush(ic);
   1770  1.16.2.7  skrll 		/* NB: silently ignore when not in use */
   1771  1.16.2.7  skrll 		break;
   1772  1.16.2.7  skrll 	case IEEE80211_MACCMD_DETACH:
   1773  1.16.2.7  skrll 		if (acl != NULL) {
   1774  1.16.2.7  skrll 			ic->ic_acl = NULL;
   1775  1.16.2.7  skrll 			acl->iac_detach(ic);
   1776  1.16.2.7  skrll 		}
   1777  1.16.2.7  skrll 		break;
   1778  1.16.2.7  skrll 	default:
   1779  1.16.2.7  skrll 		return EINVAL;
   1780  1.16.2.7  skrll 	}
   1781  1.16.2.7  skrll 	return 0;
   1782  1.16.2.7  skrll }
   1783  1.16.2.7  skrll 
   1784  1.16.2.7  skrll static int
   1785  1.16.2.7  skrll ieee80211_ioctl_setchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
   1786  1.16.2.7  skrll {
   1787  1.16.2.7  skrll 	struct ieee80211req_chanlist list;
   1788  1.16.2.7  skrll 	u_char chanlist[IEEE80211_CHAN_BYTES];
   1789  1.16.2.7  skrll 	int i, j, error;
   1790  1.16.2.7  skrll 
   1791  1.16.2.7  skrll 	if (ireq->i_len != sizeof(list))
   1792  1.16.2.7  skrll 		return EINVAL;
   1793  1.16.2.7  skrll 	error = copyin(ireq->i_data, &list, sizeof(list));
   1794  1.16.2.7  skrll 	if (error)
   1795  1.16.2.7  skrll 		return error;
   1796  1.16.2.7  skrll 	memset(chanlist, 0, sizeof(chanlist));
   1797  1.16.2.7  skrll 	/*
   1798  1.16.2.7  skrll 	 * Since channel 0 is not available for DS, channel 1
   1799  1.16.2.7  skrll 	 * is assigned to LSB on WaveLAN.
   1800  1.16.2.7  skrll 	 */
   1801  1.16.2.7  skrll 	if (ic->ic_phytype == IEEE80211_T_DS)
   1802  1.16.2.7  skrll 		i = 1;
   1803  1.16.2.7  skrll 	else
   1804  1.16.2.7  skrll 		i = 0;
   1805  1.16.2.7  skrll 	for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
   1806  1.16.2.7  skrll 		/*
   1807  1.16.2.7  skrll 		 * NB: silently discard unavailable channels so users
   1808  1.16.2.7  skrll 		 *     can specify 1-255 to get all available channels.
   1809  1.16.2.7  skrll 		 */
   1810  1.16.2.7  skrll 		if (isset(list.ic_channels, j) && isset(ic->ic_chan_avail, i))
   1811  1.16.2.7  skrll 			setbit(chanlist, i);
   1812  1.16.2.7  skrll 	}
   1813  1.16.2.7  skrll 	if (ic->ic_ibss_chan == NULL ||
   1814  1.16.2.7  skrll 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
   1815  1.16.2.7  skrll 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
   1816  1.16.2.7  skrll 			if (isset(chanlist, i)) {
   1817  1.16.2.7  skrll 				ic->ic_ibss_chan = &ic->ic_channels[i];
   1818  1.16.2.7  skrll 				goto found;
   1819  1.16.2.2  skrll 			}
   1820  1.16.2.7  skrll 		return EINVAL;			/* no active channels */
   1821  1.16.2.7  skrll found:
   1822  1.16.2.7  skrll 		;
   1823  1.16.2.7  skrll 	}
   1824  1.16.2.7  skrll 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
   1825  1.16.2.7  skrll 	if (ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC ||
   1826  1.16.2.7  skrll 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan)))
   1827  1.16.2.7  skrll 		ic->ic_bss->ni_chan = ic->ic_ibss_chan;
   1828  1.16.2.7  skrll 	return IS_UP_AUTO(ic) ? ENETRESET : 0;
   1829  1.16.2.7  skrll }
   1830  1.16.2.7  skrll 
   1831  1.16.2.7  skrll static int
   1832  1.16.2.7  skrll ieee80211_ioctl_setstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
   1833  1.16.2.7  skrll {
   1834  1.16.2.7  skrll 	struct ieee80211_node *ni;
   1835  1.16.2.7  skrll 	struct ieee80211req_sta_txpow txpow;
   1836  1.16.2.7  skrll 	int error;
   1837  1.16.2.7  skrll 
   1838  1.16.2.7  skrll 	if (ireq->i_len != sizeof(txpow))
   1839  1.16.2.7  skrll 		return EINVAL;
   1840  1.16.2.7  skrll 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
   1841  1.16.2.7  skrll 	if (error != 0)
   1842  1.16.2.7  skrll 		return error;
   1843  1.16.2.7  skrll 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
   1844  1.16.2.7  skrll 	if (ni == NULL)
   1845  1.16.2.7  skrll 		return EINVAL;		/* XXX */
   1846  1.16.2.7  skrll 	ni->ni_txpower = txpow.it_txpow;
   1847  1.16.2.7  skrll 	ieee80211_free_node(ni);
   1848  1.16.2.7  skrll 	return error;
   1849  1.16.2.7  skrll }
   1850  1.16.2.7  skrll 
   1851  1.16.2.7  skrll static int
   1852  1.16.2.7  skrll ieee80211_ioctl_setwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
   1853  1.16.2.7  skrll {
   1854  1.16.2.7  skrll 	struct ieee80211_wme_state *wme = &ic->ic_wme;
   1855  1.16.2.7  skrll 	struct wmeParams *wmep, *chanp;
   1856  1.16.2.7  skrll 	int isbss, ac;
   1857  1.16.2.7  skrll 
   1858  1.16.2.7  skrll 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   1859  1.16.2.7  skrll 		return EINVAL;
   1860  1.16.2.7  skrll 
   1861  1.16.2.7  skrll 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
   1862  1.16.2.7  skrll 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
   1863  1.16.2.7  skrll 	if (ac >= WME_NUM_AC)
   1864  1.16.2.7  skrll 		ac = WME_AC_BE;
   1865  1.16.2.7  skrll 	if (isbss) {
   1866  1.16.2.7  skrll 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
   1867  1.16.2.7  skrll 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
   1868  1.16.2.7  skrll 	} else {
   1869  1.16.2.7  skrll 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
   1870  1.16.2.7  skrll 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
   1871  1.16.2.7  skrll 	}
   1872  1.16.2.7  skrll 	switch (ireq->i_type) {
   1873  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   1874  1.16.2.7  skrll 		if (isbss) {
   1875  1.16.2.7  skrll 			wmep->wmep_logcwmin = ireq->i_val;
   1876  1.16.2.7  skrll 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1877  1.16.2.7  skrll 				chanp->wmep_logcwmin = ireq->i_val;
   1878  1.16.2.7  skrll 		} else {
   1879  1.16.2.7  skrll 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
   1880  1.16.2.7  skrll 				ireq->i_val;
   1881  1.16.2.7  skrll 		}
   1882  1.16.2.7  skrll 		break;
   1883  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   1884  1.16.2.7  skrll 		if (isbss) {
   1885  1.16.2.7  skrll 			wmep->wmep_logcwmax = ireq->i_val;
   1886  1.16.2.7  skrll 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1887  1.16.2.7  skrll 				chanp->wmep_logcwmax = ireq->i_val;
   1888  1.16.2.7  skrll 		} else {
   1889  1.16.2.7  skrll 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
   1890  1.16.2.7  skrll 				ireq->i_val;
   1891  1.16.2.7  skrll 		}
   1892  1.16.2.7  skrll 		break;
   1893  1.16.2.7  skrll 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   1894  1.16.2.7  skrll 		if (isbss) {
   1895  1.16.2.7  skrll 			wmep->wmep_aifsn = ireq->i_val;
   1896  1.16.2.7  skrll 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1897  1.16.2.7  skrll 				chanp->wmep_aifsn = ireq->i_val;
   1898  1.16.2.7  skrll 		} else {
   1899  1.16.2.7  skrll 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
   1900  1.16.2.7  skrll 		}
   1901  1.16.2.7  skrll 		break;
   1902  1.16.2.7  skrll 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   1903  1.16.2.7  skrll 		if (isbss) {
   1904  1.16.2.7  skrll 			wmep->wmep_txopLimit = ireq->i_val;
   1905  1.16.2.7  skrll 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1906  1.16.2.7  skrll 				chanp->wmep_txopLimit = ireq->i_val;
   1907  1.16.2.7  skrll 		} else {
   1908  1.16.2.7  skrll 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
   1909  1.16.2.7  skrll 				ireq->i_val;
   1910  1.16.2.7  skrll 		}
   1911  1.16.2.7  skrll 		break;
   1912  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   1913  1.16.2.7  skrll 		wmep->wmep_acm = ireq->i_val;
   1914  1.16.2.7  skrll 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
   1915  1.16.2.7  skrll 			chanp->wmep_acm = ireq->i_val;
   1916  1.16.2.7  skrll 		break;
   1917  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
   1918  1.16.2.7  skrll 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
   1919  1.16.2.7  skrll 			(ireq->i_val) == 0;
   1920  1.16.2.7  skrll 		break;
   1921  1.16.2.7  skrll 	}
   1922  1.16.2.7  skrll 	ieee80211_wme_updateparams(ic);
   1923  1.16.2.7  skrll 	return 0;
   1924  1.16.2.7  skrll }
   1925  1.16.2.7  skrll 
   1926  1.16.2.7  skrll static int
   1927  1.16.2.7  skrll cipher2cap(int cipher)
   1928  1.16.2.7  skrll {
   1929  1.16.2.7  skrll 	switch (cipher) {
   1930  1.16.2.7  skrll 	case IEEE80211_CIPHER_WEP:	return IEEE80211_C_WEP;
   1931  1.16.2.7  skrll 	case IEEE80211_CIPHER_AES_OCB:	return IEEE80211_C_AES;
   1932  1.16.2.7  skrll 	case IEEE80211_CIPHER_AES_CCM:	return IEEE80211_C_AES_CCM;
   1933  1.16.2.7  skrll 	case IEEE80211_CIPHER_CKIP:	return IEEE80211_C_CKIP;
   1934  1.16.2.7  skrll 	case IEEE80211_CIPHER_TKIP:	return IEEE80211_C_TKIP;
   1935  1.16.2.7  skrll 	}
   1936  1.16.2.7  skrll 	return 0;
   1937  1.16.2.7  skrll }
   1938  1.16.2.7  skrll 
   1939  1.16.2.7  skrll static int
   1940  1.16.2.7  skrll ieee80211_ioctl_set80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
   1941  1.16.2.7  skrll {
   1942  1.16.2.7  skrll #ifdef __FreeBSD__
   1943  1.16.2.7  skrll 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
   1944  1.16.2.7  skrll 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
   1945  1.16.2.7  skrll 	char tmpssid[IEEE80211_NWID_LEN];
   1946  1.16.2.7  skrll 	u_int8_t tmpbssid[IEEE80211_ADDR_LEN];
   1947  1.16.2.7  skrll 	struct ieee80211_key *k;
   1948  1.16.2.7  skrll 	u_int kid;
   1949  1.16.2.7  skrll #endif /* __FreeBSD__ */
   1950  1.16.2.7  skrll 	struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
   1951  1.16.2.7  skrll 	int error;
   1952  1.16.2.7  skrll 	const struct ieee80211_authenticator *auth;
   1953  1.16.2.7  skrll 	int j, caps;
   1954  1.16.2.7  skrll 
   1955  1.16.2.7  skrll 	error = 0;
   1956  1.16.2.7  skrll 	switch (ireq->i_type) {
   1957  1.16.2.7  skrll #ifdef __FreeBSD__
   1958  1.16.2.7  skrll 	case IEEE80211_IOC_SSID:
   1959  1.16.2.7  skrll 		if (ireq->i_val != 0 ||
   1960  1.16.2.7  skrll 		    ireq->i_len > IEEE80211_NWID_LEN)
   1961  1.16.2.7  skrll 			return EINVAL;
   1962  1.16.2.7  skrll 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
   1963  1.16.2.7  skrll 		if (error)
   1964  1.16.2.7  skrll 			break;
   1965  1.16.2.7  skrll 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
   1966  1.16.2.7  skrll 		ic->ic_des_esslen = ireq->i_len;
   1967  1.16.2.7  skrll 		memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
   1968  1.16.2.7  skrll 		error = ENETRESET;
   1969  1.16.2.7  skrll 		break;
   1970  1.16.2.7  skrll #endif /* __FreeBSD__ */
   1971  1.16.2.7  skrll 	case IEEE80211_IOC_WEP:
   1972  1.16.2.7  skrll 		switch (ireq->i_val) {
   1973  1.16.2.7  skrll 		case IEEE80211_WEP_OFF:
   1974  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   1975  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   1976  1.16.2.7  skrll 			break;
   1977  1.16.2.7  skrll 		case IEEE80211_WEP_ON:
   1978  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   1979  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   1980  1.16.2.7  skrll 			break;
   1981  1.16.2.7  skrll 		case IEEE80211_WEP_MIXED:
   1982  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   1983  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   1984  1.16.2.7  skrll 			break;
   1985  1.16.2.7  skrll 		}
   1986  1.16.2.7  skrll 		error = ENETRESET;
   1987  1.16.2.7  skrll 		break;
   1988  1.16.2.7  skrll #ifdef __FreeBSD__
   1989  1.16.2.7  skrll 	case IEEE80211_IOC_WEPKEY:
   1990  1.16.2.7  skrll 		kid = (u_int) ireq->i_val;
   1991  1.16.2.7  skrll 		if (kid >= IEEE80211_WEP_NKID)
   1992  1.16.2.7  skrll 			return EINVAL;
   1993  1.16.2.7  skrll 		k = &ic->ic_nw_keys[kid];
   1994  1.16.2.7  skrll 		if (ireq->i_len == 0) {
   1995  1.16.2.7  skrll 			/* zero-len =>'s delete any existing key */
   1996  1.16.2.7  skrll 			(void) ieee80211_crypto_delkey(ic, k);
   1997  1.16.2.7  skrll 			break;
   1998  1.16.2.7  skrll 		}
   1999  1.16.2.7  skrll 		if (ireq->i_len > sizeof(tmpkey))
   2000  1.16.2.7  skrll 			return EINVAL;
   2001  1.16.2.7  skrll 		memset(tmpkey, 0, sizeof(tmpkey));
   2002  1.16.2.7  skrll 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
   2003  1.16.2.7  skrll 		if (error)
   2004  1.16.2.2  skrll 			break;
   2005  1.16.2.7  skrll 		ieee80211_key_update_begin(ic);
   2006  1.16.2.7  skrll 		k->wk_keyix = kid;	/* NB: force fixed key id */
   2007  1.16.2.7  skrll 		if (ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
   2008  1.16.2.7  skrll 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
   2009  1.16.2.7  skrll 			k->wk_keylen = ireq->i_len;
   2010  1.16.2.7  skrll 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
   2011  1.16.2.7  skrll 			if  (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
   2012  1.16.2.2  skrll 				error = EINVAL;
   2013  1.16.2.7  skrll 		} else
   2014  1.16.2.7  skrll 			error = EINVAL;
   2015  1.16.2.7  skrll 		ieee80211_key_update_end(ic);
   2016  1.16.2.7  skrll 		if (!error)			/* NB: for compatibility */
   2017  1.16.2.2  skrll 			error = ENETRESET;
   2018  1.16.2.7  skrll 		break;
   2019  1.16.2.7  skrll 	case IEEE80211_IOC_WEPTXKEY:
   2020  1.16.2.7  skrll 		kid = (u_int) ireq->i_val;
   2021  1.16.2.7  skrll 		if (kid >= IEEE80211_WEP_NKID &&
   2022  1.16.2.7  skrll 		    (u_int16_t) kid != IEEE80211_KEYIX_NONE)
   2023  1.16.2.7  skrll 			return EINVAL;
   2024  1.16.2.7  skrll 		ic->ic_def_txkey = kid;
   2025  1.16.2.7  skrll 		error = ENETRESET;	/* push to hardware */
   2026  1.16.2.7  skrll 		break;
   2027  1.16.2.7  skrll #endif /* __FreeBSD__ */
   2028  1.16.2.7  skrll 	case IEEE80211_IOC_AUTHMODE:
   2029  1.16.2.7  skrll 		switch (ireq->i_val) {
   2030  1.16.2.7  skrll 		case IEEE80211_AUTH_WPA:
   2031  1.16.2.7  skrll 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2032  1.16.2.7  skrll 		case IEEE80211_AUTH_OPEN:	/* open */
   2033  1.16.2.7  skrll 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2034  1.16.2.7  skrll 		case IEEE80211_AUTH_AUTO:	/* auto */
   2035  1.16.2.7  skrll 			auth = ieee80211_authenticator_get(ireq->i_val);
   2036  1.16.2.7  skrll 			if (auth == NULL)
   2037  1.16.2.7  skrll 				return EINVAL;
   2038  1.16.2.2  skrll 			break;
   2039  1.16.2.7  skrll 		default:
   2040  1.16.2.7  skrll 			return EINVAL;
   2041  1.16.2.7  skrll 		}
   2042  1.16.2.7  skrll 		switch (ireq->i_val) {
   2043  1.16.2.7  skrll 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
   2044  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2045  1.16.2.7  skrll 			ireq->i_val = IEEE80211_AUTH_8021X;
   2046  1.16.2.2  skrll 			break;
   2047  1.16.2.7  skrll 		case IEEE80211_AUTH_OPEN:	/* open */
   2048  1.16.2.7  skrll 			ic->ic_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
   2049  1.16.2.2  skrll 			break;
   2050  1.16.2.7  skrll 		case IEEE80211_AUTH_SHARED:	/* shared-key */
   2051  1.16.2.7  skrll 		case IEEE80211_AUTH_8021X:	/* 802.1x */
   2052  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_WPA;
   2053  1.16.2.7  skrll 			/* both require a key so mark the PRIVACY capability */
   2054  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2055  1.16.2.2  skrll 			break;
   2056  1.16.2.7  skrll 		case IEEE80211_AUTH_AUTO:	/* auto */
   2057  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_WPA;
   2058  1.16.2.7  skrll 			/* XXX PRIVACY handling? */
   2059  1.16.2.7  skrll 			/* XXX what's the right way to do this? */
   2060  1.16.2.2  skrll 			break;
   2061  1.16.2.7  skrll 		}
   2062  1.16.2.7  skrll 		/* NB: authenticator attach/detach happens on state change */
   2063  1.16.2.7  skrll 		ic->ic_bss->ni_authmode = ireq->i_val;
   2064  1.16.2.7  skrll 		/* XXX mixed/mode/usage? */
   2065  1.16.2.7  skrll 		ic->ic_auth = auth;
   2066  1.16.2.7  skrll 		error = ENETRESET;
   2067  1.16.2.7  skrll 		break;
   2068  1.16.2.7  skrll #ifdef __FreeBSD__
   2069  1.16.2.7  skrll 	case IEEE80211_IOC_CHANNEL:
   2070  1.16.2.7  skrll 		/* XXX 0xffff overflows 16-bit signed */
   2071  1.16.2.7  skrll 		if (ireq->i_val == 0 ||
   2072  1.16.2.7  skrll 		    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
   2073  1.16.2.7  skrll 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
   2074  1.16.2.7  skrll 		else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
   2075  1.16.2.7  skrll 		    isclr(ic->ic_chan_active, ireq->i_val)) {
   2076  1.16.2.7  skrll 			return EINVAL;
   2077  1.16.2.7  skrll 		} else
   2078  1.16.2.7  skrll 			ic->ic_ibss_chan = ic->ic_des_chan =
   2079  1.16.2.7  skrll 				&ic->ic_channels[ireq->i_val];
   2080  1.16.2.7  skrll 		switch (ic->ic_state) {
   2081  1.16.2.7  skrll 		case IEEE80211_S_INIT:
   2082  1.16.2.7  skrll 		case IEEE80211_S_SCAN:
   2083  1.16.2.2  skrll 			error = ENETRESET;
   2084  1.16.2.2  skrll 			break;
   2085  1.16.2.7  skrll 		default:
   2086  1.16.2.7  skrll 			/*
   2087  1.16.2.7  skrll 			 * If the desired channel has changed (to something
   2088  1.16.2.7  skrll 			 * other than any) and we're not already scanning,
   2089  1.16.2.7  skrll 			 * then kick the state machine.
   2090  1.16.2.7  skrll 			 */
   2091  1.16.2.7  skrll 			if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
   2092  1.16.2.7  skrll 			    ic->ic_bss->ni_chan != ic->ic_des_chan &&
   2093  1.16.2.7  skrll 			    (ic->ic_flags & IEEE80211_F_SCAN) == 0)
   2094  1.16.2.2  skrll 				error = ENETRESET;
   2095  1.16.2.2  skrll 			break;
   2096  1.16.2.7  skrll 		}
   2097  1.16.2.7  skrll 		if (error == ENETRESET && ic->ic_opmode == IEEE80211_M_MONITOR)
   2098  1.16.2.7  skrll 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2099  1.16.2.7  skrll 		break;
   2100  1.16.2.7  skrll 	case IEEE80211_IOC_POWERSAVE:
   2101  1.16.2.7  skrll 		switch (ireq->i_val) {
   2102  1.16.2.7  skrll 		case IEEE80211_POWERSAVE_OFF:
   2103  1.16.2.7  skrll 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   2104  1.16.2.7  skrll 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   2105  1.16.2.7  skrll 				error = ENETRESET;
   2106  1.16.2.2  skrll 			}
   2107  1.16.2.7  skrll 			break;
   2108  1.16.2.7  skrll 		case IEEE80211_POWERSAVE_ON:
   2109  1.16.2.7  skrll 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
   2110  1.16.2.2  skrll 				error = EINVAL;
   2111  1.16.2.7  skrll 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   2112  1.16.2.7  skrll 				ic->ic_flags |= IEEE80211_F_PMGTON;
   2113  1.16.2.7  skrll 				error = ENETRESET;
   2114  1.16.2.2  skrll 			}
   2115  1.16.2.2  skrll 			break;
   2116  1.16.2.2  skrll 		default:
   2117  1.16.2.2  skrll 			error = EINVAL;
   2118  1.16.2.2  skrll 			break;
   2119  1.16.2.2  skrll 		}
   2120  1.16.2.2  skrll 		break;
   2121  1.16.2.7  skrll 	case IEEE80211_IOC_POWERSAVESLEEP:
   2122  1.16.2.7  skrll 		if (ireq->i_val < 0)
   2123  1.16.2.7  skrll 			return EINVAL;
   2124  1.16.2.7  skrll 		ic->ic_lintval = ireq->i_val;
   2125  1.16.2.7  skrll 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2126  1.16.2.7  skrll 		break;
   2127  1.16.2.7  skrll #endif /* __FreeBSD__ */
   2128  1.16.2.7  skrll 	case IEEE80211_IOC_RTSTHRESHOLD:
   2129  1.16.2.7  skrll 		if (!(IEEE80211_RTS_MIN < ireq->i_val &&
   2130  1.16.2.7  skrll 		      ireq->i_val < IEEE80211_RTS_MAX))
   2131  1.16.2.7  skrll 			return EINVAL;
   2132  1.16.2.7  skrll 		ic->ic_rtsthreshold = ireq->i_val;
   2133  1.16.2.7  skrll 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2134  1.16.2.7  skrll 		break;
   2135  1.16.2.7  skrll 	case IEEE80211_IOC_PROTMODE:
   2136  1.16.2.7  skrll 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
   2137  1.16.2.7  skrll 			return EINVAL;
   2138  1.16.2.7  skrll 		ic->ic_protmode = ireq->i_val;
   2139  1.16.2.7  skrll 		/* NB: if not operating in 11g this can wait */
   2140  1.16.2.7  skrll 		if (ic->ic_curmode == IEEE80211_MODE_11G)
   2141  1.16.2.7  skrll 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2142  1.16.2.7  skrll 		break;
   2143  1.16.2.7  skrll 	case IEEE80211_IOC_TXPOWER:
   2144  1.16.2.7  skrll 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
   2145  1.16.2.7  skrll 			return EINVAL;
   2146  1.16.2.7  skrll 		if (!(IEEE80211_TXPOWER_MIN < ireq->i_val &&
   2147  1.16.2.7  skrll 		      ireq->i_val < IEEE80211_TXPOWER_MAX))
   2148  1.16.2.7  skrll 			return EINVAL;
   2149  1.16.2.7  skrll 		ic->ic_txpowlimit = ireq->i_val;
   2150  1.16.2.7  skrll 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
   2151  1.16.2.7  skrll 		break;
   2152  1.16.2.7  skrll 	case IEEE80211_IOC_ROAMING:
   2153  1.16.2.7  skrll 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
   2154  1.16.2.7  skrll 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
   2155  1.16.2.7  skrll 			return EINVAL;
   2156  1.16.2.7  skrll 		ic->ic_roaming = ireq->i_val;
   2157  1.16.2.7  skrll 		/* XXXX reset? */
   2158  1.16.2.7  skrll 		break;
   2159  1.16.2.7  skrll 	case IEEE80211_IOC_PRIVACY:
   2160  1.16.2.7  skrll 		if (ireq->i_val) {
   2161  1.16.2.7  skrll 			/* XXX check for key state? */
   2162  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2163  1.16.2.7  skrll 		} else
   2164  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   2165  1.16.2.7  skrll 		break;
   2166  1.16.2.7  skrll 	case IEEE80211_IOC_DROPUNENCRYPTED:
   2167  1.16.2.7  skrll 		if (ireq->i_val)
   2168  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   2169  1.16.2.7  skrll 		else
   2170  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   2171  1.16.2.7  skrll 		break;
   2172  1.16.2.7  skrll 	case IEEE80211_IOC_WPAKEY:
   2173  1.16.2.7  skrll 		error = ieee80211_ioctl_setkey(ic, ireq);
   2174  1.16.2.7  skrll 		break;
   2175  1.16.2.7  skrll 	case IEEE80211_IOC_DELKEY:
   2176  1.16.2.7  skrll 		error = ieee80211_ioctl_delkey(ic, ireq);
   2177  1.16.2.7  skrll 		break;
   2178  1.16.2.7  skrll 	case IEEE80211_IOC_MLME:
   2179  1.16.2.7  skrll 		error = ieee80211_ioctl_setmlme(ic, ireq);
   2180  1.16.2.7  skrll 		break;
   2181  1.16.2.7  skrll 	case IEEE80211_IOC_OPTIE:
   2182  1.16.2.7  skrll 		error = ieee80211_ioctl_setoptie(ic, ireq);
   2183  1.16.2.7  skrll 		break;
   2184  1.16.2.7  skrll 	case IEEE80211_IOC_COUNTERMEASURES:
   2185  1.16.2.7  skrll 		if (ireq->i_val) {
   2186  1.16.2.7  skrll 			if ((ic->ic_flags & IEEE80211_F_WPA) == 0)
   2187  1.16.2.7  skrll 				return EINVAL;
   2188  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_COUNTERM;
   2189  1.16.2.7  skrll 		} else
   2190  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_COUNTERM;
   2191  1.16.2.7  skrll 		break;
   2192  1.16.2.7  skrll 	case IEEE80211_IOC_WPA:
   2193  1.16.2.7  skrll 		if (ireq->i_val > 3)
   2194  1.16.2.7  skrll 			return EINVAL;
   2195  1.16.2.7  skrll 		/* XXX verify ciphers available */
   2196  1.16.2.7  skrll 		ic->ic_flags &= ~IEEE80211_F_WPA;
   2197  1.16.2.7  skrll 		switch (ireq->i_val) {
   2198  1.16.2.7  skrll 		case 1:
   2199  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_WPA1;
   2200  1.16.2.7  skrll 			break;
   2201  1.16.2.7  skrll 		case 2:
   2202  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_WPA2;
   2203  1.16.2.7  skrll 			break;
   2204  1.16.2.7  skrll 		case 3:
   2205  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
   2206  1.16.2.7  skrll 			break;
   2207  1.16.2.7  skrll 		}
   2208  1.16.2.7  skrll 		error = ENETRESET;		/* XXX? */
   2209  1.16.2.7  skrll 		break;
   2210  1.16.2.7  skrll 	case IEEE80211_IOC_WME:
   2211  1.16.2.7  skrll 		if (ireq->i_val) {
   2212  1.16.2.7  skrll 			if ((ic->ic_caps & IEEE80211_C_WME) == 0)
   2213  1.16.2.7  skrll 				return EINVAL;
   2214  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_WME;
   2215  1.16.2.7  skrll 		} else
   2216  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_WME;
   2217  1.16.2.7  skrll 		error = ENETRESET;		/* XXX maybe not for station? */
   2218  1.16.2.7  skrll 		break;
   2219  1.16.2.7  skrll 	case IEEE80211_IOC_HIDESSID:
   2220  1.16.2.7  skrll 		if (ireq->i_val)
   2221  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_HIDESSID;
   2222  1.16.2.7  skrll 		else
   2223  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_HIDESSID;
   2224  1.16.2.7  skrll 		error = ENETRESET;
   2225  1.16.2.7  skrll 		break;
   2226  1.16.2.7  skrll 	case IEEE80211_IOC_APBRIDGE:
   2227  1.16.2.7  skrll 		if (ireq->i_val == 0)
   2228  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_NOBRIDGE;
   2229  1.16.2.7  skrll 		else
   2230  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_NOBRIDGE;
   2231  1.16.2.7  skrll 		break;
   2232  1.16.2.7  skrll 	case IEEE80211_IOC_MCASTCIPHER:
   2233  1.16.2.7  skrll 		if ((ic->ic_caps & cipher2cap(ireq->i_val)) == 0 &&
   2234  1.16.2.7  skrll 		    !ieee80211_crypto_available(ireq->i_val))
   2235  1.16.2.7  skrll 			return EINVAL;
   2236  1.16.2.7  skrll 		rsn->rsn_mcastcipher = ireq->i_val;
   2237  1.16.2.7  skrll 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2238  1.16.2.7  skrll 		break;
   2239  1.16.2.7  skrll 	case IEEE80211_IOC_MCASTKEYLEN:
   2240  1.16.2.7  skrll 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
   2241  1.16.2.7  skrll 			return EINVAL;
   2242  1.16.2.7  skrll 		/* XXX no way to verify driver capability */
   2243  1.16.2.7  skrll 		rsn->rsn_mcastkeylen = ireq->i_val;
   2244  1.16.2.7  skrll 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2245  1.16.2.7  skrll 		break;
   2246  1.16.2.7  skrll 	case IEEE80211_IOC_UCASTCIPHERS:
   2247  1.16.2.7  skrll 		/*
   2248  1.16.2.7  skrll 		 * Convert user-specified cipher set to the set
   2249  1.16.2.7  skrll 		 * we can support (via hardware or software).
   2250  1.16.2.7  skrll 		 * NB: this logic intentionally ignores unknown and
   2251  1.16.2.7  skrll 		 * unsupported ciphers so folks can specify 0xff or
   2252  1.16.2.7  skrll 		 * similar and get all available ciphers.
   2253  1.16.2.7  skrll 		 */
   2254  1.16.2.7  skrll 		caps = 0;
   2255  1.16.2.7  skrll 		for (j = 1; j < 32; j++)	/* NB: skip WEP */
   2256  1.16.2.7  skrll 			if ((ireq->i_val & (1<<j)) &&
   2257  1.16.2.7  skrll 			    ((ic->ic_caps & cipher2cap(j)) ||
   2258  1.16.2.7  skrll 			     ieee80211_crypto_available(j)))
   2259  1.16.2.7  skrll 				caps |= 1<<j;
   2260  1.16.2.7  skrll 		if (caps == 0)			/* nothing available */
   2261  1.16.2.7  skrll 			return EINVAL;
   2262  1.16.2.7  skrll 		/* XXX verify ciphers ok for unicast use? */
   2263  1.16.2.7  skrll 		/* XXX disallow if running as it'll have no effect */
   2264  1.16.2.7  skrll 		rsn->rsn_ucastcipherset = caps;
   2265  1.16.2.7  skrll 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2266  1.16.2.7  skrll 		break;
   2267  1.16.2.7  skrll 	case IEEE80211_IOC_UCASTCIPHER:
   2268  1.16.2.7  skrll 		if ((rsn->rsn_ucastcipherset & cipher2cap(ireq->i_val)) == 0)
   2269  1.16.2.7  skrll 			return EINVAL;
   2270  1.16.2.7  skrll 		rsn->rsn_ucastcipher = ireq->i_val;
   2271  1.16.2.7  skrll 		break;
   2272  1.16.2.7  skrll 	case IEEE80211_IOC_UCASTKEYLEN:
   2273  1.16.2.7  skrll 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
   2274  1.16.2.7  skrll 			return EINVAL;
   2275  1.16.2.7  skrll 		/* XXX no way to verify driver capability */
   2276  1.16.2.7  skrll 		rsn->rsn_ucastkeylen = ireq->i_val;
   2277  1.16.2.7  skrll 		break;
   2278  1.16.2.7  skrll 	case IEEE80211_IOC_DRIVER_CAPS:
   2279  1.16.2.7  skrll 		/* NB: for testing */
   2280  1.16.2.7  skrll 		ic->ic_caps = (((u_int16_t) ireq->i_val) << 16) |
   2281  1.16.2.7  skrll 			       ((u_int16_t) ireq->i_len);
   2282  1.16.2.7  skrll 		break;
   2283  1.16.2.7  skrll 	case IEEE80211_IOC_KEYMGTALGS:
   2284  1.16.2.7  skrll 		/* XXX check */
   2285  1.16.2.7  skrll 		rsn->rsn_keymgmtset = ireq->i_val;
   2286  1.16.2.7  skrll 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2287  1.16.2.7  skrll 		break;
   2288  1.16.2.7  skrll 	case IEEE80211_IOC_RSNCAPS:
   2289  1.16.2.7  skrll 		/* XXX check */
   2290  1.16.2.7  skrll 		rsn->rsn_caps = ireq->i_val;
   2291  1.16.2.7  skrll 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
   2292  1.16.2.7  skrll 		break;
   2293  1.16.2.7  skrll #ifdef __FreeBSD__
   2294  1.16.2.7  skrll 	case IEEE80211_IOC_BSSID:
   2295  1.16.2.7  skrll 		/* NB: should only be set when in STA mode */
   2296  1.16.2.7  skrll 		if (ic->ic_opmode != IEEE80211_M_STA)
   2297  1.16.2.7  skrll 			return EINVAL;
   2298  1.16.2.7  skrll 		if (ireq->i_len != sizeof(tmpbssid))
   2299  1.16.2.7  skrll 			return EINVAL;
   2300  1.16.2.7  skrll 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
   2301  1.16.2.7  skrll 		if (error)
   2302  1.16.2.7  skrll 			break;
   2303  1.16.2.7  skrll 		IEEE80211_ADDR_COPY(ic->ic_des_bssid, tmpbssid);
   2304  1.16.2.7  skrll 		if (IEEE80211_ADDR_EQ(ic->ic_des_bssid, zerobssid))
   2305  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
   2306  1.16.2.7  skrll 		else
   2307  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_DESBSSID;
   2308  1.16.2.7  skrll 		error = ENETRESET;
   2309  1.16.2.7  skrll 		break;
   2310  1.16.2.7  skrll #endif /* __FreeBSD__ */
   2311  1.16.2.7  skrll 	case IEEE80211_IOC_CHANLIST:
   2312  1.16.2.7  skrll 		error = ieee80211_ioctl_setchanlist(ic, ireq);
   2313  1.16.2.7  skrll 		break;
   2314  1.16.2.7  skrll 	case IEEE80211_IOC_SCAN_REQ:
   2315  1.16.2.7  skrll 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)	/* XXX ignore */
   2316  1.16.2.7  skrll 			break;
   2317  1.16.2.7  skrll 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
   2318  1.16.2.7  skrll 		if (error == 0)		/* XXX background scan */
   2319  1.16.2.7  skrll 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
   2320  1.16.2.7  skrll 		break;
   2321  1.16.2.7  skrll 	case IEEE80211_IOC_ADDMAC:
   2322  1.16.2.7  skrll 	case IEEE80211_IOC_DELMAC:
   2323  1.16.2.7  skrll 		error = ieee80211_ioctl_macmac(ic, ireq);
   2324  1.16.2.7  skrll 		break;
   2325  1.16.2.7  skrll 	case IEEE80211_IOC_MACCMD:
   2326  1.16.2.7  skrll 		error = ieee80211_ioctl_maccmd(ic, ireq);
   2327  1.16.2.7  skrll 		break;
   2328  1.16.2.7  skrll 	case IEEE80211_IOC_STA_TXPOW:
   2329  1.16.2.7  skrll 		error = ieee80211_ioctl_setstatxpow(ic, ireq);
   2330  1.16.2.7  skrll 		break;
   2331  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
   2332  1.16.2.7  skrll 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
   2333  1.16.2.7  skrll 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
   2334  1.16.2.7  skrll 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
   2335  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
   2336  1.16.2.7  skrll 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
   2337  1.16.2.7  skrll 		error = ieee80211_ioctl_setwmeparam(ic, ireq);
   2338  1.16.2.7  skrll 		break;
   2339  1.16.2.7  skrll 	case IEEE80211_IOC_DTIM_PERIOD:
   2340  1.16.2.7  skrll 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2341  1.16.2.7  skrll 		    ic->ic_opmode != IEEE80211_M_IBSS)
   2342  1.16.2.7  skrll 			return EINVAL;
   2343  1.16.2.7  skrll 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
   2344  1.16.2.7  skrll 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
   2345  1.16.2.7  skrll 			ic->ic_dtim_period = ireq->i_val;
   2346  1.16.2.7  skrll 			error = ENETRESET;		/* requires restart */
   2347  1.16.2.7  skrll 		} else
   2348  1.16.2.7  skrll 			error = EINVAL;
   2349  1.16.2.7  skrll 		break;
   2350  1.16.2.7  skrll 	case IEEE80211_IOC_BEACON_INTERVAL:
   2351  1.16.2.7  skrll 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
   2352  1.16.2.7  skrll 		    ic->ic_opmode != IEEE80211_M_IBSS)
   2353  1.16.2.7  skrll 			return EINVAL;
   2354  1.16.2.7  skrll 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
   2355  1.16.2.7  skrll 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
   2356  1.16.2.7  skrll 			ic->ic_lintval = ireq->i_val;
   2357  1.16.2.7  skrll 			error = ENETRESET;		/* requires restart */
   2358  1.16.2.7  skrll 		} else
   2359  1.16.2.7  skrll 			error = EINVAL;
   2360  1.16.2.7  skrll 		break;
   2361  1.16.2.7  skrll 	case IEEE80211_IOC_PUREG:
   2362  1.16.2.7  skrll 		if (ireq->i_val)
   2363  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_PUREG;
   2364  1.16.2.7  skrll 		else
   2365  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_PUREG;
   2366  1.16.2.7  skrll 		/* NB: reset only if we're operating on an 11g channel */
   2367  1.16.2.7  skrll 		if (ic->ic_curmode == IEEE80211_MODE_11G)
   2368  1.16.2.7  skrll 			error = ENETRESET;
   2369  1.16.2.7  skrll 		break;
   2370  1.16.2.7  skrll 	default:
   2371  1.16.2.7  skrll 		error = EINVAL;
   2372  1.16.2.7  skrll 		break;
   2373  1.16.2.7  skrll 	}
   2374  1.16.2.7  skrll 	if (error == ENETRESET && !IS_UP_AUTO(ic))
   2375  1.16.2.7  skrll 		error = 0;
   2376  1.16.2.7  skrll 	return error;
   2377  1.16.2.7  skrll }
   2378  1.16.2.7  skrll 
   2379  1.16.2.7  skrll #ifdef __FreeBSD__
   2380  1.16.2.7  skrll int
   2381  1.16.2.7  skrll ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
   2382  1.16.2.7  skrll {
   2383  1.16.2.7  skrll 	struct ifnet *ifp = ic->ic_ifp;
   2384  1.16.2.7  skrll 	int error = 0;
   2385  1.16.2.7  skrll 	struct ifreq *ifr;
   2386  1.16.2.7  skrll 	struct ifaddr *ifa;			/* XXX */
   2387  1.16.2.7  skrll 
   2388  1.16.2.7  skrll 	switch (cmd) {
   2389  1.16.2.7  skrll 	case SIOCSIFMEDIA:
   2390  1.16.2.7  skrll 	case SIOCGIFMEDIA:
   2391  1.16.2.7  skrll 		error = ifmedia_ioctl(ifp, (struct ifreq *) data,
   2392  1.16.2.7  skrll 				&ic->ic_media, cmd);
   2393  1.16.2.7  skrll 		break;
   2394  1.16.2.7  skrll 	case SIOCG80211:
   2395  1.16.2.7  skrll 		error = ieee80211_ioctl_get80211(ic, cmd,
   2396  1.16.2.7  skrll 				(struct ieee80211req *) data);
   2397  1.16.2.7  skrll 		break;
   2398  1.16.2.7  skrll 	case SIOCS80211:
   2399  1.16.2.7  skrll 		error = suser(curthread);
   2400  1.16.2.7  skrll 		if (error == 0)
   2401  1.16.2.7  skrll 			error = ieee80211_ioctl_set80211(ic, cmd,
   2402  1.16.2.7  skrll 					(struct ieee80211req *) data);
   2403  1.16.2.7  skrll 		break;
   2404  1.16.2.2  skrll 	case SIOCGIFGENERIC:
   2405  1.16.2.7  skrll 		error = ieee80211_cfgget(ic, cmd, data);
   2406  1.16.2.2  skrll 		break;
   2407  1.16.2.2  skrll 	case SIOCSIFGENERIC:
   2408  1.16.2.7  skrll 		error = suser(curthread);
   2409  1.16.2.2  skrll 		if (error)
   2410  1.16.2.2  skrll 			break;
   2411  1.16.2.7  skrll 		error = ieee80211_cfgset(ic, cmd, data);
   2412  1.16.2.7  skrll 		break;
   2413  1.16.2.7  skrll 	case SIOCG80211STATS:
   2414  1.16.2.7  skrll 		ifr = (struct ifreq *)data;
   2415  1.16.2.7  skrll 		copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
   2416  1.16.2.7  skrll 		break;
   2417  1.16.2.7  skrll 	case SIOCSIFMTU:
   2418  1.16.2.7  skrll 		ifr = (struct ifreq *)data;
   2419  1.16.2.7  skrll 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
   2420  1.16.2.7  skrll 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
   2421  1.16.2.7  skrll 			error = EINVAL;
   2422  1.16.2.7  skrll 		else
   2423  1.16.2.7  skrll 			ifp->if_mtu = ifr->ifr_mtu;
   2424  1.16.2.7  skrll 		break;
   2425  1.16.2.7  skrll 	case SIOCSIFADDR:
   2426  1.16.2.7  skrll 		/*
   2427  1.16.2.7  skrll 		 * XXX Handle this directly so we can supress if_init calls.
   2428  1.16.2.7  skrll 		 * XXX This should be done in ether_ioctl but for the moment
   2429  1.16.2.7  skrll 		 * XXX there are too many other parts of the system that
   2430  1.16.2.7  skrll 		 * XXX set IFF_UP and so supress if_init being called when
   2431  1.16.2.7  skrll 		 * XXX it should be.
   2432  1.16.2.7  skrll 		 */
   2433  1.16.2.7  skrll 		ifa = (struct ifaddr *) data;
   2434  1.16.2.7  skrll 		switch (ifa->ifa_addr->sa_family) {
   2435  1.16.2.7  skrll #ifdef INET
   2436  1.16.2.7  skrll 		case AF_INET:
   2437  1.16.2.7  skrll 			if ((ifp->if_flags & IFF_UP) == 0) {
   2438  1.16.2.7  skrll 				ifp->if_flags |= IFF_UP;
   2439  1.16.2.7  skrll 				ifp->if_init(ifp->if_softc);
   2440  1.16.2.7  skrll 			}
   2441  1.16.2.7  skrll 			arp_ifinit(ifp, ifa);
   2442  1.16.2.7  skrll 			break;
   2443  1.16.2.7  skrll #endif
   2444  1.16.2.7  skrll #ifdef IPX
   2445  1.16.2.7  skrll 		/*
   2446  1.16.2.7  skrll 		 * XXX - This code is probably wrong,
   2447  1.16.2.7  skrll 		 *	 but has been copied many times.
   2448  1.16.2.7  skrll 		 */
   2449  1.16.2.7  skrll 		case AF_IPX: {
   2450  1.16.2.7  skrll 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
   2451  1.16.2.7  skrll 
   2452  1.16.2.7  skrll 			if (ipx_nullhost(*ina))
   2453  1.16.2.7  skrll 				ina->x_host = *(union ipx_host *)
   2454  1.16.2.7  skrll 				    IFP2ENADDR(ifp);
   2455  1.16.2.7  skrll 			else
   2456  1.16.2.7  skrll 				bcopy((caddr_t) ina->x_host.c_host,
   2457  1.16.2.7  skrll 				      (caddr_t) IFP2ENADDR(ifp),
   2458  1.16.2.7  skrll 				      ETHER_ADDR_LEN);
   2459  1.16.2.7  skrll 			/* fall thru... */
   2460  1.16.2.7  skrll 		}
   2461  1.16.2.7  skrll #endif
   2462  1.16.2.7  skrll 		default:
   2463  1.16.2.7  skrll 			if ((ifp->if_flags & IFF_UP) == 0) {
   2464  1.16.2.7  skrll 				ifp->if_flags |= IFF_UP;
   2465  1.16.2.7  skrll 				ifp->if_init(ifp->if_softc);
   2466  1.16.2.7  skrll 			}
   2467  1.16.2.7  skrll 			break;
   2468  1.16.2.7  skrll 		}
   2469  1.16.2.2  skrll 		break;
   2470  1.16.2.2  skrll 	default:
   2471  1.16.2.2  skrll 		error = ether_ioctl(ifp, cmd, data);
   2472  1.16.2.2  skrll 		break;
   2473  1.16.2.2  skrll 	}
   2474  1.16.2.2  skrll 	return error;
   2475  1.16.2.2  skrll }
   2476  1.16.2.2  skrll #endif /* __FreeBSD__ */
   2477  1.16.2.2  skrll 
   2478  1.16.2.7  skrll #ifdef COMPAT_20
   2479  1.16.2.7  skrll static void
   2480  1.16.2.7  skrll ieee80211_get_ostats(struct ieee80211_ostats *ostats,
   2481  1.16.2.7  skrll     struct ieee80211_stats *stats)
   2482  1.16.2.7  skrll {
   2483  1.16.2.7  skrll #define	COPYSTATS1(__ostats, __nstats, __dstmemb, __srcmemb, __lastmemb)\
   2484  1.16.2.7  skrll 	(void)memcpy(&(__ostats)->__dstmemb, &(__nstats)->__srcmemb,	\
   2485  1.16.2.7  skrll 	    offsetof(struct ieee80211_stats, __lastmemb) -		\
   2486  1.16.2.7  skrll 	    offsetof(struct ieee80211_stats, __srcmemb))
   2487  1.16.2.7  skrll #define	COPYSTATS(__ostats, __nstats, __dstmemb, __lastmemb)		\
   2488  1.16.2.7  skrll 	COPYSTATS1(__ostats, __nstats, __dstmemb, __dstmemb, __lastmemb)
   2489  1.16.2.7  skrll 
   2490  1.16.2.7  skrll 	COPYSTATS(ostats, stats, is_rx_badversion, is_rx_unencrypted);
   2491  1.16.2.7  skrll 	COPYSTATS(ostats, stats, is_rx_wepfail, is_rx_beacon);
   2492  1.16.2.7  skrll 	COPYSTATS(ostats, stats, is_rx_rstoobig, is_rx_auth_countermeasures);
   2493  1.16.2.7  skrll 	COPYSTATS(ostats, stats, is_rx_assoc_bss, is_rx_assoc_badwpaie);
   2494  1.16.2.7  skrll 	COPYSTATS(ostats, stats, is_rx_deauth, is_rx_unauth);
   2495  1.16.2.7  skrll 	COPYSTATS1(ostats, stats, is_tx_nombuf, is_tx_nobuf, is_tx_badcipher);
   2496  1.16.2.7  skrll 	COPYSTATS(ostats, stats, is_scan_active, is_crypto_tkip);
   2497  1.16.2.7  skrll }
   2498  1.16.2.7  skrll #endif /* COMPAT_20 */
   2499  1.16.2.7  skrll 
   2500  1.16.2.2  skrll #ifdef __NetBSD__
   2501  1.16.2.2  skrll int
   2502  1.16.2.7  skrll ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
   2503  1.16.2.2  skrll {
   2504  1.16.2.7  skrll 	struct ifnet *ifp = ic->ic_ifp;
   2505  1.16.2.2  skrll 	struct ifreq *ifr = (struct ifreq *)data;
   2506  1.16.2.7  skrll 	int i, error = 0, kid, klen, s;
   2507  1.16.2.7  skrll 	struct ieee80211_key *k;
   2508  1.16.2.2  skrll 	struct ieee80211_nwid nwid;
   2509  1.16.2.2  skrll 	struct ieee80211_nwkey *nwkey;
   2510  1.16.2.2  skrll 	struct ieee80211_power *power;
   2511  1.16.2.2  skrll 	struct ieee80211_bssid *bssid;
   2512  1.16.2.2  skrll 	struct ieee80211chanreq *chanreq;
   2513  1.16.2.2  skrll 	struct ieee80211_channel *chan;
   2514  1.16.2.7  skrll 	uint32_t oflags;
   2515  1.16.2.7  skrll #ifdef COMPAT_20
   2516  1.16.2.7  skrll 	struct ieee80211_ostats ostats;
   2517  1.16.2.7  skrll #endif /* COMPAT_20 */
   2518  1.16.2.2  skrll 	static const u_int8_t empty_macaddr[IEEE80211_ADDR_LEN] = {
   2519  1.16.2.2  skrll 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00
   2520  1.16.2.2  skrll 	};
   2521  1.16.2.7  skrll 	u_int8_t tmpkey[IEEE80211_WEP_NKID][IEEE80211_KEYBUF_SIZE];
   2522  1.16.2.2  skrll 
   2523  1.16.2.2  skrll 	switch (cmd) {
   2524  1.16.2.2  skrll 	case SIOCSIFMEDIA:
   2525  1.16.2.2  skrll 	case SIOCGIFMEDIA:
   2526  1.16.2.2  skrll 		error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd);
   2527  1.16.2.2  skrll 		break;
   2528  1.16.2.7  skrll 	case SIOCG80211:
   2529  1.16.2.7  skrll 		error = ieee80211_ioctl_get80211(ic, cmd,
   2530  1.16.2.7  skrll 				(struct ieee80211req *) data);
   2531  1.16.2.7  skrll 		break;
   2532  1.16.2.7  skrll 	case SIOCS80211:
   2533  1.16.2.7  skrll 		if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0)
   2534  1.16.2.7  skrll 			break;
   2535  1.16.2.7  skrll 		error = ieee80211_ioctl_set80211(ic, cmd,
   2536  1.16.2.7  skrll 				(struct ieee80211req *) data);
   2537  1.16.2.7  skrll 		break;
   2538  1.16.2.2  skrll 	case SIOCS80211NWID:
   2539  1.16.2.2  skrll 		if ((error = copyin(ifr->ifr_data, &nwid, sizeof(nwid))) != 0)
   2540  1.16.2.2  skrll 			break;
   2541  1.16.2.2  skrll 		if (nwid.i_len > IEEE80211_NWID_LEN) {
   2542  1.16.2.2  skrll 			error = EINVAL;
   2543  1.16.2.2  skrll 			break;
   2544  1.16.2.2  skrll 		}
   2545  1.16.2.2  skrll 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
   2546  1.16.2.2  skrll 		ic->ic_des_esslen = nwid.i_len;
   2547  1.16.2.2  skrll 		memcpy(ic->ic_des_essid, nwid.i_nwid, nwid.i_len);
   2548  1.16.2.2  skrll 		error = ENETRESET;
   2549  1.16.2.2  skrll 		break;
   2550  1.16.2.2  skrll 	case SIOCG80211NWID:
   2551  1.16.2.2  skrll 		memset(&nwid, 0, sizeof(nwid));
   2552  1.16.2.2  skrll 		switch (ic->ic_state) {
   2553  1.16.2.2  skrll 		case IEEE80211_S_INIT:
   2554  1.16.2.2  skrll 		case IEEE80211_S_SCAN:
   2555  1.16.2.2  skrll 			nwid.i_len = ic->ic_des_esslen;
   2556  1.16.2.2  skrll 			memcpy(nwid.i_nwid, ic->ic_des_essid, nwid.i_len);
   2557  1.16.2.2  skrll 			break;
   2558  1.16.2.2  skrll 		default:
   2559  1.16.2.2  skrll 			nwid.i_len = ic->ic_bss->ni_esslen;
   2560  1.16.2.2  skrll 			memcpy(nwid.i_nwid, ic->ic_bss->ni_essid, nwid.i_len);
   2561  1.16.2.2  skrll 			break;
   2562  1.16.2.2  skrll 		}
   2563  1.16.2.2  skrll 		error = copyout(&nwid, ifr->ifr_data, sizeof(nwid));
   2564  1.16.2.2  skrll 		break;
   2565  1.16.2.2  skrll 	case SIOCS80211NWKEY:
   2566  1.16.2.2  skrll 		nwkey = (struct ieee80211_nwkey *)data;
   2567  1.16.2.7  skrll 		/* transmit key index out of range? */
   2568  1.16.2.7  skrll 		kid = nwkey->i_defkid - 1;
   2569  1.16.2.7  skrll 		if (kid < 0 || kid >= IEEE80211_WEP_NKID) {
   2570  1.16.2.2  skrll 			error = EINVAL;
   2571  1.16.2.2  skrll 			break;
   2572  1.16.2.2  skrll 		}
   2573  1.16.2.7  skrll 		/* no such transmit key is set? */
   2574  1.16.2.7  skrll 		if (nwkey->i_key[kid].i_keylen == 0 ||
   2575  1.16.2.7  skrll 		    (nwkey->i_key[kid].i_keylen == -1 &&
   2576  1.16.2.7  skrll 		     ic->ic_nw_keys[kid].wk_keylen == 0)) {
   2577  1.16.2.7  skrll 			if (nwkey->i_wepon != IEEE80211_NWKEY_OPEN) {
   2578  1.16.2.7  skrll 				error = EINVAL;
   2579  1.16.2.7  skrll 				break;
   2580  1.16.2.7  skrll 			}
   2581  1.16.2.7  skrll 		}
   2582  1.16.2.7  skrll 		/* check key lengths */
   2583  1.16.2.7  skrll 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2584  1.16.2.7  skrll 			klen = nwkey->i_key[kid].i_keylen;
   2585  1.16.2.7  skrll 			if ((klen > 0 &&
   2586  1.16.2.7  skrll 			    klen < IEEE80211_WEP_KEYLEN) ||
   2587  1.16.2.7  skrll 			    klen > sizeof(ic->ic_nw_keys[kid].wk_key)) {
   2588  1.16.2.2  skrll 				error = EINVAL;
   2589  1.16.2.2  skrll 				break;
   2590  1.16.2.2  skrll 			}
   2591  1.16.2.7  skrll 		}
   2592  1.16.2.7  skrll 
   2593  1.16.2.7  skrll 		if (error)
   2594  1.16.2.7  skrll 			break;
   2595  1.16.2.7  skrll 
   2596  1.16.2.7  skrll 		/* copy in keys */
   2597  1.16.2.7  skrll 		(void)memset(tmpkey, 0, sizeof(tmpkey));
   2598  1.16.2.7  skrll 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2599  1.16.2.7  skrll 			klen = nwkey->i_key[kid].i_keylen;
   2600  1.16.2.7  skrll 			if (klen <= 0)
   2601  1.16.2.2  skrll 				continue;
   2602  1.16.2.7  skrll 			if ((error = copyin(nwkey->i_key[kid].i_keydat,
   2603  1.16.2.7  skrll 			    tmpkey[kid], klen)) != 0)
   2604  1.16.2.2  skrll 				break;
   2605  1.16.2.2  skrll 		}
   2606  1.16.2.7  skrll 
   2607  1.16.2.2  skrll 		if (error)
   2608  1.16.2.2  skrll 			break;
   2609  1.16.2.7  skrll 
   2610  1.16.2.7  skrll 		/* set keys */
   2611  1.16.2.7  skrll 		ieee80211_key_update_begin(ic);
   2612  1.16.2.7  skrll 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2613  1.16.2.7  skrll 			klen = nwkey->i_key[kid].i_keylen;
   2614  1.16.2.7  skrll 			if (klen <= 0)
   2615  1.16.2.7  skrll 				continue;
   2616  1.16.2.7  skrll 			k = &ic->ic_nw_keys[kid];
   2617  1.16.2.7  skrll 			k->wk_keyix = kid;
   2618  1.16.2.7  skrll 			if (!ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
   2619  1.16.2.7  skrll 			    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
   2620  1.16.2.2  skrll 				error = EINVAL;
   2621  1.16.2.7  skrll 				continue;
   2622  1.16.2.2  skrll 			}
   2623  1.16.2.7  skrll 			k->wk_keylen = nwkey->i_key[kid].i_keylen;
   2624  1.16.2.7  skrll 			(void)memcpy(k->wk_key, tmpkey[kid],
   2625  1.16.2.7  skrll 			    sizeof(tmpkey[kid]));
   2626  1.16.2.7  skrll 			if (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
   2627  1.16.2.7  skrll 				error = EINVAL;
   2628  1.16.2.7  skrll 		}
   2629  1.16.2.7  skrll 		ieee80211_key_update_end(ic);
   2630  1.16.2.7  skrll 
   2631  1.16.2.7  skrll 		if (error)
   2632  1.16.2.7  skrll 			break;
   2633  1.16.2.7  skrll 
   2634  1.16.2.7  skrll 		/* delete keys */
   2635  1.16.2.7  skrll 		for (kid = 0; kid < IEEE80211_WEP_NKID; kid++) {
   2636  1.16.2.7  skrll 			klen = nwkey->i_key[kid].i_keylen;
   2637  1.16.2.7  skrll 			k = &ic->ic_nw_keys[kid];
   2638  1.16.2.7  skrll 			if (klen <= 0)
   2639  1.16.2.7  skrll 				(void)ieee80211_crypto_delkey(ic, k);
   2640  1.16.2.7  skrll 		}
   2641  1.16.2.7  skrll 
   2642  1.16.2.7  skrll 		/* set transmit key */
   2643  1.16.2.7  skrll 		kid = nwkey->i_defkid - 1;
   2644  1.16.2.7  skrll 		if (ic->ic_def_txkey != kid) {
   2645  1.16.2.7  skrll 			ic->ic_def_txkey = kid;
   2646  1.16.2.7  skrll 			error = ENETRESET;
   2647  1.16.2.7  skrll 		}
   2648  1.16.2.7  skrll 		oflags = ic->ic_flags;
   2649  1.16.2.7  skrll 		if (nwkey->i_wepon == IEEE80211_NWKEY_OPEN) {
   2650  1.16.2.2  skrll 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
   2651  1.16.2.7  skrll 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
   2652  1.16.2.7  skrll 		} else {
   2653  1.16.2.2  skrll 			ic->ic_flags |= IEEE80211_F_PRIVACY;
   2654  1.16.2.7  skrll 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
   2655  1.16.2.2  skrll 		}
   2656  1.16.2.7  skrll 		if (oflags != ic->ic_flags)
   2657  1.16.2.7  skrll 			error = ENETRESET;
   2658  1.16.2.2  skrll 		break;
   2659  1.16.2.2  skrll 	case SIOCG80211NWKEY:
   2660  1.16.2.2  skrll 		nwkey = (struct ieee80211_nwkey *)data;
   2661  1.16.2.2  skrll 		if (ic->ic_flags & IEEE80211_F_PRIVACY)
   2662  1.16.2.2  skrll 			nwkey->i_wepon = IEEE80211_NWKEY_WEP;
   2663  1.16.2.2  skrll 		else
   2664  1.16.2.2  skrll 			nwkey->i_wepon = IEEE80211_NWKEY_OPEN;
   2665  1.16.2.7  skrll 		nwkey->i_defkid = ic->ic_def_txkey + 1;
   2666  1.16.2.2  skrll 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
   2667  1.16.2.2  skrll 			if (nwkey->i_key[i].i_keydat == NULL)
   2668  1.16.2.2  skrll 				continue;
   2669  1.16.2.2  skrll 			/* do not show any keys to non-root user */
   2670  1.16.2.2  skrll 			if ((error = suser(curproc->p_ucred,
   2671  1.16.2.2  skrll 			    &curproc->p_acflag)) != 0)
   2672  1.16.2.2  skrll 				break;
   2673  1.16.2.7  skrll 			nwkey->i_key[i].i_keylen = ic->ic_nw_keys[i].wk_keylen;
   2674  1.16.2.2  skrll 			if ((error = copyout(ic->ic_nw_keys[i].wk_key,
   2675  1.16.2.2  skrll 			    nwkey->i_key[i].i_keydat,
   2676  1.16.2.7  skrll 			    ic->ic_nw_keys[i].wk_keylen)) != 0)
   2677  1.16.2.2  skrll 				break;
   2678  1.16.2.2  skrll 		}
   2679  1.16.2.2  skrll 		break;
   2680  1.16.2.2  skrll 	case SIOCS80211POWER:
   2681  1.16.2.2  skrll 		power = (struct ieee80211_power *)data;
   2682  1.16.2.2  skrll 		ic->ic_lintval = power->i_maxsleep;
   2683  1.16.2.2  skrll 		if (power->i_enabled != 0) {
   2684  1.16.2.2  skrll 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
   2685  1.16.2.2  skrll 				error = EINVAL;
   2686  1.16.2.2  skrll 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
   2687  1.16.2.2  skrll 				ic->ic_flags |= IEEE80211_F_PMGTON;
   2688  1.16.2.2  skrll 				error = ENETRESET;
   2689  1.16.2.2  skrll 			}
   2690  1.16.2.2  skrll 		} else {
   2691  1.16.2.2  skrll 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
   2692  1.16.2.2  skrll 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
   2693  1.16.2.2  skrll 				error = ENETRESET;
   2694  1.16.2.2  skrll 			}
   2695  1.16.2.2  skrll 		}
   2696  1.16.2.2  skrll 		break;
   2697  1.16.2.2  skrll 	case SIOCG80211POWER:
   2698  1.16.2.2  skrll 		power = (struct ieee80211_power *)data;
   2699  1.16.2.2  skrll 		power->i_enabled = (ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0;
   2700  1.16.2.2  skrll 		power->i_maxsleep = ic->ic_lintval;
   2701  1.16.2.2  skrll 		break;
   2702  1.16.2.2  skrll 	case SIOCS80211BSSID:
   2703  1.16.2.2  skrll 		bssid = (struct ieee80211_bssid *)data;
   2704  1.16.2.2  skrll 		if (IEEE80211_ADDR_EQ(bssid->i_bssid, empty_macaddr))
   2705  1.16.2.2  skrll 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
   2706  1.16.2.2  skrll 		else {
   2707  1.16.2.2  skrll 			ic->ic_flags |= IEEE80211_F_DESBSSID;
   2708  1.16.2.2  skrll 			IEEE80211_ADDR_COPY(ic->ic_des_bssid, bssid->i_bssid);
   2709  1.16.2.2  skrll 		}
   2710  1.16.2.2  skrll 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2711  1.16.2.2  skrll 			break;
   2712  1.16.2.2  skrll 		switch (ic->ic_state) {
   2713  1.16.2.2  skrll 		case IEEE80211_S_INIT:
   2714  1.16.2.2  skrll 		case IEEE80211_S_SCAN:
   2715  1.16.2.2  skrll 			error = ENETRESET;
   2716  1.16.2.2  skrll 			break;
   2717  1.16.2.2  skrll 		default:
   2718  1.16.2.2  skrll 			if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
   2719  1.16.2.2  skrll 			    !IEEE80211_ADDR_EQ(ic->ic_des_bssid,
   2720  1.16.2.2  skrll 			    ic->ic_bss->ni_bssid))
   2721  1.16.2.2  skrll 				error = ENETRESET;
   2722  1.16.2.2  skrll 			break;
   2723  1.16.2.2  skrll 		}
   2724  1.16.2.2  skrll 		break;
   2725  1.16.2.2  skrll 	case SIOCG80211BSSID:
   2726  1.16.2.2  skrll 		bssid = (struct ieee80211_bssid *)data;
   2727  1.16.2.2  skrll 		switch (ic->ic_state) {
   2728  1.16.2.2  skrll 		case IEEE80211_S_INIT:
   2729  1.16.2.2  skrll 		case IEEE80211_S_SCAN:
   2730  1.16.2.2  skrll 			if (ic->ic_opmode == IEEE80211_M_HOSTAP)
   2731  1.16.2.2  skrll 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   2732  1.16.2.2  skrll 				    ic->ic_myaddr);
   2733  1.16.2.2  skrll 			else if (ic->ic_flags & IEEE80211_F_DESBSSID)
   2734  1.16.2.2  skrll 				IEEE80211_ADDR_COPY(bssid->i_bssid,
   2735  1.16.2.2  skrll 				    ic->ic_des_bssid);
   2736  1.16.2.2  skrll 			else
   2737  1.16.2.2  skrll 				memset(bssid->i_bssid, 0, IEEE80211_ADDR_LEN);
   2738  1.16.2.2  skrll 			break;
   2739  1.16.2.2  skrll 		default:
   2740  1.16.2.2  skrll 			IEEE80211_ADDR_COPY(bssid->i_bssid,
   2741  1.16.2.2  skrll 			    ic->ic_bss->ni_bssid);
   2742  1.16.2.2  skrll 			break;
   2743  1.16.2.2  skrll 		}
   2744  1.16.2.2  skrll 		break;
   2745  1.16.2.2  skrll 	case SIOCS80211CHANNEL:
   2746  1.16.2.2  skrll 		chanreq = (struct ieee80211chanreq *)data;
   2747  1.16.2.2  skrll 		if (chanreq->i_channel == IEEE80211_CHAN_ANY)
   2748  1.16.2.2  skrll 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
   2749  1.16.2.2  skrll 		else if (chanreq->i_channel > IEEE80211_CHAN_MAX ||
   2750  1.16.2.2  skrll 		    isclr(ic->ic_chan_active, chanreq->i_channel)) {
   2751  1.16.2.2  skrll 			error = EINVAL;
   2752  1.16.2.2  skrll 			break;
   2753  1.16.2.2  skrll 		} else
   2754  1.16.2.2  skrll 			ic->ic_ibss_chan = ic->ic_des_chan =
   2755  1.16.2.2  skrll 			    &ic->ic_channels[chanreq->i_channel];
   2756  1.16.2.2  skrll 		switch (ic->ic_state) {
   2757  1.16.2.2  skrll 		case IEEE80211_S_INIT:
   2758  1.16.2.2  skrll 		case IEEE80211_S_SCAN:
   2759  1.16.2.2  skrll 			error = ENETRESET;
   2760  1.16.2.2  skrll 			break;
   2761  1.16.2.2  skrll 		default:
   2762  1.16.2.2  skrll 			if (ic->ic_opmode == IEEE80211_M_STA) {
   2763  1.16.2.2  skrll 				if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
   2764  1.16.2.2  skrll 				    ic->ic_bss->ni_chan != ic->ic_des_chan)
   2765  1.16.2.2  skrll 					error = ENETRESET;
   2766  1.16.2.2  skrll 			} else {
   2767  1.16.2.2  skrll 				if (ic->ic_bss->ni_chan != ic->ic_ibss_chan)
   2768  1.16.2.2  skrll 					error = ENETRESET;
   2769  1.16.2.2  skrll 			}
   2770  1.16.2.2  skrll 			break;
   2771  1.16.2.2  skrll 		}
   2772  1.16.2.2  skrll 		break;
   2773  1.16.2.2  skrll 	case SIOCG80211CHANNEL:
   2774  1.16.2.2  skrll 		chanreq = (struct ieee80211chanreq *)data;
   2775  1.16.2.2  skrll 		switch (ic->ic_state) {
   2776  1.16.2.2  skrll 		case IEEE80211_S_INIT:
   2777  1.16.2.2  skrll 		case IEEE80211_S_SCAN:
   2778  1.16.2.2  skrll 			if (ic->ic_opmode == IEEE80211_M_STA)
   2779  1.16.2.2  skrll 				chan = ic->ic_des_chan;
   2780  1.16.2.2  skrll 			else
   2781  1.16.2.2  skrll 				chan = ic->ic_ibss_chan;
   2782  1.16.2.2  skrll 			break;
   2783  1.16.2.2  skrll 		default:
   2784  1.16.2.2  skrll 			chan = ic->ic_bss->ni_chan;
   2785  1.16.2.2  skrll 			break;
   2786  1.16.2.2  skrll 		}
   2787  1.16.2.2  skrll 		chanreq->i_channel = ieee80211_chan2ieee(ic, chan);
   2788  1.16.2.2  skrll 		break;
   2789  1.16.2.2  skrll 	case SIOCGIFGENERIC:
   2790  1.16.2.7  skrll 		error = ieee80211_cfgget(ic, cmd, data);
   2791  1.16.2.2  skrll 		break;
   2792  1.16.2.2  skrll 	case SIOCSIFGENERIC:
   2793  1.16.2.2  skrll 		error = suser(curproc->p_ucred, &curproc->p_acflag);
   2794  1.16.2.2  skrll 		if (error)
   2795  1.16.2.2  skrll 			break;
   2796  1.16.2.7  skrll 		error = ieee80211_cfgset(ic, cmd, data);
   2797  1.16.2.7  skrll 		break;
   2798  1.16.2.7  skrll #ifdef COMPAT_20
   2799  1.16.2.7  skrll 	case OSIOCG80211STATS:
   2800  1.16.2.7  skrll 	case OSIOCG80211ZSTATS:
   2801  1.16.2.7  skrll 		ifr = (struct ifreq *)data;
   2802  1.16.2.7  skrll 		s = splnet();
   2803  1.16.2.7  skrll 		ieee80211_get_ostats(&ostats, &ic->ic_stats);
   2804  1.16.2.7  skrll 		error = copyout(&ostats, ifr->ifr_data, sizeof(ostats));
   2805  1.16.2.7  skrll 		if (error == 0 && cmd == OSIOCG80211ZSTATS)
   2806  1.16.2.7  skrll 			(void)memset(&ic->ic_stats, 0, sizeof(ic->ic_stats));
   2807  1.16.2.7  skrll 		splx(s);
   2808  1.16.2.2  skrll 		break;
   2809  1.16.2.7  skrll #endif /* COMPAT_20 */
   2810  1.16.2.5  skrll 	case SIOCG80211ZSTATS:
   2811  1.16.2.2  skrll 	case SIOCG80211STATS:
   2812  1.16.2.2  skrll 		ifr = (struct ifreq *)data;
   2813  1.16.2.5  skrll 		s = splnet();
   2814  1.16.2.7  skrll 		error = copyout(&ic->ic_stats, ifr->ifr_buf,
   2815  1.16.2.7  skrll 		    MIN(sizeof(ic->ic_stats), ifr->ifr_buflen));
   2816  1.16.2.7  skrll 		if (error == 0 && cmd == SIOCG80211ZSTATS)
   2817  1.16.2.5  skrll 			(void)memset(&ic->ic_stats, 0, sizeof(ic->ic_stats));
   2818  1.16.2.5  skrll 		splx(s);
   2819  1.16.2.2  skrll 		break;
   2820  1.16.2.2  skrll 	case SIOCSIFMTU:
   2821  1.16.2.2  skrll 		ifr = (struct ifreq *)data;
   2822  1.16.2.2  skrll 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
   2823  1.16.2.2  skrll 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
   2824  1.16.2.2  skrll 			error = EINVAL;
   2825  1.16.2.2  skrll 		else
   2826  1.16.2.2  skrll 			ifp->if_mtu = ifr->ifr_mtu;
   2827  1.16.2.2  skrll 		break;
   2828  1.16.2.2  skrll 	default:
   2829  1.16.2.2  skrll 		error = ether_ioctl(ifp, cmd, data);
   2830  1.16.2.2  skrll 		break;
   2831  1.16.2.2  skrll 	}
   2832  1.16.2.2  skrll 	return error;
   2833  1.16.2.2  skrll }
   2834  1.16.2.2  skrll #endif /* __NetBSD__ */
   2835