xref: /freebsd/sys/net80211/ieee80211_ioctl.c (revision 386d84f60e3a38eb6dc45697fdfbe87d3d8d37aa)
11a1e1d21SSam Leffler /*-
27535e66aSSam Leffler  * Copyright (c) 2001 Atsushi Onoe
31f1d7810SSam Leffler  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
41a1e1d21SSam Leffler  * All rights reserved.
51a1e1d21SSam Leffler  *
61a1e1d21SSam Leffler  * Redistribution and use in source and binary forms, with or without
71a1e1d21SSam Leffler  * modification, are permitted provided that the following conditions
81a1e1d21SSam Leffler  * are met:
91a1e1d21SSam Leffler  * 1. Redistributions of source code must retain the above copyright
107535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer.
117535e66aSSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
127535e66aSSam Leffler  *    notice, this list of conditions and the following disclaimer in the
137535e66aSSam Leffler  *    documentation and/or other materials provided with the distribution.
147535e66aSSam Leffler  * 3. The name of the author may not be used to endorse or promote products
157535e66aSSam Leffler  *    derived from this software without specific prior written permission.
161a1e1d21SSam Leffler  *
171a1e1d21SSam Leffler  * Alternatively, this software may be distributed under the terms of the
181a1e1d21SSam Leffler  * GNU General Public License ("GPL") version 2 as published by the Free
191a1e1d21SSam Leffler  * Software Foundation.
201a1e1d21SSam Leffler  *
217535e66aSSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
227535e66aSSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
237535e66aSSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
247535e66aSSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
257535e66aSSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
267535e66aSSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
277535e66aSSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
287535e66aSSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
297535e66aSSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
307535e66aSSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
311a1e1d21SSam Leffler  */
321a1e1d21SSam Leffler 
331a1e1d21SSam Leffler #include <sys/cdefs.h>
341a1e1d21SSam Leffler __FBSDID("$FreeBSD$");
351a1e1d21SSam Leffler 
361a1e1d21SSam Leffler /*
371a1e1d21SSam Leffler  * IEEE 802.11 ioctl support (FreeBSD-specific)
381a1e1d21SSam Leffler  */
391a1e1d21SSam Leffler 
40b2e95691SSam Leffler #include "opt_inet.h"
41b2e95691SSam Leffler #include "opt_ipx.h"
42b2e95691SSam Leffler 
431a1e1d21SSam Leffler #include <sys/endian.h>
441a1e1d21SSam Leffler #include <sys/param.h>
451a1e1d21SSam Leffler #include <sys/kernel.h>
461a1e1d21SSam Leffler #include <sys/socket.h>
471a1e1d21SSam Leffler #include <sys/sockio.h>
481a1e1d21SSam Leffler #include <sys/systm.h>
491a1e1d21SSam Leffler 
501a1e1d21SSam Leffler #include <net/if.h>
514a0d6638SRuslan Ermilov #include <net/if_dl.h>
521a1e1d21SSam Leffler #include <net/if_media.h>
531a1e1d21SSam Leffler #include <net/ethernet.h>
541a1e1d21SSam Leffler 
55b2e95691SSam Leffler #ifdef INET
56b2e95691SSam Leffler #include <netinet/in.h>
57b2e95691SSam Leffler #include <netinet/if_ether.h>
58b2e95691SSam Leffler #endif
59b2e95691SSam Leffler 
60b2e95691SSam Leffler #ifdef IPX
61b2e95691SSam Leffler #include <netipx/ipx.h>
62b2e95691SSam Leffler #include <netipx/ipx_if.h>
63b2e95691SSam Leffler #endif
64b2e95691SSam Leffler 
651a1e1d21SSam Leffler #include <net80211/ieee80211_var.h>
661a1e1d21SSam Leffler #include <net80211/ieee80211_ioctl.h>
671a1e1d21SSam Leffler 
681a1e1d21SSam Leffler #include <dev/wi/if_wavelan_ieee.h>
691a1e1d21SSam Leffler 
708a1b9b6aSSam Leffler #define	IS_UP(_ic) \
7113f4c340SRobert Watson 	(((_ic)->ic_ifp->if_flags & IFF_UP) &&			\
7213f4c340SRobert Watson 	    ((_ic)->ic_ifp->if_drv_flags & IFF_DRV_RUNNING))
738a1b9b6aSSam Leffler #define	IS_UP_AUTO(_ic) \
748a1b9b6aSSam Leffler 	(IS_UP(_ic) && (_ic)->ic_roaming == IEEE80211_ROAMING_AUTO)
758a1b9b6aSSam Leffler 
761a1e1d21SSam Leffler /*
771a1e1d21SSam Leffler  * XXX
781a1e1d21SSam Leffler  * Wireless LAN specific configuration interface, which is compatible
791a1e1d21SSam Leffler  * with wicontrol(8).
801a1e1d21SSam Leffler  */
811a1e1d21SSam Leffler 
828a1b9b6aSSam Leffler struct wi_read_ap_args {
838a1b9b6aSSam Leffler 	int	i;		/* result count */
848a1b9b6aSSam Leffler 	struct wi_apinfo *ap;	/* current entry in result buffer */
858a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
868a1b9b6aSSam Leffler };
878a1b9b6aSSam Leffler 
888a1b9b6aSSam Leffler static void
898a1b9b6aSSam Leffler wi_read_ap_result(void *arg, struct ieee80211_node *ni)
901a1e1d21SSam Leffler {
918a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
928a1b9b6aSSam Leffler 	struct wi_read_ap_args *sa = arg;
938a1b9b6aSSam Leffler 	struct wi_apinfo *ap = sa->ap;
948a1b9b6aSSam Leffler 	struct ieee80211_rateset *rs;
958a1b9b6aSSam Leffler 	int j;
968a1b9b6aSSam Leffler 
978a1b9b6aSSam Leffler 	if ((caddr_t)(ap + 1) > sa->max)
988a1b9b6aSSam Leffler 		return;
998a1b9b6aSSam Leffler 	memset(ap, 0, sizeof(struct wi_apinfo));
1008a1b9b6aSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
1018a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_macaddr);
1028a1b9b6aSSam Leffler 		ap->namelen = ic->ic_des_esslen;
1038a1b9b6aSSam Leffler 		if (ic->ic_des_esslen)
1048a1b9b6aSSam Leffler 			memcpy(ap->name, ic->ic_des_essid,
1058a1b9b6aSSam Leffler 			    ic->ic_des_esslen);
1068a1b9b6aSSam Leffler 	} else {
1078a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ap->bssid, ni->ni_bssid);
1088a1b9b6aSSam Leffler 		ap->namelen = ni->ni_esslen;
1098a1b9b6aSSam Leffler 		if (ni->ni_esslen)
1108a1b9b6aSSam Leffler 			memcpy(ap->name, ni->ni_essid,
1118a1b9b6aSSam Leffler 			    ni->ni_esslen);
1128a1b9b6aSSam Leffler 	}
1138a1b9b6aSSam Leffler 	ap->channel = ieee80211_chan2ieee(ic, ni->ni_chan);
1148a1b9b6aSSam Leffler 	ap->signal = ic->ic_node_getrssi(ni);
1158a1b9b6aSSam Leffler 	ap->capinfo = ni->ni_capinfo;
1168a1b9b6aSSam Leffler 	ap->interval = ni->ni_intval;
1178a1b9b6aSSam Leffler 	rs = &ni->ni_rates;
1188a1b9b6aSSam Leffler 	for (j = 0; j < rs->rs_nrates; j++) {
1198a1b9b6aSSam Leffler 		if (rs->rs_rates[j] & IEEE80211_RATE_BASIC) {
1208a1b9b6aSSam Leffler 			ap->rate = (rs->rs_rates[j] &
1218a1b9b6aSSam Leffler 			    IEEE80211_RATE_VAL) * 5; /* XXX */
1228a1b9b6aSSam Leffler 		}
1238a1b9b6aSSam Leffler 	}
1248a1b9b6aSSam Leffler 	sa->i++;
1258a1b9b6aSSam Leffler 	sa->ap++;
1268a1b9b6aSSam Leffler }
1278a1b9b6aSSam Leffler 
1288a1b9b6aSSam Leffler struct wi_read_prism2_args {
1298a1b9b6aSSam Leffler 	int	i;		/* result count */
1308a1b9b6aSSam Leffler 	struct wi_scan_res *res;/* current entry in result buffer */
1318a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
1328a1b9b6aSSam Leffler };
1338a1b9b6aSSam Leffler 
1348a1b9b6aSSam Leffler static void
1358a1b9b6aSSam Leffler wi_read_prism2_result(void *arg, struct ieee80211_node *ni)
1368a1b9b6aSSam Leffler {
1378a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1388a1b9b6aSSam Leffler 	struct wi_read_prism2_args *sa = arg;
1398a1b9b6aSSam Leffler 	struct wi_scan_res *res = sa->res;
1408a1b9b6aSSam Leffler 
1418a1b9b6aSSam Leffler 	if ((caddr_t)(res + 1) > sa->max)
1428a1b9b6aSSam Leffler 		return;
1438a1b9b6aSSam Leffler 	res->wi_chan = ieee80211_chan2ieee(ic, ni->ni_chan);
1448a1b9b6aSSam Leffler 	res->wi_noise = 0;
1458a1b9b6aSSam Leffler 	res->wi_signal = ic->ic_node_getrssi(ni);
1468a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(res->wi_bssid, ni->ni_bssid);
1478a1b9b6aSSam Leffler 	res->wi_interval = ni->ni_intval;
1488a1b9b6aSSam Leffler 	res->wi_capinfo = ni->ni_capinfo;
1498a1b9b6aSSam Leffler 	res->wi_ssid_len = ni->ni_esslen;
1508a1b9b6aSSam Leffler 	memcpy(res->wi_ssid, ni->ni_essid, IEEE80211_NWID_LEN);
1518a1b9b6aSSam Leffler 	/* NB: assumes wi_srates holds <= ni->ni_rates */
1528a1b9b6aSSam Leffler 	memcpy(res->wi_srates, ni->ni_rates.rs_rates,
1538a1b9b6aSSam Leffler 		sizeof(res->wi_srates));
1548a1b9b6aSSam Leffler 	if (ni->ni_rates.rs_nrates < 10)
1558a1b9b6aSSam Leffler 		res->wi_srates[ni->ni_rates.rs_nrates] = 0;
1568a1b9b6aSSam Leffler 	res->wi_rate = ni->ni_rates.rs_rates[ni->ni_txrate];
1578a1b9b6aSSam Leffler 	res->wi_rsvd = 0;
1588a1b9b6aSSam Leffler 
1598a1b9b6aSSam Leffler 	sa->i++;
1608a1b9b6aSSam Leffler 	sa->res++;
1618a1b9b6aSSam Leffler }
1628a1b9b6aSSam Leffler 
1638a1b9b6aSSam Leffler struct wi_read_sigcache_args {
1648a1b9b6aSSam Leffler 	int	i;		/* result count */
1658a1b9b6aSSam Leffler 	struct wi_sigcache *wsc;/* current entry in result buffer */
1668a1b9b6aSSam Leffler 	caddr_t	max;		/* result buffer bound */
1678a1b9b6aSSam Leffler };
1688a1b9b6aSSam Leffler 
1698a1b9b6aSSam Leffler static void
1708a1b9b6aSSam Leffler wi_read_sigcache(void *arg, struct ieee80211_node *ni)
1718a1b9b6aSSam Leffler {
1728a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1738a1b9b6aSSam Leffler 	struct wi_read_sigcache_args *sa = arg;
1748a1b9b6aSSam Leffler 	struct wi_sigcache *wsc = sa->wsc;
1758a1b9b6aSSam Leffler 
1768a1b9b6aSSam Leffler 	if ((caddr_t)(wsc + 1) > sa->max)
1778a1b9b6aSSam Leffler 		return;
1788a1b9b6aSSam Leffler 	memset(wsc, 0, sizeof(struct wi_sigcache));
1798a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(wsc->macsrc, ni->ni_macaddr);
1808a1b9b6aSSam Leffler 	wsc->signal = ic->ic_node_getrssi(ni);
1818a1b9b6aSSam Leffler 
1828a1b9b6aSSam Leffler 	sa->wsc++;
1838a1b9b6aSSam Leffler 	sa->i++;
1848a1b9b6aSSam Leffler }
1858a1b9b6aSSam Leffler 
1868a1b9b6aSSam Leffler int
1878a1b9b6aSSam Leffler ieee80211_cfgget(struct ieee80211com *ic, u_long cmd, caddr_t data)
1888a1b9b6aSSam Leffler {
1898a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
1901a1e1d21SSam Leffler 	int i, j, error;
1911a1e1d21SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
1921a1e1d21SSam Leffler 	struct wi_req wreq;
1931a1e1d21SSam Leffler 	struct wi_ltv_keys *keys;
1941a1e1d21SSam Leffler 
1951a1e1d21SSam Leffler 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
1961a1e1d21SSam Leffler 	if (error)
1971a1e1d21SSam Leffler 		return error;
1981a1e1d21SSam Leffler 	wreq.wi_len = 0;
1991a1e1d21SSam Leffler 	switch (wreq.wi_type) {
2001a1e1d21SSam Leffler 	case WI_RID_SERIALNO:
2011a1e1d21SSam Leffler 		/* nothing appropriate */
2021a1e1d21SSam Leffler 		break;
2031a1e1d21SSam Leffler 	case WI_RID_NODENAME:
2041a1e1d21SSam Leffler 		strcpy((char *)&wreq.wi_val[1], hostname);
2051a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(strlen(hostname));
2061a1e1d21SSam Leffler 		wreq.wi_len = (1 + strlen(hostname) + 1) / 2;
2071a1e1d21SSam Leffler 		break;
2081a1e1d21SSam Leffler 	case WI_RID_CURRENT_SSID:
2091a1e1d21SSam Leffler 		if (ic->ic_state != IEEE80211_S_RUN) {
2101a1e1d21SSam Leffler 			wreq.wi_val[0] = 0;
2111a1e1d21SSam Leffler 			wreq.wi_len = 1;
2121a1e1d21SSam Leffler 			break;
2131a1e1d21SSam Leffler 		}
2141a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_esslen);
2151a1e1d21SSam Leffler 		memcpy(&wreq.wi_val[1], ic->ic_bss->ni_essid,
2161a1e1d21SSam Leffler 		    ic->ic_bss->ni_esslen);
2171a1e1d21SSam Leffler 		wreq.wi_len = (1 + ic->ic_bss->ni_esslen + 1) / 2;
2181a1e1d21SSam Leffler 		break;
2191a1e1d21SSam Leffler 	case WI_RID_OWN_SSID:
2201a1e1d21SSam Leffler 	case WI_RID_DESIRED_SSID:
2211a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_des_esslen);
2221a1e1d21SSam Leffler 		memcpy(&wreq.wi_val[1], ic->ic_des_essid, ic->ic_des_esslen);
2231a1e1d21SSam Leffler 		wreq.wi_len = (1 + ic->ic_des_esslen + 1) / 2;
2241a1e1d21SSam Leffler 		break;
2251a1e1d21SSam Leffler 	case WI_RID_CURRENT_BSSID:
2261a1e1d21SSam Leffler 		if (ic->ic_state == IEEE80211_S_RUN)
2271a1e1d21SSam Leffler 			IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_bss->ni_bssid);
2281a1e1d21SSam Leffler 		else
2291a1e1d21SSam Leffler 			memset(wreq.wi_val, 0, IEEE80211_ADDR_LEN);
2301a1e1d21SSam Leffler 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
2311a1e1d21SSam Leffler 		break;
2321a1e1d21SSam Leffler 	case WI_RID_CHANNEL_LIST:
2331a1e1d21SSam Leffler 		memset(wreq.wi_val, 0, sizeof(wreq.wi_val));
2341a1e1d21SSam Leffler 		/*
2351a1e1d21SSam Leffler 		 * Since channel 0 is not available for DS, channel 1
2361a1e1d21SSam Leffler 		 * is assigned to LSB on WaveLAN.
2371a1e1d21SSam Leffler 		 */
2381a1e1d21SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_DS)
2391a1e1d21SSam Leffler 			i = 1;
2401a1e1d21SSam Leffler 		else
2411a1e1d21SSam Leffler 			i = 0;
2421a1e1d21SSam Leffler 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++)
2431a1e1d21SSam Leffler 			if (isset(ic->ic_chan_active, i)) {
2441a1e1d21SSam Leffler 				setbit((u_int8_t *)wreq.wi_val, j);
2451a1e1d21SSam Leffler 				wreq.wi_len = j / 16 + 1;
2461a1e1d21SSam Leffler 			}
2471a1e1d21SSam Leffler 		break;
2481a1e1d21SSam Leffler 	case WI_RID_OWN_CHNL:
2491a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
2501a1e1d21SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
2511a1e1d21SSam Leffler 		wreq.wi_len = 1;
2521a1e1d21SSam Leffler 		break;
2531a1e1d21SSam Leffler 	case WI_RID_CURRENT_CHAN:
2541a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
255b5c99415SSam Leffler 			ieee80211_chan2ieee(ic, ic->ic_curchan));
2561a1e1d21SSam Leffler 		wreq.wi_len = 1;
2571a1e1d21SSam Leffler 		break;
2581a1e1d21SSam Leffler 	case WI_RID_COMMS_QUALITY:
2591a1e1d21SSam Leffler 		wreq.wi_val[0] = 0;				/* quality */
2608a1b9b6aSSam Leffler 		wreq.wi_val[1] = htole16(ic->ic_node_getrssi(ic->ic_bss));
2611a1e1d21SSam Leffler 		wreq.wi_val[2] = 0;				/* noise */
2621a1e1d21SSam Leffler 		wreq.wi_len = 3;
2631a1e1d21SSam Leffler 		break;
2641a1e1d21SSam Leffler 	case WI_RID_PROMISC:
2651a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16((ifp->if_flags & IFF_PROMISC) ? 1 : 0);
2661a1e1d21SSam Leffler 		wreq.wi_len = 1;
2671a1e1d21SSam Leffler 		break;
2681a1e1d21SSam Leffler 	case WI_RID_PORTTYPE:
2691a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_opmode);
2701a1e1d21SSam Leffler 		wreq.wi_len = 1;
2711a1e1d21SSam Leffler 		break;
2721a1e1d21SSam Leffler 	case WI_RID_MAC_NODE:
2731a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(wreq.wi_val, ic->ic_myaddr);
2741a1e1d21SSam Leffler 		wreq.wi_len = IEEE80211_ADDR_LEN / 2;
2751a1e1d21SSam Leffler 		break;
2761a1e1d21SSam Leffler 	case WI_RID_TX_RATE:
2772c39b32cSSam Leffler 		if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE)
2781a1e1d21SSam Leffler 			wreq.wi_val[0] = 0;	/* auto */
2791a1e1d21SSam Leffler 		else
2801a1e1d21SSam Leffler 			wreq.wi_val[0] = htole16(
2811a1e1d21SSam Leffler 			    (ic->ic_sup_rates[ic->ic_curmode].rs_rates[ic->ic_fixed_rate] &
2821a1e1d21SSam Leffler 			    IEEE80211_RATE_VAL) / 2);
2831a1e1d21SSam Leffler 		wreq.wi_len = 1;
2841a1e1d21SSam Leffler 		break;
2851a1e1d21SSam Leffler 	case WI_RID_CUR_TX_RATE:
2861a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(
2871a1e1d21SSam Leffler 		    (ic->ic_bss->ni_rates.rs_rates[ic->ic_bss->ni_txrate] &
2881a1e1d21SSam Leffler 		    IEEE80211_RATE_VAL) / 2);
2891a1e1d21SSam Leffler 		wreq.wi_len = 1;
2901a1e1d21SSam Leffler 		break;
2911a1e1d21SSam Leffler 	case WI_RID_RTS_THRESH:
2921a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_rtsthreshold);
2931a1e1d21SSam Leffler 		wreq.wi_len = 1;
2941a1e1d21SSam Leffler 		break;
2951a1e1d21SSam Leffler 	case WI_RID_CREATE_IBSS:
2961a1e1d21SSam Leffler 		wreq.wi_val[0] =
2971a1e1d21SSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_IBSSON) ? 1 : 0);
2981a1e1d21SSam Leffler 		wreq.wi_len = 1;
2991a1e1d21SSam Leffler 		break;
3001a1e1d21SSam Leffler 	case WI_RID_MICROWAVE_OVEN:
3011a1e1d21SSam Leffler 		wreq.wi_val[0] = 0;	/* no ... not supported */
3021a1e1d21SSam Leffler 		wreq.wi_len = 1;
3031a1e1d21SSam Leffler 		break;
3041a1e1d21SSam Leffler 	case WI_RID_ROAMING_MODE:
3058a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_roaming);	/* XXX map */
3061a1e1d21SSam Leffler 		wreq.wi_len = 1;
3071a1e1d21SSam Leffler 		break;
3081a1e1d21SSam Leffler 	case WI_RID_SYSTEM_SCALE:
3091a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(1);	/* low density ... not supp */
3101a1e1d21SSam Leffler 		wreq.wi_len = 1;
3111a1e1d21SSam Leffler 		break;
3121a1e1d21SSam Leffler 	case WI_RID_PM_ENABLED:
3131a1e1d21SSam Leffler 		wreq.wi_val[0] =
3141a1e1d21SSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_PMGTON) ? 1 : 0);
3151a1e1d21SSam Leffler 		wreq.wi_len = 1;
3161a1e1d21SSam Leffler 		break;
3171a1e1d21SSam Leffler 	case WI_RID_MAX_SLEEP:
3181a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_lintval);
3191a1e1d21SSam Leffler 		wreq.wi_len = 1;
3201a1e1d21SSam Leffler 		break;
3211a1e1d21SSam Leffler 	case WI_RID_CUR_BEACON_INT:
3221a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_bss->ni_intval);
3231a1e1d21SSam Leffler 		wreq.wi_len = 1;
3241a1e1d21SSam Leffler 		break;
3251a1e1d21SSam Leffler 	case WI_RID_WEP_AVAIL:
3268a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(1);	/* always available */
3271a1e1d21SSam Leffler 		wreq.wi_len = 1;
3281a1e1d21SSam Leffler 		break;
3291a1e1d21SSam Leffler 	case WI_RID_CNFAUTHMODE:
3301a1e1d21SSam Leffler 		wreq.wi_val[0] = htole16(1);	/* TODO: open system only */
3311a1e1d21SSam Leffler 		wreq.wi_len = 1;
3321a1e1d21SSam Leffler 		break;
3331a1e1d21SSam Leffler 	case WI_RID_ENCRYPTION:
3341a1e1d21SSam Leffler 		wreq.wi_val[0] =
3358a1b9b6aSSam Leffler 		    htole16((ic->ic_flags & IEEE80211_F_PRIVACY) ? 1 : 0);
3361a1e1d21SSam Leffler 		wreq.wi_len = 1;
3371a1e1d21SSam Leffler 		break;
3381a1e1d21SSam Leffler 	case WI_RID_TX_CRYPT_KEY:
3398a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_def_txkey);
3401a1e1d21SSam Leffler 		wreq.wi_len = 1;
3411a1e1d21SSam Leffler 		break;
3421a1e1d21SSam Leffler 	case WI_RID_DEFLT_CRYPT_KEYS:
3431a1e1d21SSam Leffler 		keys = (struct wi_ltv_keys *)&wreq;
3441a1e1d21SSam Leffler 		/* do not show keys to non-root user */
3451a1e1d21SSam Leffler 		error = suser(curthread);
3461a1e1d21SSam Leffler 		if (error) {
3471a1e1d21SSam Leffler 			memset(keys, 0, sizeof(*keys));
3481a1e1d21SSam Leffler 			error = 0;
3491a1e1d21SSam Leffler 			break;
3501a1e1d21SSam Leffler 		}
3511a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
3521a1e1d21SSam Leffler 			keys->wi_keys[i].wi_keylen =
3538a1b9b6aSSam Leffler 			    htole16(ic->ic_nw_keys[i].wk_keylen);
3541a1e1d21SSam Leffler 			memcpy(keys->wi_keys[i].wi_keydat,
3558a1b9b6aSSam Leffler 			    ic->ic_nw_keys[i].wk_key,
3568a1b9b6aSSam Leffler 			    ic->ic_nw_keys[i].wk_keylen);
3571a1e1d21SSam Leffler 		}
3581a1e1d21SSam Leffler 		wreq.wi_len = sizeof(*keys) / 2;
3591a1e1d21SSam Leffler 		break;
3601a1e1d21SSam Leffler 	case WI_RID_MAX_DATALEN:
3618a1b9b6aSSam Leffler 		wreq.wi_val[0] = htole16(ic->ic_fragthreshold);
3621a1e1d21SSam Leffler 		wreq.wi_len = 1;
3631a1e1d21SSam Leffler 		break;
3641a1e1d21SSam Leffler 	case WI_RID_IFACE_STATS:
3651a1e1d21SSam Leffler 		/* XXX: should be implemented in lower drivers */
3661a1e1d21SSam Leffler 		break;
3671a1e1d21SSam Leffler 	case WI_RID_READ_APS:
3681a1e1d21SSam Leffler 		/*
3691a1e1d21SSam Leffler 		 * Don't return results until active scan completes.
3701a1e1d21SSam Leffler 		 */
3718a1b9b6aSSam Leffler 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
3728a1b9b6aSSam Leffler 			struct wi_read_ap_args args;
3738a1b9b6aSSam Leffler 
3748a1b9b6aSSam Leffler 			args.i = 0;
3758a1b9b6aSSam Leffler 			args.ap = (void *)((char *)wreq.wi_val + sizeof(i));
3768a1b9b6aSSam Leffler 			args.max = (void *)(&wreq + 1);
3778a1b9b6aSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_scan,
3788a1b9b6aSSam Leffler 				wi_read_ap_result, &args);
3798a1b9b6aSSam Leffler 			memcpy(wreq.wi_val, &args.i, sizeof(args.i));
3808a1b9b6aSSam Leffler 			wreq.wi_len = (sizeof(int) +
3818a1b9b6aSSam Leffler 				sizeof(struct wi_apinfo) * args.i) / 2;
3828a1b9b6aSSam Leffler 		} else
3831a1e1d21SSam Leffler 			error = EINPROGRESS;
3841a1e1d21SSam Leffler 		break;
3851a1e1d21SSam Leffler 	case WI_RID_PRISM2:
3868a1b9b6aSSam Leffler 		/* NB: we lie so WI_RID_SCAN_RES can include rates */
3878a1b9b6aSSam Leffler 		wreq.wi_val[0] = 1;
3881a1e1d21SSam Leffler 		wreq.wi_len = sizeof(u_int16_t) / 2;
3891a1e1d21SSam Leffler 		break;
3901a1e1d21SSam Leffler 	case WI_RID_SCAN_RES:			/* compatibility interface */
3918a1b9b6aSSam Leffler 		if ((ic->ic_flags & (IEEE80211_F_SCAN|IEEE80211_F_ASCAN)) == 0) {
3928a1b9b6aSSam Leffler 			struct wi_read_prism2_args args;
3938a1b9b6aSSam Leffler 			struct wi_scan_p2_hdr *p2;
3948a1b9b6aSSam Leffler 
3958a1b9b6aSSam Leffler 			/* NB: use Prism2 format so we can include rate info */
3968a1b9b6aSSam Leffler 			p2 = (struct wi_scan_p2_hdr *)wreq.wi_val;
3978a1b9b6aSSam Leffler 			args.i = 0;
3988a1b9b6aSSam Leffler 			args.res = (void *)&p2[1];
3998a1b9b6aSSam Leffler 			args.max = (void *)(&wreq + 1);
4008a1b9b6aSSam Leffler 			ieee80211_iterate_nodes(&ic->ic_scan,
4018a1b9b6aSSam Leffler 				wi_read_prism2_result, &args);
4028a1b9b6aSSam Leffler 			p2->wi_rsvd = 0;
4038a1b9b6aSSam Leffler 			p2->wi_reason = args.i;
4048a1b9b6aSSam Leffler 			wreq.wi_len = (sizeof(*p2) +
4058a1b9b6aSSam Leffler 				sizeof(struct wi_scan_res) * args.i) / 2;
4068a1b9b6aSSam Leffler 		} else
4071a1e1d21SSam Leffler 			error = EINPROGRESS;
4081a1e1d21SSam Leffler 		break;
4098a1b9b6aSSam Leffler 	case WI_RID_READ_CACHE: {
4108a1b9b6aSSam Leffler 		struct wi_read_sigcache_args args;
4118a1b9b6aSSam Leffler 		args.i = 0;
4128a1b9b6aSSam Leffler 		args.wsc = (struct wi_sigcache *) wreq.wi_val;
4138a1b9b6aSSam Leffler 		args.max = (void *)(&wreq + 1);
4148a1b9b6aSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_scan, wi_read_sigcache, &args);
4158a1b9b6aSSam Leffler 		wreq.wi_len = sizeof(struct wi_sigcache) * args.i / 2;
4168a1b9b6aSSam Leffler 		break;
4171a1e1d21SSam Leffler 	}
4181a1e1d21SSam Leffler 	default:
4191a1e1d21SSam Leffler 		error = EINVAL;
4201a1e1d21SSam Leffler 		break;
4211a1e1d21SSam Leffler 	}
4221a1e1d21SSam Leffler 	if (error == 0) {
4231a1e1d21SSam Leffler 		wreq.wi_len++;
4241a1e1d21SSam Leffler 		error = copyout(&wreq, ifr->ifr_data, sizeof(wreq));
4251a1e1d21SSam Leffler 	}
4261a1e1d21SSam Leffler 	return error;
4271a1e1d21SSam Leffler }
4281a1e1d21SSam Leffler 
4291a1e1d21SSam Leffler static int
4301a1e1d21SSam Leffler findrate(struct ieee80211com *ic, enum ieee80211_phymode mode, int rate)
4311a1e1d21SSam Leffler {
4321a1e1d21SSam Leffler #define	IEEERATE(_ic,_m,_i) \
4331a1e1d21SSam Leffler 	((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
4341a1e1d21SSam Leffler 	int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
4351a1e1d21SSam Leffler 	for (i = 0; i < nrates; i++)
4361a1e1d21SSam Leffler 		if (IEEERATE(ic, mode, i) == rate)
4371a1e1d21SSam Leffler 			return i;
4381a1e1d21SSam Leffler 	return -1;
4391a1e1d21SSam Leffler #undef IEEERATE
4401a1e1d21SSam Leffler }
4411a1e1d21SSam Leffler 
44293685685SSam Leffler /*
44393685685SSam Leffler  * Prepare to do a user-initiated scan for AP's.  If no
44493685685SSam Leffler  * current/default channel is setup or the current channel
44593685685SSam Leffler  * is invalid then pick the first available channel from
44693685685SSam Leffler  * the active list as the place to start the scan.
44793685685SSam Leffler  */
44893685685SSam Leffler static int
4498a1b9b6aSSam Leffler ieee80211_setupscan(struct ieee80211com *ic, const u_int8_t chanlist[])
45093685685SSam Leffler {
45193685685SSam Leffler 
4528a1b9b6aSSam Leffler 	/*
4538a1b9b6aSSam Leffler 	 * XXX don't permit a scan to be started unless we
4548a1b9b6aSSam Leffler 	 * know the device is ready.  For the moment this means
4558a1b9b6aSSam Leffler 	 * the device is marked up as this is the required to
4568a1b9b6aSSam Leffler 	 * initialize the hardware.  It would be better to permit
4578a1b9b6aSSam Leffler 	 * scanning prior to being up but that'll require some
4588a1b9b6aSSam Leffler 	 * changes to the infrastructure.
4598a1b9b6aSSam Leffler 	 */
4608a1b9b6aSSam Leffler 	if (!IS_UP(ic))
4618a1b9b6aSSam Leffler 		return EINVAL;
4628a1b9b6aSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
46393685685SSam Leffler 	/*
4648a1b9b6aSSam Leffler 	 * We force the state to INIT before calling ieee80211_new_state
4658a1b9b6aSSam Leffler 	 * to get ieee80211_begin_scan called.  We really want to scan w/o
4668a1b9b6aSSam Leffler 	 * altering the current state but that's not possible right now.
46793685685SSam Leffler 	 */
4688a1b9b6aSSam Leffler 	/* XXX handle proberequest case */
4698a1b9b6aSSam Leffler 	ic->ic_state = IEEE80211_S_INIT;	/* XXX bypass state machine */
4708a1b9b6aSSam Leffler 	return 0;
47193685685SSam Leffler }
47293685685SSam Leffler 
4731a1e1d21SSam Leffler int
4748a1b9b6aSSam Leffler ieee80211_cfgset(struct ieee80211com *ic, u_long cmd, caddr_t data)
4751a1e1d21SSam Leffler {
4768a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
4771a1e1d21SSam Leffler 	int i, j, len, error, rate;
4781a1e1d21SSam Leffler 	struct ifreq *ifr = (struct ifreq *)data;
4791a1e1d21SSam Leffler 	struct wi_ltv_keys *keys;
4801a1e1d21SSam Leffler 	struct wi_req wreq;
4811a1e1d21SSam Leffler 	u_char chanlist[roundup(IEEE80211_CHAN_MAX, NBBY)];
4821a1e1d21SSam Leffler 
4831a1e1d21SSam Leffler 	error = copyin(ifr->ifr_data, &wreq, sizeof(wreq));
4841a1e1d21SSam Leffler 	if (error)
4851a1e1d21SSam Leffler 		return error;
4861a1e1d21SSam Leffler 	len = wreq.wi_len ? (wreq.wi_len - 1) * 2 : 0;
4871a1e1d21SSam Leffler 	switch (wreq.wi_type) {
4881a1e1d21SSam Leffler 	case WI_RID_SERIALNO:
4891a1e1d21SSam Leffler 	case WI_RID_NODENAME:
4901a1e1d21SSam Leffler 		return EPERM;
4911a1e1d21SSam Leffler 	case WI_RID_CURRENT_SSID:
4921a1e1d21SSam Leffler 		return EPERM;
4931a1e1d21SSam Leffler 	case WI_RID_OWN_SSID:
4941a1e1d21SSam Leffler 	case WI_RID_DESIRED_SSID:
4951a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) * 2 > len ||
4961a1e1d21SSam Leffler 		    le16toh(wreq.wi_val[0]) > IEEE80211_NWID_LEN) {
4971a1e1d21SSam Leffler 			error = ENOSPC;
4981a1e1d21SSam Leffler 			break;
4991a1e1d21SSam Leffler 		}
5001a1e1d21SSam Leffler 		memset(ic->ic_des_essid, 0, sizeof(ic->ic_des_essid));
5011a1e1d21SSam Leffler 		ic->ic_des_esslen = le16toh(wreq.wi_val[0]) * 2;
502b9ee58c4SSam Leffler 		memcpy(ic->ic_des_essid, &wreq.wi_val[1], ic->ic_des_esslen);
5031a1e1d21SSam Leffler 		error = ENETRESET;
5041a1e1d21SSam Leffler 		break;
5051a1e1d21SSam Leffler 	case WI_RID_CURRENT_BSSID:
5061a1e1d21SSam Leffler 		return EPERM;
5071a1e1d21SSam Leffler 	case WI_RID_OWN_CHNL:
5081a1e1d21SSam Leffler 		if (len != 2)
5091a1e1d21SSam Leffler 			return EINVAL;
5101a1e1d21SSam Leffler 		i = le16toh(wreq.wi_val[0]);
5111a1e1d21SSam Leffler 		if (i < 0 ||
5121a1e1d21SSam Leffler 		    i > IEEE80211_CHAN_MAX ||
5131a1e1d21SSam Leffler 		    isclr(ic->ic_chan_active, i))
5141a1e1d21SSam Leffler 			return EINVAL;
5151a1e1d21SSam Leffler 		ic->ic_ibss_chan = &ic->ic_channels[i];
5168a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_MONITOR)
5178a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
5188a1b9b6aSSam Leffler 		else
5191a1e1d21SSam Leffler 			error = ENETRESET;
5201a1e1d21SSam Leffler 		break;
5211a1e1d21SSam Leffler 	case WI_RID_CURRENT_CHAN:
5221a1e1d21SSam Leffler 		return EPERM;
5231a1e1d21SSam Leffler 	case WI_RID_COMMS_QUALITY:
5241a1e1d21SSam Leffler 		return EPERM;
5251a1e1d21SSam Leffler 	case WI_RID_PROMISC:
5261a1e1d21SSam Leffler 		if (len != 2)
5271a1e1d21SSam Leffler 			return EINVAL;
5281a1e1d21SSam Leffler 		if (ifp->if_flags & IFF_PROMISC) {
5291a1e1d21SSam Leffler 			if (wreq.wi_val[0] == 0) {
5301a1e1d21SSam Leffler 				ifp->if_flags &= ~IFF_PROMISC;
5311a1e1d21SSam Leffler 				error = ENETRESET;
5321a1e1d21SSam Leffler 			}
5331a1e1d21SSam Leffler 		} else {
5341a1e1d21SSam Leffler 			if (wreq.wi_val[0] != 0) {
5351a1e1d21SSam Leffler 				ifp->if_flags |= IFF_PROMISC;
5361a1e1d21SSam Leffler 				error = ENETRESET;
5371a1e1d21SSam Leffler 			}
5381a1e1d21SSam Leffler 		}
5391a1e1d21SSam Leffler 		break;
5401a1e1d21SSam Leffler 	case WI_RID_PORTTYPE:
5411a1e1d21SSam Leffler 		if (len != 2)
5421a1e1d21SSam Leffler 			return EINVAL;
5431a1e1d21SSam Leffler 		switch (le16toh(wreq.wi_val[0])) {
5441a1e1d21SSam Leffler 		case IEEE80211_M_STA:
5451a1e1d21SSam Leffler 			break;
5461a1e1d21SSam Leffler 		case IEEE80211_M_IBSS:
5471a1e1d21SSam Leffler 			if (!(ic->ic_caps & IEEE80211_C_IBSS))
5481a1e1d21SSam Leffler 				return EINVAL;
5491a1e1d21SSam Leffler 			break;
5501a1e1d21SSam Leffler 		case IEEE80211_M_AHDEMO:
5511a1e1d21SSam Leffler 			if (ic->ic_phytype != IEEE80211_T_DS ||
5521a1e1d21SSam Leffler 			    !(ic->ic_caps & IEEE80211_C_AHDEMO))
5531a1e1d21SSam Leffler 				return EINVAL;
5541a1e1d21SSam Leffler 			break;
5551a1e1d21SSam Leffler 		case IEEE80211_M_HOSTAP:
5561a1e1d21SSam Leffler 			if (!(ic->ic_caps & IEEE80211_C_HOSTAP))
5571a1e1d21SSam Leffler 				return EINVAL;
5581a1e1d21SSam Leffler 			break;
5591a1e1d21SSam Leffler 		default:
5601a1e1d21SSam Leffler 			return EINVAL;
5611a1e1d21SSam Leffler 		}
5621a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != ic->ic_opmode) {
5631a1e1d21SSam Leffler 			ic->ic_opmode = le16toh(wreq.wi_val[0]);
5648a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
5651a1e1d21SSam Leffler 		}
5661a1e1d21SSam Leffler 		break;
5671a1e1d21SSam Leffler #if 0
5681a1e1d21SSam Leffler 	case WI_RID_MAC_NODE:
5691a1e1d21SSam Leffler 		if (len != IEEE80211_ADDR_LEN)
5701a1e1d21SSam Leffler 			return EINVAL;
5711a1e1d21SSam Leffler 		IEEE80211_ADDR_COPY(LLADDR(ifp->if_sadl), wreq.wi_val);
5721a1e1d21SSam Leffler 		/* if_init will copy lladdr into ic_myaddr */
5731a1e1d21SSam Leffler 		error = ENETRESET;
5741a1e1d21SSam Leffler 		break;
5751a1e1d21SSam Leffler #endif
5761a1e1d21SSam Leffler 	case WI_RID_TX_RATE:
5771a1e1d21SSam Leffler 		if (len != 2)
5781a1e1d21SSam Leffler 			return EINVAL;
5791a1e1d21SSam Leffler 		if (wreq.wi_val[0] == 0) {
5801a1e1d21SSam Leffler 			/* auto */
5812c39b32cSSam Leffler 			ic->ic_fixed_rate = IEEE80211_FIXED_RATE_NONE;
5821a1e1d21SSam Leffler 			break;
5831a1e1d21SSam Leffler 		}
5841a1e1d21SSam Leffler 		rate = 2 * le16toh(wreq.wi_val[0]);
5851a1e1d21SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_AUTO) {
5861a1e1d21SSam Leffler 			/*
5871a1e1d21SSam Leffler 			 * In autoselect mode search for the rate.  We take
5881a1e1d21SSam Leffler 			 * the first instance which may not be right, but we
5891a1e1d21SSam Leffler 			 * are limited by the interface.  Note that we also
5901a1e1d21SSam Leffler 			 * lock the mode to insure the rate is meaningful
5911a1e1d21SSam Leffler 			 * when it is used.
5921a1e1d21SSam Leffler 			 */
5931a1e1d21SSam Leffler 			for (j = IEEE80211_MODE_11A;
5941a1e1d21SSam Leffler 			     j < IEEE80211_MODE_MAX; j++) {
5951a1e1d21SSam Leffler 				if ((ic->ic_modecaps & (1<<j)) == 0)
5961a1e1d21SSam Leffler 					continue;
5971a1e1d21SSam Leffler 				i = findrate(ic, j, rate);
5981a1e1d21SSam Leffler 				if (i != -1) {
5991a1e1d21SSam Leffler 					/* lock mode too */
6001a1e1d21SSam Leffler 					ic->ic_curmode = j;
6011a1e1d21SSam Leffler 					goto setrate;
6021a1e1d21SSam Leffler 				}
6031a1e1d21SSam Leffler 			}
6041a1e1d21SSam Leffler 		} else {
6051a1e1d21SSam Leffler 			i = findrate(ic, ic->ic_curmode, rate);
6061a1e1d21SSam Leffler 			if (i != -1)
6071a1e1d21SSam Leffler 				goto setrate;
6081a1e1d21SSam Leffler 		}
6091a1e1d21SSam Leffler 		return EINVAL;
6101a1e1d21SSam Leffler 	setrate:
6111a1e1d21SSam Leffler 		ic->ic_fixed_rate = i;
6128a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6131a1e1d21SSam Leffler 		break;
6141a1e1d21SSam Leffler 	case WI_RID_CUR_TX_RATE:
6151a1e1d21SSam Leffler 		return EPERM;
6161a1e1d21SSam Leffler 	case WI_RID_RTS_THRESH:
6171a1e1d21SSam Leffler 		if (len != 2)
6181a1e1d21SSam Leffler 			return EINVAL;
6191a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != IEEE80211_MAX_LEN)
6201a1e1d21SSam Leffler 			return EINVAL;		/* TODO: RTS */
6211a1e1d21SSam Leffler 		break;
6221a1e1d21SSam Leffler 	case WI_RID_CREATE_IBSS:
6231a1e1d21SSam Leffler 		if (len != 2)
6241a1e1d21SSam Leffler 			return EINVAL;
6251a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
6261a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_IBSS) == 0)
6271a1e1d21SSam Leffler 				return EINVAL;
6281a1e1d21SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_IBSSON) == 0) {
6291a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_IBSSON;
6301a1e1d21SSam Leffler 				if (ic->ic_opmode == IEEE80211_M_IBSS &&
6311a1e1d21SSam Leffler 				    ic->ic_state == IEEE80211_S_SCAN)
6328a1b9b6aSSam Leffler 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
6331a1e1d21SSam Leffler 			}
6341a1e1d21SSam Leffler 		} else {
6351a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_IBSSON) {
6361a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_IBSSON;
6371a1e1d21SSam Leffler 				if (ic->ic_flags & IEEE80211_F_SIBSS) {
6381a1e1d21SSam Leffler 					ic->ic_flags &= ~IEEE80211_F_SIBSS;
6398a1b9b6aSSam Leffler 					error = IS_UP_AUTO(ic) ? ENETRESET : 0;
6401a1e1d21SSam Leffler 				}
6411a1e1d21SSam Leffler 			}
6421a1e1d21SSam Leffler 		}
6431a1e1d21SSam Leffler 		break;
6441a1e1d21SSam Leffler 	case WI_RID_MICROWAVE_OVEN:
6451a1e1d21SSam Leffler 		if (len != 2)
6461a1e1d21SSam Leffler 			return EINVAL;
6471a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0)
6481a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6491a1e1d21SSam Leffler 		break;
6501a1e1d21SSam Leffler 	case WI_RID_ROAMING_MODE:
6511a1e1d21SSam Leffler 		if (len != 2)
6521a1e1d21SSam Leffler 			return EINVAL;
6538a1b9b6aSSam Leffler 		i = le16toh(wreq.wi_val[0]);
6548a1b9b6aSSam Leffler 		if (i > IEEE80211_ROAMING_MANUAL)
6551a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6568a1b9b6aSSam Leffler 		ic->ic_roaming = i;
6571a1e1d21SSam Leffler 		break;
6581a1e1d21SSam Leffler 	case WI_RID_SYSTEM_SCALE:
6591a1e1d21SSam Leffler 		if (len != 2)
6601a1e1d21SSam Leffler 			return EINVAL;
6611a1e1d21SSam Leffler 		if (le16toh(wreq.wi_val[0]) != 1)
6621a1e1d21SSam Leffler 			return EINVAL;		/* not supported */
6631a1e1d21SSam Leffler 		break;
6641a1e1d21SSam Leffler 	case WI_RID_PM_ENABLED:
6651a1e1d21SSam Leffler 		if (len != 2)
6661a1e1d21SSam Leffler 			return EINVAL;
6671a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
6681a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
6691a1e1d21SSam Leffler 				return EINVAL;
6701a1e1d21SSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
6711a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_PMGTON;
6728a1b9b6aSSam Leffler 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6731a1e1d21SSam Leffler 			}
6741a1e1d21SSam Leffler 		} else {
6751a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
6761a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
6778a1b9b6aSSam Leffler 				error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6781a1e1d21SSam Leffler 			}
6791a1e1d21SSam Leffler 		}
6801a1e1d21SSam Leffler 		break;
6811a1e1d21SSam Leffler 	case WI_RID_MAX_SLEEP:
6821a1e1d21SSam Leffler 		if (len != 2)
6831a1e1d21SSam Leffler 			return EINVAL;
6841a1e1d21SSam Leffler 		ic->ic_lintval = le16toh(wreq.wi_val[0]);
6851a1e1d21SSam Leffler 		if (ic->ic_flags & IEEE80211_F_PMGTON)
6868a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
6871a1e1d21SSam Leffler 		break;
6881a1e1d21SSam Leffler 	case WI_RID_CUR_BEACON_INT:
6891a1e1d21SSam Leffler 		return EPERM;
6901a1e1d21SSam Leffler 	case WI_RID_WEP_AVAIL:
6911a1e1d21SSam Leffler 		return EPERM;
6921a1e1d21SSam Leffler 	case WI_RID_CNFAUTHMODE:
6931a1e1d21SSam Leffler 		if (len != 2)
6941a1e1d21SSam Leffler 			return EINVAL;
6958a1b9b6aSSam Leffler 		i = le16toh(wreq.wi_val[0]);
6968a1b9b6aSSam Leffler 		if (i > IEEE80211_AUTH_WPA)
6978a1b9b6aSSam Leffler 			return EINVAL;
6988a1b9b6aSSam Leffler 		ic->ic_bss->ni_authmode = i;		/* XXX ENETRESET? */
6998a1b9b6aSSam Leffler 		error = ENETRESET;
7001a1e1d21SSam Leffler 		break;
7011a1e1d21SSam Leffler 	case WI_RID_ENCRYPTION:
7021a1e1d21SSam Leffler 		if (len != 2)
7031a1e1d21SSam Leffler 			return EINVAL;
7041a1e1d21SSam Leffler 		if (wreq.wi_val[0] != 0) {
7051a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WEP) == 0)
7061a1e1d21SSam Leffler 				return EINVAL;
7078a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0) {
7088a1b9b6aSSam Leffler 				ic->ic_flags |= IEEE80211_F_PRIVACY;
7091a1e1d21SSam Leffler 				error = ENETRESET;
7101a1e1d21SSam Leffler 			}
7111a1e1d21SSam Leffler 		} else {
7128a1b9b6aSSam Leffler 			if (ic->ic_flags & IEEE80211_F_PRIVACY) {
7138a1b9b6aSSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PRIVACY;
7141a1e1d21SSam Leffler 				error = ENETRESET;
7151a1e1d21SSam Leffler 			}
7161a1e1d21SSam Leffler 		}
7171a1e1d21SSam Leffler 		break;
7181a1e1d21SSam Leffler 	case WI_RID_TX_CRYPT_KEY:
7191a1e1d21SSam Leffler 		if (len != 2)
7201a1e1d21SSam Leffler 			return EINVAL;
7211a1e1d21SSam Leffler 		i = le16toh(wreq.wi_val[0]);
7221a1e1d21SSam Leffler 		if (i >= IEEE80211_WEP_NKID)
7231a1e1d21SSam Leffler 			return EINVAL;
7248a1b9b6aSSam Leffler 		ic->ic_def_txkey = i;
7258a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
7261a1e1d21SSam Leffler 		break;
7271a1e1d21SSam Leffler 	case WI_RID_DEFLT_CRYPT_KEYS:
7281a1e1d21SSam Leffler 		if (len != sizeof(struct wi_ltv_keys))
7291a1e1d21SSam Leffler 			return EINVAL;
7301a1e1d21SSam Leffler 		keys = (struct wi_ltv_keys *)&wreq;
7311a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
7321a1e1d21SSam Leffler 			len = le16toh(keys->wi_keys[i].wi_keylen);
7331a1e1d21SSam Leffler 			if (len != 0 && len < IEEE80211_WEP_KEYLEN)
7341a1e1d21SSam Leffler 				return EINVAL;
7358a1b9b6aSSam Leffler 			if (len > IEEE80211_KEYBUF_SIZE)
7361a1e1d21SSam Leffler 				return EINVAL;
7371a1e1d21SSam Leffler 		}
7381a1e1d21SSam Leffler 		for (i = 0; i < IEEE80211_WEP_NKID; i++) {
7398a1b9b6aSSam Leffler 			struct ieee80211_key *k = &ic->ic_nw_keys[i];
7408a1b9b6aSSam Leffler 
7411a1e1d21SSam Leffler 			len = le16toh(keys->wi_keys[i].wi_keylen);
7428a1b9b6aSSam Leffler 			k->wk_keylen = len;
7438a1b9b6aSSam Leffler 			k->wk_flags = IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV;
7448a1b9b6aSSam Leffler 			memset(k->wk_key, 0, sizeof(k->wk_key));
7458a1b9b6aSSam Leffler 			memcpy(k->wk_key, keys->wi_keys[i].wi_keydat, len);
7468a1b9b6aSSam Leffler #if 0
7478a1b9b6aSSam Leffler 			k->wk_type = IEEE80211_CIPHER_WEP;
7488a1b9b6aSSam Leffler #endif
7491a1e1d21SSam Leffler 		}
7501a1e1d21SSam Leffler 		error = ENETRESET;
7511a1e1d21SSam Leffler 		break;
7521a1e1d21SSam Leffler 	case WI_RID_MAX_DATALEN:
7531a1e1d21SSam Leffler 		if (len != 2)
7541a1e1d21SSam Leffler 			return EINVAL;
7551a1e1d21SSam Leffler 		len = le16toh(wreq.wi_val[0]);
7561a1e1d21SSam Leffler 		if (len < 350 /* ? */ || len > IEEE80211_MAX_LEN)
7571a1e1d21SSam Leffler 			return EINVAL;
7581a1e1d21SSam Leffler 		ic->ic_fragthreshold = len;
7598a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
7601a1e1d21SSam Leffler 		break;
7611a1e1d21SSam Leffler 	case WI_RID_IFACE_STATS:
7621a1e1d21SSam Leffler 		error = EPERM;
7631a1e1d21SSam Leffler 		break;
7641a1e1d21SSam Leffler 	case WI_RID_SCAN_REQ:			/* XXX wicontrol */
7651a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
7661a1e1d21SSam Leffler 			break;
7678a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
76893685685SSam Leffler 		if (error == 0)
769a11c9a5cSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
7701a1e1d21SSam Leffler 		break;
7711a1e1d21SSam Leffler 	case WI_RID_SCAN_APS:
7721a1e1d21SSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)
7731a1e1d21SSam Leffler 			break;
7741a1e1d21SSam Leffler 		len--;			/* XXX: tx rate? */
7751a1e1d21SSam Leffler 		/* FALLTHRU */
7761a1e1d21SSam Leffler 	case WI_RID_CHANNEL_LIST:
7771a1e1d21SSam Leffler 		memset(chanlist, 0, sizeof(chanlist));
7781a1e1d21SSam Leffler 		/*
7791a1e1d21SSam Leffler 		 * Since channel 0 is not available for DS, channel 1
7801a1e1d21SSam Leffler 		 * is assigned to LSB on WaveLAN.
7811a1e1d21SSam Leffler 		 */
7821a1e1d21SSam Leffler 		if (ic->ic_phytype == IEEE80211_T_DS)
7831a1e1d21SSam Leffler 			i = 1;
7841a1e1d21SSam Leffler 		else
7851a1e1d21SSam Leffler 			i = 0;
7861a1e1d21SSam Leffler 		for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
7871a1e1d21SSam Leffler 			if ((j / 8) >= len)
7881a1e1d21SSam Leffler 				break;
7891a1e1d21SSam Leffler 			if (isclr((u_int8_t *)wreq.wi_val, j))
7901a1e1d21SSam Leffler 				continue;
7911a1e1d21SSam Leffler 			if (isclr(ic->ic_chan_active, i)) {
7921a1e1d21SSam Leffler 				if (wreq.wi_type != WI_RID_CHANNEL_LIST)
7931a1e1d21SSam Leffler 					continue;
7941a1e1d21SSam Leffler 				if (isclr(ic->ic_chan_avail, i))
7951a1e1d21SSam Leffler 					return EPERM;
7961a1e1d21SSam Leffler 			}
7971a1e1d21SSam Leffler 			setbit(chanlist, i);
7981a1e1d21SSam Leffler 		}
7998a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, chanlist);
80093685685SSam Leffler 		if (wreq.wi_type == WI_RID_CHANNEL_LIST) {
80193685685SSam Leffler 			/* NB: ignore error from ieee80211_setupscan */
8021a1e1d21SSam Leffler 			error = ENETRESET;
80393685685SSam Leffler 		} else if (error == 0)
804a11c9a5cSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
8051a1e1d21SSam Leffler 		break;
8061a1e1d21SSam Leffler 	default:
8071a1e1d21SSam Leffler 		error = EINVAL;
8081a1e1d21SSam Leffler 		break;
8091a1e1d21SSam Leffler 	}
8108a1b9b6aSSam Leffler 	if (error == ENETRESET && !IS_UP_AUTO(ic))
8118a1b9b6aSSam Leffler 		error = 0;
8121a1e1d21SSam Leffler 	return error;
8131a1e1d21SSam Leffler }
8141a1e1d21SSam Leffler 
8158a1b9b6aSSam Leffler static int
8168a1b9b6aSSam Leffler cap2cipher(int flag)
8178a1b9b6aSSam Leffler {
8188a1b9b6aSSam Leffler 	switch (flag) {
8198a1b9b6aSSam Leffler 	case IEEE80211_C_WEP:		return IEEE80211_CIPHER_WEP;
8208a1b9b6aSSam Leffler 	case IEEE80211_C_AES:		return IEEE80211_CIPHER_AES_OCB;
8218a1b9b6aSSam Leffler 	case IEEE80211_C_AES_CCM:	return IEEE80211_CIPHER_AES_CCM;
8228a1b9b6aSSam Leffler 	case IEEE80211_C_CKIP:		return IEEE80211_CIPHER_CKIP;
8238a1b9b6aSSam Leffler 	case IEEE80211_C_TKIP:		return IEEE80211_CIPHER_TKIP;
8248a1b9b6aSSam Leffler 	}
8258a1b9b6aSSam Leffler 	return -1;
8268a1b9b6aSSam Leffler }
8278a1b9b6aSSam Leffler 
8288a1b9b6aSSam Leffler static int
8298a1b9b6aSSam Leffler ieee80211_ioctl_getkey(struct ieee80211com *ic, struct ieee80211req *ireq)
8308a1b9b6aSSam Leffler {
8318a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
8328a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
8338a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
8348a1b9b6aSSam Leffler 	const struct ieee80211_cipher *cip;
8358a1b9b6aSSam Leffler 	u_int kid;
8368a1b9b6aSSam Leffler 	int error;
8378a1b9b6aSSam Leffler 
8388a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
8398a1b9b6aSSam Leffler 		return EINVAL;
8408a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
8418a1b9b6aSSam Leffler 	if (error)
8428a1b9b6aSSam Leffler 		return error;
8438a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
8448a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
845acc4f7f5SSam Leffler 		ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
8468a1b9b6aSSam Leffler 		if (ni == NULL)
8478a1b9b6aSSam Leffler 			return EINVAL;		/* XXX */
8488a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
8498a1b9b6aSSam Leffler 	} else {
8508a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
8518a1b9b6aSSam Leffler 			return EINVAL;
8528a1b9b6aSSam Leffler 		wk = &ic->ic_nw_keys[kid];
8538a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(&ik.ik_macaddr, ic->ic_bss->ni_macaddr);
8548a1b9b6aSSam Leffler 		ni = NULL;
8558a1b9b6aSSam Leffler 	}
8568a1b9b6aSSam Leffler 	cip = wk->wk_cipher;
8578a1b9b6aSSam Leffler 	ik.ik_type = cip->ic_cipher;
8588a1b9b6aSSam Leffler 	ik.ik_keylen = wk->wk_keylen;
8598a1b9b6aSSam Leffler 	ik.ik_flags = wk->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV);
8608a1b9b6aSSam Leffler 	if (wk->wk_keyix == ic->ic_def_txkey)
8618a1b9b6aSSam Leffler 		ik.ik_flags |= IEEE80211_KEY_DEFAULT;
8628a1b9b6aSSam Leffler 	if (suser(curthread) == 0) {
8638a1b9b6aSSam Leffler 		/* NB: only root can read key data */
8648a1b9b6aSSam Leffler 		ik.ik_keyrsc = wk->wk_keyrsc;
8658a1b9b6aSSam Leffler 		ik.ik_keytsc = wk->wk_keytsc;
8668a1b9b6aSSam Leffler 		memcpy(ik.ik_keydata, wk->wk_key, wk->wk_keylen);
8678a1b9b6aSSam Leffler 		if (cip->ic_cipher == IEEE80211_CIPHER_TKIP) {
8688a1b9b6aSSam Leffler 			memcpy(ik.ik_keydata+wk->wk_keylen,
8698a1b9b6aSSam Leffler 				wk->wk_key + IEEE80211_KEYBUF_SIZE,
8708a1b9b6aSSam Leffler 				IEEE80211_MICBUF_SIZE);
8718a1b9b6aSSam Leffler 			ik.ik_keylen += IEEE80211_MICBUF_SIZE;
8728a1b9b6aSSam Leffler 		}
8738a1b9b6aSSam Leffler 	} else {
8748a1b9b6aSSam Leffler 		ik.ik_keyrsc = 0;
8758a1b9b6aSSam Leffler 		ik.ik_keytsc = 0;
8768a1b9b6aSSam Leffler 		memset(ik.ik_keydata, 0, sizeof(ik.ik_keydata));
8778a1b9b6aSSam Leffler 	}
8788a1b9b6aSSam Leffler 	if (ni != NULL)
8798a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
8808a1b9b6aSSam Leffler 	return copyout(&ik, ireq->i_data, sizeof(ik));
8818a1b9b6aSSam Leffler }
8828a1b9b6aSSam Leffler 
8838a1b9b6aSSam Leffler static int
8848a1b9b6aSSam Leffler ieee80211_ioctl_getchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
8858a1b9b6aSSam Leffler {
8868a1b9b6aSSam Leffler 
887e55e5e42SSam Leffler 	if (sizeof(ic->ic_chan_active) < ireq->i_len)
8888a1b9b6aSSam Leffler 		ireq->i_len = sizeof(ic->ic_chan_active);
8898a1b9b6aSSam Leffler 	return copyout(&ic->ic_chan_active, ireq->i_data, ireq->i_len);
8908a1b9b6aSSam Leffler }
8918a1b9b6aSSam Leffler 
8928a1b9b6aSSam Leffler static int
8938a1b9b6aSSam Leffler ieee80211_ioctl_getchaninfo(struct ieee80211com *ic, struct ieee80211req *ireq)
8948a1b9b6aSSam Leffler {
8958a1b9b6aSSam Leffler 	struct ieee80211req_chaninfo chans;	/* XXX off stack? */
8968a1b9b6aSSam Leffler 	int i, space;
8978a1b9b6aSSam Leffler 
8988a1b9b6aSSam Leffler 	/*
8998a1b9b6aSSam Leffler 	 * Since channel 0 is not available for DS, channel 1
9008a1b9b6aSSam Leffler 	 * is assigned to LSB on WaveLAN.
9018a1b9b6aSSam Leffler 	 */
9028a1b9b6aSSam Leffler 	if (ic->ic_phytype == IEEE80211_T_DS)
9038a1b9b6aSSam Leffler 		i = 1;
9048a1b9b6aSSam Leffler 	else
9058a1b9b6aSSam Leffler 		i = 0;
9068a1b9b6aSSam Leffler 	memset(&chans, 0, sizeof(chans));
9078a1b9b6aSSam Leffler 	for (; i <= IEEE80211_CHAN_MAX; i++)
9088a1b9b6aSSam Leffler 		if (isset(ic->ic_chan_avail, i)) {
9098a1b9b6aSSam Leffler 			struct ieee80211_channel *c = &ic->ic_channels[i];
9108a1b9b6aSSam Leffler 			chans.ic_chans[chans.ic_nchans].ic_freq = c->ic_freq;
9118a1b9b6aSSam Leffler 			chans.ic_chans[chans.ic_nchans].ic_flags = c->ic_flags;
9128a1b9b6aSSam Leffler 			chans.ic_nchans++;
9138a1b9b6aSSam Leffler 		}
9148a1b9b6aSSam Leffler 	space = __offsetof(struct ieee80211req_chaninfo,
9158a1b9b6aSSam Leffler 			ic_chans[chans.ic_nchans]);
9168a1b9b6aSSam Leffler 	if (space > ireq->i_len)
9178a1b9b6aSSam Leffler 		space = ireq->i_len;
9188a1b9b6aSSam Leffler 	return copyout(&chans, ireq->i_data, space);
9198a1b9b6aSSam Leffler }
9208a1b9b6aSSam Leffler 
9218a1b9b6aSSam Leffler static int
9228a1b9b6aSSam Leffler ieee80211_ioctl_getwpaie(struct ieee80211com *ic, struct ieee80211req *ireq)
9238a1b9b6aSSam Leffler {
9248a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9258a1b9b6aSSam Leffler 	struct ieee80211req_wpaie wpaie;
9268a1b9b6aSSam Leffler 	int error;
9278a1b9b6aSSam Leffler 
9288a1b9b6aSSam Leffler 	if (ireq->i_len < IEEE80211_ADDR_LEN)
9298a1b9b6aSSam Leffler 		return EINVAL;
9308a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, wpaie.wpa_macaddr, IEEE80211_ADDR_LEN);
9318a1b9b6aSSam Leffler 	if (error != 0)
9328a1b9b6aSSam Leffler 		return error;
933acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, wpaie.wpa_macaddr);
9348a1b9b6aSSam Leffler 	if (ni == NULL)
9358a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
9368a1b9b6aSSam Leffler 	memset(wpaie.wpa_ie, 0, sizeof(wpaie.wpa_ie));
9378a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL) {
9388a1b9b6aSSam Leffler 		int ielen = ni->ni_wpa_ie[1] + 2;
9398a1b9b6aSSam Leffler 		if (ielen > sizeof(wpaie.wpa_ie))
9408a1b9b6aSSam Leffler 			ielen = sizeof(wpaie.wpa_ie);
9418a1b9b6aSSam Leffler 		memcpy(wpaie.wpa_ie, ni->ni_wpa_ie, ielen);
9428a1b9b6aSSam Leffler 	}
9438a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
9448a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(wpaie))
9458a1b9b6aSSam Leffler 		ireq->i_len = sizeof(wpaie);
9468a1b9b6aSSam Leffler 	return copyout(&wpaie, ireq->i_data, ireq->i_len);
9478a1b9b6aSSam Leffler }
9488a1b9b6aSSam Leffler 
9498a1b9b6aSSam Leffler static int
9508a1b9b6aSSam Leffler ieee80211_ioctl_getstastats(struct ieee80211com *ic, struct ieee80211req *ireq)
9518a1b9b6aSSam Leffler {
9528a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
9538a1b9b6aSSam Leffler 	u_int8_t macaddr[IEEE80211_ADDR_LEN];
9548a1b9b6aSSam Leffler 	const int off = __offsetof(struct ieee80211req_sta_stats, is_stats);
9558a1b9b6aSSam Leffler 	int error;
9568a1b9b6aSSam Leffler 
9578a1b9b6aSSam Leffler 	if (ireq->i_len < off)
9588a1b9b6aSSam Leffler 		return EINVAL;
9598a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, macaddr, IEEE80211_ADDR_LEN);
9608a1b9b6aSSam Leffler 	if (error != 0)
9618a1b9b6aSSam Leffler 		return error;
962acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, macaddr);
9638a1b9b6aSSam Leffler 	if (ni == NULL)
9648a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
9658a1b9b6aSSam Leffler 	if (ireq->i_len > sizeof(struct ieee80211req_sta_stats))
9668a1b9b6aSSam Leffler 		ireq->i_len = sizeof(struct ieee80211req_sta_stats);
9678a1b9b6aSSam Leffler 	/* NB: copy out only the statistics */
9688a1b9b6aSSam Leffler 	error = copyout(&ni->ni_stats, (u_int8_t *) ireq->i_data + off,
9698a1b9b6aSSam Leffler 			ireq->i_len - off);
9708a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
9718a1b9b6aSSam Leffler 	return error;
9728a1b9b6aSSam Leffler }
9738a1b9b6aSSam Leffler 
974239cc3b6SSam Leffler #define COMPAT_FREEBSD6
975239cc3b6SSam Leffler #ifdef COMPAT_FREEBSD6
976239cc3b6SSam Leffler #define	IEEE80211_IOC_SCAN_RESULTS_OLD	24
977239cc3b6SSam Leffler 
978239cc3b6SSam Leffler struct scan_result_old {
979239cc3b6SSam Leffler 	u_int16_t	isr_len;		/* length (mult of 4) */
980239cc3b6SSam Leffler 	u_int16_t	isr_freq;		/* MHz */
981239cc3b6SSam Leffler 	u_int16_t	isr_flags;		/* channel flags */
982239cc3b6SSam Leffler 	u_int8_t	isr_noise;
983239cc3b6SSam Leffler 	u_int8_t	isr_rssi;
984239cc3b6SSam Leffler 	u_int8_t	isr_intval;		/* beacon interval */
985239cc3b6SSam Leffler 	u_int8_t	isr_capinfo;		/* capabilities */
986239cc3b6SSam Leffler 	u_int8_t	isr_erp;		/* ERP element */
987239cc3b6SSam Leffler 	u_int8_t	isr_bssid[IEEE80211_ADDR_LEN];
988239cc3b6SSam Leffler 	u_int8_t	isr_nrates;
989239cc3b6SSam Leffler 	u_int8_t	isr_rates[IEEE80211_RATE_MAXSIZE];
990239cc3b6SSam Leffler 	u_int8_t	isr_ssid_len;		/* SSID length */
991239cc3b6SSam Leffler 	u_int8_t	isr_ie_len;		/* IE length */
992239cc3b6SSam Leffler 	u_int8_t	isr_pad[5];
993239cc3b6SSam Leffler 	/* variable length SSID followed by IE data */
994239cc3b6SSam Leffler };
995239cc3b6SSam Leffler 
9968a1b9b6aSSam Leffler static void
997239cc3b6SSam Leffler old_get_scan_result(struct scan_result_old *sr,
9988a1b9b6aSSam Leffler 	const struct ieee80211_node *ni)
9998a1b9b6aSSam Leffler {
10008a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1001239cc3b6SSam Leffler 	u_int ielen;
10028a1b9b6aSSam Leffler 
10038a1b9b6aSSam Leffler 	memset(sr, 0, sizeof(*sr));
10048a1b9b6aSSam Leffler 	sr->isr_ssid_len = ni->ni_esslen;
1005239cc3b6SSam Leffler 	ielen = 0;
10068a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
10079238d0afSColin Percival 		ielen += 2+ni->ni_wpa_ie[1];
10088a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
10099238d0afSColin Percival 		ielen += 2+ni->ni_wme_ie[1];
1010239cc3b6SSam Leffler 	/* NB: beware of overflow, isr_ie_len is 8 bits */
1011239cc3b6SSam Leffler 	sr->isr_ie_len = (ielen > 255 ? 0 : ielen);
10128a1b9b6aSSam Leffler 	sr->isr_len = sizeof(*sr) + sr->isr_ssid_len + sr->isr_ie_len;
10138a1b9b6aSSam Leffler 	sr->isr_len = roundup(sr->isr_len, sizeof(u_int32_t));
10148a1b9b6aSSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
10158a1b9b6aSSam Leffler 		sr->isr_freq = ni->ni_chan->ic_freq;
10168a1b9b6aSSam Leffler 		sr->isr_flags = ni->ni_chan->ic_flags;
10178a1b9b6aSSam Leffler 	}
10188a1b9b6aSSam Leffler 	sr->isr_rssi = ic->ic_node_getrssi(ni);
10198a1b9b6aSSam Leffler 	sr->isr_intval = ni->ni_intval;
10208a1b9b6aSSam Leffler 	sr->isr_capinfo = ni->ni_capinfo;
10218a1b9b6aSSam Leffler 	sr->isr_erp = ni->ni_erp;
10228a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
10238a1b9b6aSSam Leffler 	sr->isr_nrates = ni->ni_rates.rs_nrates;
10248a1b9b6aSSam Leffler 	if (sr->isr_nrates > 15)
10258a1b9b6aSSam Leffler 		sr->isr_nrates = 15;
10268a1b9b6aSSam Leffler 	memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
10278a1b9b6aSSam Leffler }
10288a1b9b6aSSam Leffler 
10298a1b9b6aSSam Leffler static int
1030239cc3b6SSam Leffler old_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
10318a1b9b6aSSam Leffler {
10328a1b9b6aSSam Leffler 	union {
1033239cc3b6SSam Leffler 		struct scan_result_old res;
10348a1b9b6aSSam Leffler 		char data[512];		/* XXX shrink? */
10358a1b9b6aSSam Leffler 	} u;
1036239cc3b6SSam Leffler 	struct scan_result_old *sr = &u.res;
10378a1b9b6aSSam Leffler 	struct ieee80211_node_table *nt;
10388a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
10398a1b9b6aSSam Leffler 	int error, space;
10408a1b9b6aSSam Leffler 	u_int8_t *p, *cp;
10418a1b9b6aSSam Leffler 
10428a1b9b6aSSam Leffler 	p = ireq->i_data;
10438a1b9b6aSSam Leffler 	space = ireq->i_len;
10448a1b9b6aSSam Leffler 	error = 0;
10458a1b9b6aSSam Leffler 	/* XXX locking */
10468a1b9b6aSSam Leffler 	nt =  &ic->ic_scan;
10478a1b9b6aSSam Leffler 	TAILQ_FOREACH(ni, &nt->nt_node, ni_list) {
10488a1b9b6aSSam Leffler 		/* NB: skip pre-scan node state */
10498a1b9b6aSSam Leffler 		if (ni->ni_chan == IEEE80211_CHAN_ANYC)
10508a1b9b6aSSam Leffler 			continue;
1051239cc3b6SSam Leffler 		old_get_scan_result(sr, ni);
10528a1b9b6aSSam Leffler 		if (sr->isr_len > sizeof(u))
10538a1b9b6aSSam Leffler 			continue;		/* XXX */
10548a1b9b6aSSam Leffler 		if (space < sr->isr_len)
10558a1b9b6aSSam Leffler 			break;
10568a1b9b6aSSam Leffler 		cp = (u_int8_t *)(sr+1);
10578a1b9b6aSSam Leffler 		memcpy(cp, ni->ni_essid, ni->ni_esslen);
10588a1b9b6aSSam Leffler 		cp += ni->ni_esslen;
1059239cc3b6SSam Leffler 		if (sr->isr_ie_len) {
1060239cc3b6SSam Leffler 			if (ni->ni_wpa_ie != NULL) {
10618a1b9b6aSSam Leffler 				memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
10628a1b9b6aSSam Leffler 				cp += 2+ni->ni_wpa_ie[1];
10638a1b9b6aSSam Leffler 			}
1064239cc3b6SSam Leffler 			if (ni->ni_wme_ie != NULL) {
10658a1b9b6aSSam Leffler 				memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
10668a1b9b6aSSam Leffler 				cp += 2+ni->ni_wme_ie[1];
10678a1b9b6aSSam Leffler 			}
1068239cc3b6SSam Leffler 		}
10698a1b9b6aSSam Leffler 		error = copyout(sr, p, sr->isr_len);
10708a1b9b6aSSam Leffler 		if (error)
10718a1b9b6aSSam Leffler 			break;
10728a1b9b6aSSam Leffler 		p += sr->isr_len;
10738a1b9b6aSSam Leffler 		space -= sr->isr_len;
10748a1b9b6aSSam Leffler 	}
10758a1b9b6aSSam Leffler 	ireq->i_len -= space;
10768a1b9b6aSSam Leffler 	return error;
10778a1b9b6aSSam Leffler }
1078239cc3b6SSam Leffler #endif /* COMPAT_FREEBSD6 */
1079239cc3b6SSam Leffler 
1080239cc3b6SSam Leffler struct scanresultsreq {
1081239cc3b6SSam Leffler 	struct ieee80211req_scan_result *sr;
1082239cc3b6SSam Leffler 	size_t	space;
1083239cc3b6SSam Leffler };
1084239cc3b6SSam Leffler 
1085239cc3b6SSam Leffler static size_t
1086239cc3b6SSam Leffler scan_space(const struct ieee80211_node *ni, size_t *ielen)
1087239cc3b6SSam Leffler {
1088239cc3b6SSam Leffler 	size_t len;
1089239cc3b6SSam Leffler 
1090239cc3b6SSam Leffler 	*ielen = 0;
1091239cc3b6SSam Leffler 	if (ni->ni_wpa_ie != NULL)
1092239cc3b6SSam Leffler 		*ielen += 2+ni->ni_wpa_ie[1];
1093239cc3b6SSam Leffler 	if (ni->ni_wme_ie != NULL)
1094239cc3b6SSam Leffler 		*ielen += 2+ni->ni_wme_ie[1];
1095239cc3b6SSam Leffler 	/*
1096239cc3b6SSam Leffler 	 * NB: ie's can be no more than 255 bytes and the max 802.11
1097239cc3b6SSam Leffler 	 * packet is <3Kbytes so we are sure this doesn't overflow
1098239cc3b6SSam Leffler 	 * 16-bits; if this is a concern we can drop the ie's.
1099239cc3b6SSam Leffler 	 */
1100239cc3b6SSam Leffler 	len = sizeof(struct ieee80211req_scan_result) + ni->ni_esslen + *ielen;
1101239cc3b6SSam Leffler 	return roundup(len, sizeof(u_int32_t));
1102239cc3b6SSam Leffler }
1103239cc3b6SSam Leffler 
1104239cc3b6SSam Leffler static void
1105239cc3b6SSam Leffler get_scan_space(void *arg, struct ieee80211_node *ni)
1106239cc3b6SSam Leffler {
1107239cc3b6SSam Leffler 	struct scanresultsreq *req = arg;
1108239cc3b6SSam Leffler 	size_t ielen;
1109239cc3b6SSam Leffler 
1110239cc3b6SSam Leffler 	req->space += scan_space(ni, &ielen);
1111239cc3b6SSam Leffler }
1112239cc3b6SSam Leffler 
1113239cc3b6SSam Leffler static void
1114239cc3b6SSam Leffler get_scan_result(void *arg, struct ieee80211_node *ni)
1115239cc3b6SSam Leffler {
1116239cc3b6SSam Leffler 	struct scanresultsreq *req = arg;
1117239cc3b6SSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
1118239cc3b6SSam Leffler 	struct ieee80211req_scan_result *sr;
1119239cc3b6SSam Leffler 	size_t ielen, len;
1120239cc3b6SSam Leffler 	u_int8_t *cp;
1121239cc3b6SSam Leffler 
1122239cc3b6SSam Leffler 	len = scan_space(ni, &ielen);
1123239cc3b6SSam Leffler 	if (len > req->space)
1124239cc3b6SSam Leffler 		return;
1125239cc3b6SSam Leffler 	sr = req->sr;
1126239cc3b6SSam Leffler 	KASSERT(len <= 65535 && ielen <= 65535,
1127239cc3b6SSam Leffler 	    ("len %zu ssid %u ie %zu", len, ni->ni_esslen, ielen));
1128239cc3b6SSam Leffler 	sr->isr_len = len;
1129239cc3b6SSam Leffler 	sr->isr_ssid_len = ni->ni_esslen;
1130239cc3b6SSam Leffler 	sr->isr_ie_len = ielen;
1131239cc3b6SSam Leffler 	if (ni->ni_chan != IEEE80211_CHAN_ANYC) {
1132239cc3b6SSam Leffler 		sr->isr_freq = ni->ni_chan->ic_freq;
1133239cc3b6SSam Leffler 		sr->isr_flags = ni->ni_chan->ic_flags;
1134239cc3b6SSam Leffler 	}
1135239cc3b6SSam Leffler 	/* XXX need to rev driver apis for signal data */
1136239cc3b6SSam Leffler 	sr->isr_rssi = (int8_t) ic->ic_node_getrssi(ni);
1137239cc3b6SSam Leffler 	sr->isr_intval = ni->ni_intval;
1138239cc3b6SSam Leffler 	sr->isr_capinfo = ni->ni_capinfo;
1139239cc3b6SSam Leffler 	sr->isr_erp = ni->ni_erp;
1140239cc3b6SSam Leffler 	IEEE80211_ADDR_COPY(sr->isr_bssid, ni->ni_bssid);
1141239cc3b6SSam Leffler 	sr->isr_nrates = ni->ni_rates.rs_nrates;
1142239cc3b6SSam Leffler 	if (sr->isr_nrates > 15)
1143239cc3b6SSam Leffler 		sr->isr_nrates = 15;
1144239cc3b6SSam Leffler 	memcpy(sr->isr_rates, ni->ni_rates.rs_rates, sr->isr_nrates);
1145239cc3b6SSam Leffler 	cp = (u_int8_t *)(sr+1);
1146239cc3b6SSam Leffler 	memcpy(cp, ni->ni_essid, ni->ni_esslen);
1147239cc3b6SSam Leffler 	cp += ni->ni_esslen;
1148239cc3b6SSam Leffler 	if (sr->isr_ie_len) {
1149239cc3b6SSam Leffler 		if (ni->ni_wpa_ie != NULL) {
1150239cc3b6SSam Leffler 			memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
1151239cc3b6SSam Leffler 			cp += 2+ni->ni_wpa_ie[1];
1152239cc3b6SSam Leffler 		}
1153239cc3b6SSam Leffler 		if (ni->ni_wme_ie != NULL) {
1154239cc3b6SSam Leffler 			memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
1155239cc3b6SSam Leffler 			cp += 2+ni->ni_wme_ie[1];
1156239cc3b6SSam Leffler 		}
1157239cc3b6SSam Leffler 	}
1158239cc3b6SSam Leffler 
1159239cc3b6SSam Leffler 	req->sr = (struct ieee80211req_scan_result *)(((u_int8_t *)sr) + len);
1160239cc3b6SSam Leffler 	req->space -= len;
1161239cc3b6SSam Leffler }
1162239cc3b6SSam Leffler 
1163239cc3b6SSam Leffler static int
1164239cc3b6SSam Leffler ieee80211_ioctl_getscanresults(struct ieee80211com *ic, struct ieee80211req *ireq)
1165239cc3b6SSam Leffler {
1166239cc3b6SSam Leffler 	struct scanresultsreq req;
1167239cc3b6SSam Leffler 	int error;
1168239cc3b6SSam Leffler 
1169239cc3b6SSam Leffler 	if (ireq->i_len < sizeof(struct scanresultsreq))
1170239cc3b6SSam Leffler 		return EFAULT;
1171239cc3b6SSam Leffler 
1172239cc3b6SSam Leffler 	error = 0;
1173239cc3b6SSam Leffler 	req.space = 0;
1174239cc3b6SSam Leffler 	ieee80211_iterate_nodes(&ic->ic_scan, get_scan_space, &req);
1175239cc3b6SSam Leffler 	if (req.space > ireq->i_len)
1176239cc3b6SSam Leffler 		req.space = ireq->i_len;
1177239cc3b6SSam Leffler 	if (req.space > 0) {
1178239cc3b6SSam Leffler 		size_t space;
1179239cc3b6SSam Leffler 		void *p;
1180239cc3b6SSam Leffler 
1181239cc3b6SSam Leffler 		space = req.space;
1182239cc3b6SSam Leffler 		/* XXX M_WAITOK after driver lock released */
1183239cc3b6SSam Leffler 		MALLOC(p, void *, space, M_TEMP, M_NOWAIT | M_ZERO);
1184239cc3b6SSam Leffler 		if (p == NULL)
1185239cc3b6SSam Leffler 			return ENOMEM;
1186239cc3b6SSam Leffler 		req.sr = p;
1187239cc3b6SSam Leffler 		ieee80211_iterate_nodes(&ic->ic_scan, get_scan_result, &req);
1188239cc3b6SSam Leffler 		ireq->i_len = space - req.space;
1189239cc3b6SSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
1190239cc3b6SSam Leffler 		FREE(p, M_TEMP);
1191239cc3b6SSam Leffler 	} else
1192239cc3b6SSam Leffler 		ireq->i_len = 0;
1193239cc3b6SSam Leffler 
1194239cc3b6SSam Leffler 	return error;
1195239cc3b6SSam Leffler }
11968a1b9b6aSSam Leffler 
11972cab1d3dSSam Leffler struct stainforeq {
11982cab1d3dSSam Leffler 	struct ieee80211com *ic;
11992cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
12002cab1d3dSSam Leffler 	size_t	space;
12012cab1d3dSSam Leffler };
12028a1b9b6aSSam Leffler 
12032cab1d3dSSam Leffler static size_t
12042cab1d3dSSam Leffler sta_space(const struct ieee80211_node *ni, size_t *ielen)
12052cab1d3dSSam Leffler {
12062cab1d3dSSam Leffler 	*ielen = 0;
12078a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL)
12082cab1d3dSSam Leffler 		*ielen += 2+ni->ni_wpa_ie[1];
12098a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL)
12102cab1d3dSSam Leffler 		*ielen += 2+ni->ni_wme_ie[1];
12112cab1d3dSSam Leffler 	return roundup(sizeof(struct ieee80211req_sta_info) + *ielen,
12122cab1d3dSSam Leffler 		      sizeof(u_int32_t));
12132cab1d3dSSam Leffler }
12142cab1d3dSSam Leffler 
12152cab1d3dSSam Leffler static void
12162cab1d3dSSam Leffler get_sta_space(void *arg, struct ieee80211_node *ni)
12172cab1d3dSSam Leffler {
12182cab1d3dSSam Leffler 	struct stainforeq *req = arg;
12192cab1d3dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
12202cab1d3dSSam Leffler 	size_t ielen;
12212cab1d3dSSam Leffler 
12222cab1d3dSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
12232cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
12242cab1d3dSSam Leffler 		return;
12252cab1d3dSSam Leffler 	req->space += sta_space(ni, &ielen);
12262cab1d3dSSam Leffler }
12272cab1d3dSSam Leffler 
12282cab1d3dSSam Leffler static void
12292cab1d3dSSam Leffler get_sta_info(void *arg, struct ieee80211_node *ni)
12302cab1d3dSSam Leffler {
12312cab1d3dSSam Leffler 	struct stainforeq *req = arg;
12322cab1d3dSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
12332cab1d3dSSam Leffler 	struct ieee80211req_sta_info *si;
12342cab1d3dSSam Leffler 	size_t ielen, len;
12352cab1d3dSSam Leffler 	u_int8_t *cp;
12362cab1d3dSSam Leffler 
12372cab1d3dSSam Leffler 	if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
12382cab1d3dSSam Leffler 	    ni->ni_associd == 0)	/* only associated stations */
12392cab1d3dSSam Leffler 		return;
12402cab1d3dSSam Leffler 	if (ni->ni_chan == IEEE80211_CHAN_ANYC)	/* XXX bogus entry */
12412cab1d3dSSam Leffler 		return;
12422cab1d3dSSam Leffler 	len = sta_space(ni, &ielen);
12432cab1d3dSSam Leffler 	if (len > req->space)
12442cab1d3dSSam Leffler 		return;
12452cab1d3dSSam Leffler 	si = req->si;
1246239cc3b6SSam Leffler 	KASSERT(len <= 65535 && ielen <= 65535, ("len %zu ie %zu", len, ielen));
12472cab1d3dSSam Leffler 	si->isi_len = len;
12482cab1d3dSSam Leffler 	si->isi_ie_len = ielen;
12498a1b9b6aSSam Leffler 	si->isi_freq = ni->ni_chan->ic_freq;
12508a1b9b6aSSam Leffler 	si->isi_flags = ni->ni_chan->ic_flags;
12518a1b9b6aSSam Leffler 	si->isi_state = ni->ni_flags;
12528a1b9b6aSSam Leffler 	si->isi_authmode = ni->ni_authmode;
12538a1b9b6aSSam Leffler 	si->isi_rssi = ic->ic_node_getrssi(ni);
12548a1b9b6aSSam Leffler 	si->isi_capinfo = ni->ni_capinfo;
12558a1b9b6aSSam Leffler 	si->isi_erp = ni->ni_erp;
12568a1b9b6aSSam Leffler 	IEEE80211_ADDR_COPY(si->isi_macaddr, ni->ni_macaddr);
12578a1b9b6aSSam Leffler 	si->isi_nrates = ni->ni_rates.rs_nrates;
12588a1b9b6aSSam Leffler 	if (si->isi_nrates > 15)
12598a1b9b6aSSam Leffler 		si->isi_nrates = 15;
12608a1b9b6aSSam Leffler 	memcpy(si->isi_rates, ni->ni_rates.rs_rates, si->isi_nrates);
12618a1b9b6aSSam Leffler 	si->isi_txrate = ni->ni_txrate;
12628a1b9b6aSSam Leffler 	si->isi_associd = ni->ni_associd;
12638a1b9b6aSSam Leffler 	si->isi_txpower = ni->ni_txpower;
12648a1b9b6aSSam Leffler 	si->isi_vlan = ni->ni_vlan;
12658a1b9b6aSSam Leffler 	if (ni->ni_flags & IEEE80211_NODE_QOS) {
12668a1b9b6aSSam Leffler 		memcpy(si->isi_txseqs, ni->ni_txseqs, sizeof(ni->ni_txseqs));
12678a1b9b6aSSam Leffler 		memcpy(si->isi_rxseqs, ni->ni_rxseqs, sizeof(ni->ni_rxseqs));
12688a1b9b6aSSam Leffler 	} else {
12698a1b9b6aSSam Leffler 		si->isi_txseqs[0] = ni->ni_txseqs[0];
12708a1b9b6aSSam Leffler 		si->isi_rxseqs[0] = ni->ni_rxseqs[0];
12718a1b9b6aSSam Leffler 	}
12722cab1d3dSSam Leffler 	/* NB: leave all cases in case we relax ni_associd == 0 check */
12732cab1d3dSSam Leffler 	if (ieee80211_node_is_authorized(ni))
12748a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_run;
12752cab1d3dSSam Leffler 	else if (ni->ni_associd != 0)
12768a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_auth;
12778a1b9b6aSSam Leffler 	else
12788a1b9b6aSSam Leffler 		si->isi_inact = ic->ic_inact_init;
12798a1b9b6aSSam Leffler 	si->isi_inact = (si->isi_inact - ni->ni_inact) * IEEE80211_INACT_WAIT;
12808a1b9b6aSSam Leffler 
12818a1b9b6aSSam Leffler 	cp = (u_int8_t *)(si+1);
12828a1b9b6aSSam Leffler 	if (ni->ni_wpa_ie != NULL) {
12838a1b9b6aSSam Leffler 		memcpy(cp, ni->ni_wpa_ie, 2+ni->ni_wpa_ie[1]);
12848a1b9b6aSSam Leffler 		cp += 2+ni->ni_wpa_ie[1];
12858a1b9b6aSSam Leffler 	}
12868a1b9b6aSSam Leffler 	if (ni->ni_wme_ie != NULL) {
12878a1b9b6aSSam Leffler 		memcpy(cp, ni->ni_wme_ie, 2+ni->ni_wme_ie[1]);
12888a1b9b6aSSam Leffler 		cp += 2+ni->ni_wme_ie[1];
12898a1b9b6aSSam Leffler 	}
12902cab1d3dSSam Leffler 
12912cab1d3dSSam Leffler 	req->si = (struct ieee80211req_sta_info *)(((u_int8_t *)si) + len);
12922cab1d3dSSam Leffler 	req->space -= len;
12938a1b9b6aSSam Leffler }
12942cab1d3dSSam Leffler 
12952cab1d3dSSam Leffler static int
12962cab1d3dSSam Leffler ieee80211_ioctl_getstainfo(struct ieee80211com *ic, struct ieee80211req *ireq)
12972cab1d3dSSam Leffler {
12982cab1d3dSSam Leffler 	struct stainforeq req;
12992cab1d3dSSam Leffler 	int error;
13002cab1d3dSSam Leffler 
13012cab1d3dSSam Leffler 	if (ireq->i_len < sizeof(struct stainforeq))
13022cab1d3dSSam Leffler 		return EFAULT;
13032cab1d3dSSam Leffler 
13042cab1d3dSSam Leffler 	error = 0;
13052cab1d3dSSam Leffler 	req.space = 0;
13062cab1d3dSSam Leffler 	ieee80211_iterate_nodes(&ic->ic_sta, get_sta_space, &req);
13072cab1d3dSSam Leffler 	if (req.space > ireq->i_len)
13082cab1d3dSSam Leffler 		req.space = ireq->i_len;
13092cab1d3dSSam Leffler 	if (req.space > 0) {
13102cab1d3dSSam Leffler 		size_t space;
13112cab1d3dSSam Leffler 		void *p;
13122cab1d3dSSam Leffler 
13132cab1d3dSSam Leffler 		space = req.space;
13142cab1d3dSSam Leffler 		/* XXX M_WAITOK after driver lock released */
13152cab1d3dSSam Leffler 		MALLOC(p, void *, space, M_TEMP, M_NOWAIT);
13162cab1d3dSSam Leffler 		if (p == NULL)
13172cab1d3dSSam Leffler 			return ENOMEM;
13182cab1d3dSSam Leffler 		req.si = p;
13192cab1d3dSSam Leffler 		ieee80211_iterate_nodes(&ic->ic_sta, get_sta_info, &req);
13202cab1d3dSSam Leffler 		ireq->i_len = space - req.space;
13212cab1d3dSSam Leffler 		error = copyout(p, ireq->i_data, ireq->i_len);
13222cab1d3dSSam Leffler 		FREE(p, M_TEMP);
13232cab1d3dSSam Leffler 	} else
13242cab1d3dSSam Leffler 		ireq->i_len = 0;
13252cab1d3dSSam Leffler 
13268a1b9b6aSSam Leffler 	return error;
13278a1b9b6aSSam Leffler }
13288a1b9b6aSSam Leffler 
13298a1b9b6aSSam Leffler static int
13308a1b9b6aSSam Leffler ieee80211_ioctl_getstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
13318a1b9b6aSSam Leffler {
13328a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
13338a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
13348a1b9b6aSSam Leffler 	int error;
13358a1b9b6aSSam Leffler 
13368a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
13378a1b9b6aSSam Leffler 		return EINVAL;
13388a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
13398a1b9b6aSSam Leffler 	if (error != 0)
13408a1b9b6aSSam Leffler 		return error;
1341acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
13428a1b9b6aSSam Leffler 	if (ni == NULL)
13438a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
13448a1b9b6aSSam Leffler 	txpow.it_txpow = ni->ni_txpower;
13458a1b9b6aSSam Leffler 	error = copyout(&txpow, ireq->i_data, sizeof(txpow));
13468a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
13478a1b9b6aSSam Leffler 	return error;
13488a1b9b6aSSam Leffler }
13498a1b9b6aSSam Leffler 
13508a1b9b6aSSam Leffler static int
13518a1b9b6aSSam Leffler ieee80211_ioctl_getwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
13528a1b9b6aSSam Leffler {
13538a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
13548a1b9b6aSSam Leffler 	struct wmeParams *wmep;
13558a1b9b6aSSam Leffler 	int ac;
13568a1b9b6aSSam Leffler 
13578a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
13588a1b9b6aSSam Leffler 		return EINVAL;
13598a1b9b6aSSam Leffler 
13608a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
13618a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
13628a1b9b6aSSam Leffler 		ac = WME_AC_BE;
13638a1b9b6aSSam Leffler 	if (ireq->i_len & IEEE80211_WMEPARAM_BSS)
13648a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
13658a1b9b6aSSam Leffler 	else
13668a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
13678a1b9b6aSSam Leffler 	switch (ireq->i_type) {
13688a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
13698a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmin;
13708a1b9b6aSSam Leffler 		break;
13718a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
13728a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_logcwmax;
13738a1b9b6aSSam Leffler 		break;
13748a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
13758a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_aifsn;
13768a1b9b6aSSam Leffler 		break;
13778a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
13788a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_txopLimit;
13798a1b9b6aSSam Leffler 		break;
13808a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
13818a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
13828a1b9b6aSSam Leffler 		ireq->i_val = wmep->wmep_acm;
13838a1b9b6aSSam Leffler 		break;
13848a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
13858a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
13868a1b9b6aSSam Leffler 		ireq->i_val = !wmep->wmep_noackPolicy;
13878a1b9b6aSSam Leffler 		break;
13888a1b9b6aSSam Leffler 	}
13898a1b9b6aSSam Leffler 	return 0;
13908a1b9b6aSSam Leffler }
13918a1b9b6aSSam Leffler 
1392188757f5SSam Leffler static int
1393188757f5SSam Leffler ieee80211_ioctl_getmaccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
1394188757f5SSam Leffler {
1395188757f5SSam Leffler 	const struct ieee80211_aclator *acl = ic->ic_acl;
1396188757f5SSam Leffler 
1397188757f5SSam Leffler 	return (acl == NULL ? EINVAL : acl->iac_getioctl(ic, ireq));
1398188757f5SSam Leffler }
1399188757f5SSam Leffler 
1400c788ca3eSBill Paul /*
1401c788ca3eSBill Paul  * When building the kernel with -O2 on the i386 architecture, gcc
1402c788ca3eSBill Paul  * seems to want to inline this function into ieee80211_ioctl()
1403c788ca3eSBill Paul  * (which is the only routine that calls it). When this happens,
1404c788ca3eSBill Paul  * ieee80211_ioctl() ends up consuming an additional 2K of stack
1405c788ca3eSBill Paul  * space. (Exactly why it needs so much is unclear.) The problem
1406c788ca3eSBill Paul  * is that it's possible for ieee80211_ioctl() to invoke other
1407c788ca3eSBill Paul  * routines (including driver init functions) which could then find
1408c788ca3eSBill Paul  * themselves perilously close to exhausting the stack.
1409c788ca3eSBill Paul  *
1410c788ca3eSBill Paul  * To avoid this, we deliberately prevent gcc from inlining this
1411c788ca3eSBill Paul  * routine. Another way to avoid this is to use less agressive
1412c788ca3eSBill Paul  * optimization when compiling this file (i.e. -O instead of -O2)
1413c788ca3eSBill Paul  * but special-casing the compilation of this one module in the
1414c788ca3eSBill Paul  * build system would be awkward.
1415c788ca3eSBill Paul  */
1416c788ca3eSBill Paul #ifdef __GNUC__
1417c788ca3eSBill Paul __attribute__ ((noinline))
1418c788ca3eSBill Paul #endif
14198a1b9b6aSSam Leffler static int
14208a1b9b6aSSam Leffler ieee80211_ioctl_get80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
14218a1b9b6aSSam Leffler {
14228a1b9b6aSSam Leffler 	const struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
14231a1e1d21SSam Leffler 	int error = 0;
14248a1b9b6aSSam Leffler 	u_int kid, len, m;
14251a1e1d21SSam Leffler 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
14261a1e1d21SSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
14271a1e1d21SSam Leffler 
14281a1e1d21SSam Leffler 	switch (ireq->i_type) {
14291a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
14301a1e1d21SSam Leffler 		switch (ic->ic_state) {
14311a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
14321a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
14331a1e1d21SSam Leffler 			ireq->i_len = ic->ic_des_esslen;
14341a1e1d21SSam Leffler 			memcpy(tmpssid, ic->ic_des_essid, ireq->i_len);
14351a1e1d21SSam Leffler 			break;
14361a1e1d21SSam Leffler 		default:
14371a1e1d21SSam Leffler 			ireq->i_len = ic->ic_bss->ni_esslen;
14381a1e1d21SSam Leffler 			memcpy(tmpssid, ic->ic_bss->ni_essid,
14391a1e1d21SSam Leffler 				ireq->i_len);
14401a1e1d21SSam Leffler 			break;
14411a1e1d21SSam Leffler 		}
14421a1e1d21SSam Leffler 		error = copyout(tmpssid, ireq->i_data, ireq->i_len);
14431a1e1d21SSam Leffler 		break;
14441a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMSSIDS:
14451a1e1d21SSam Leffler 		ireq->i_val = 1;
14461a1e1d21SSam Leffler 		break;
14471a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
14488a1b9b6aSSam Leffler 		if ((ic->ic_flags & IEEE80211_F_PRIVACY) == 0)
14498a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_OFF;
14508a1b9b6aSSam Leffler 		else if (ic->ic_flags & IEEE80211_F_DROPUNENC)
14518a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_ON;
14528a1b9b6aSSam Leffler 		else
14538a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_WEP_MIXED;
14541a1e1d21SSam Leffler 		break;
14551a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
14561a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
14578a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
14588a1b9b6aSSam Leffler 			return EINVAL;
14598a1b9b6aSSam Leffler 		len = (u_int) ic->ic_nw_keys[kid].wk_keylen;
14601a1e1d21SSam Leffler 		/* NB: only root can read WEP keys */
14615c8bb90bSBrian Feldman 		if (suser(curthread) == 0) {
14621a1e1d21SSam Leffler 			bcopy(ic->ic_nw_keys[kid].wk_key, tmpkey, len);
14631a1e1d21SSam Leffler 		} else {
14641a1e1d21SSam Leffler 			bzero(tmpkey, len);
14651a1e1d21SSam Leffler 		}
14661a1e1d21SSam Leffler 		ireq->i_len = len;
14671a1e1d21SSam Leffler 		error = copyout(tmpkey, ireq->i_data, len);
14681a1e1d21SSam Leffler 		break;
14691a1e1d21SSam Leffler 	case IEEE80211_IOC_NUMWEPKEYS:
14701a1e1d21SSam Leffler 		ireq->i_val = IEEE80211_WEP_NKID;
14711a1e1d21SSam Leffler 		break;
14721a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
14738a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_def_txkey;
14741a1e1d21SSam Leffler 		break;
14751a1e1d21SSam Leffler 	case IEEE80211_IOC_AUTHMODE:
14768a1b9b6aSSam Leffler 		if (ic->ic_flags & IEEE80211_F_WPA)
14778a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_WPA;
14788a1b9b6aSSam Leffler 		else
14798a1b9b6aSSam Leffler 			ireq->i_val = ic->ic_bss->ni_authmode;
14801a1e1d21SSam Leffler 		break;
14811a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
1482b5c99415SSam Leffler 		ireq->i_val = ieee80211_chan2ieee(ic, ic->ic_curchan);
14831a1e1d21SSam Leffler 		break;
14841a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
14851a1e1d21SSam Leffler 		if (ic->ic_flags & IEEE80211_F_PMGTON)
14861a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_ON;
14871a1e1d21SSam Leffler 		else
14881a1e1d21SSam Leffler 			ireq->i_val = IEEE80211_POWERSAVE_OFF;
14891a1e1d21SSam Leffler 		break;
14901a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
14911a1e1d21SSam Leffler 		ireq->i_val = ic->ic_lintval;
14921a1e1d21SSam Leffler 		break;
149313604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
14941a1e1d21SSam Leffler 		ireq->i_val = ic->ic_rtsthreshold;
14951a1e1d21SSam Leffler 		break;
14962e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
14972e79ca97SSam Leffler 		ireq->i_val = ic->ic_protmode;
14982e79ca97SSam Leffler 		break;
14992e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
15002e79ca97SSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
15018a1b9b6aSSam Leffler 			return EINVAL;
15028a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_txpowlimit;
15038a1b9b6aSSam Leffler 		break;
15048a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTCIPHER:
15058a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_mcastcipher;
15068a1b9b6aSSam Leffler 		break;
15078a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTKEYLEN:
15088a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_mcastkeylen;
15098a1b9b6aSSam Leffler 		break;
15108a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHERS:
15118a1b9b6aSSam Leffler 		ireq->i_val = 0;
15128a1b9b6aSSam Leffler 		for (m = 0x1; m != 0; m <<= 1)
15138a1b9b6aSSam Leffler 			if (rsn->rsn_ucastcipherset & m)
15148a1b9b6aSSam Leffler 				ireq->i_val |= 1<<cap2cipher(m);
15158a1b9b6aSSam Leffler 		break;
15168a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHER:
15178a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_ucastcipher;
15188a1b9b6aSSam Leffler 		break;
15198a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTKEYLEN:
15208a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_ucastkeylen;
15218a1b9b6aSSam Leffler 		break;
15228a1b9b6aSSam Leffler 	case IEEE80211_IOC_KEYMGTALGS:
15238a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_keymgmtset;
15248a1b9b6aSSam Leffler 		break;
15258a1b9b6aSSam Leffler 	case IEEE80211_IOC_RSNCAPS:
15268a1b9b6aSSam Leffler 		ireq->i_val = rsn->rsn_caps;
15278a1b9b6aSSam Leffler 		break;
15288a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
15298a1b9b6aSSam Leffler 		switch (ic->ic_flags & IEEE80211_F_WPA) {
15308a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1:
15318a1b9b6aSSam Leffler 			ireq->i_val = 1;
15328a1b9b6aSSam Leffler 			break;
15338a1b9b6aSSam Leffler 		case IEEE80211_F_WPA2:
15348a1b9b6aSSam Leffler 			ireq->i_val = 2;
15358a1b9b6aSSam Leffler 			break;
15368a1b9b6aSSam Leffler 		case IEEE80211_F_WPA1 | IEEE80211_F_WPA2:
15378a1b9b6aSSam Leffler 			ireq->i_val = 3;
15388a1b9b6aSSam Leffler 			break;
15398a1b9b6aSSam Leffler 		default:
15408a1b9b6aSSam Leffler 			ireq->i_val = 0;
15418a1b9b6aSSam Leffler 			break;
15428a1b9b6aSSam Leffler 		}
15438a1b9b6aSSam Leffler 		break;
15448a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
15458a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getchanlist(ic, ireq);
15468a1b9b6aSSam Leffler 		break;
15478a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
15488a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_roaming;
15498a1b9b6aSSam Leffler 		break;
15508a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
15518a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PRIVACY) != 0;
15528a1b9b6aSSam Leffler 		break;
15538a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
15548a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_DROPUNENC) != 0;
15558a1b9b6aSSam Leffler 		break;
15568a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
15578a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_COUNTERM) != 0;
15588a1b9b6aSSam Leffler 		break;
15598a1b9b6aSSam Leffler 	case IEEE80211_IOC_DRIVER_CAPS:
15608a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_caps>>16;
15618a1b9b6aSSam Leffler 		ireq->i_len = ic->ic_caps&0xffff;
15628a1b9b6aSSam Leffler 		break;
15638a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
15648a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_WME) != 0;
15658a1b9b6aSSam Leffler 		break;
15668a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
15678a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_HIDESSID) != 0;
15688a1b9b6aSSam Leffler 		break;
15698a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
15708a1b9b6aSSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_NOBRIDGE) == 0;
15718a1b9b6aSSam Leffler 		break;
15728a1b9b6aSSam Leffler 	case IEEE80211_IOC_OPTIE:
15738a1b9b6aSSam Leffler 		if (ic->ic_opt_ie == NULL)
15748a1b9b6aSSam Leffler 			return EINVAL;
15758a1b9b6aSSam Leffler 		/* NB: truncate, caller can check length */
15768a1b9b6aSSam Leffler 		if (ireq->i_len > ic->ic_opt_ie_len)
15778a1b9b6aSSam Leffler 			ireq->i_len = ic->ic_opt_ie_len;
15788a1b9b6aSSam Leffler 		error = copyout(ic->ic_opt_ie, ireq->i_data, ireq->i_len);
15798a1b9b6aSSam Leffler 		break;
15808a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
15818a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getkey(ic, ireq);
15828a1b9b6aSSam Leffler 		break;
15838a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANINFO:
15848a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getchaninfo(ic, ireq);
15858a1b9b6aSSam Leffler 		break;
15868a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
15878a1b9b6aSSam Leffler 		if (ireq->i_len != IEEE80211_ADDR_LEN)
15888a1b9b6aSSam Leffler 			return EINVAL;
15898a1b9b6aSSam Leffler 		error = copyout(ic->ic_state == IEEE80211_S_RUN ?
15908a1b9b6aSSam Leffler 					ic->ic_bss->ni_bssid :
15918a1b9b6aSSam Leffler 					ic->ic_des_bssid,
15928a1b9b6aSSam Leffler 				ireq->i_data, ireq->i_len);
15938a1b9b6aSSam Leffler 		break;
15948a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAIE:
15958a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getwpaie(ic, ireq);
15968a1b9b6aSSam Leffler 		break;
1597239cc3b6SSam Leffler #ifdef COMPAT_FREEBSD6
1598239cc3b6SSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS_OLD:
1599239cc3b6SSam Leffler 		error = old_getscanresults(ic, ireq);
1600239cc3b6SSam Leffler 		break;
1601239cc3b6SSam Leffler #endif
16028a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_RESULTS:
16038a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getscanresults(ic, ireq);
16048a1b9b6aSSam Leffler 		break;
16058a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_STATS:
16068a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstastats(ic, ireq);
16078a1b9b6aSSam Leffler 		break;
16088a1b9b6aSSam Leffler 	case IEEE80211_IOC_TXPOWMAX:
16098a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_bss->ni_txpower;
16108a1b9b6aSSam Leffler 		break;
16118a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
16128a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstatxpow(ic, ireq);
16138a1b9b6aSSam Leffler 		break;
16148a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_INFO:
16158a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getstainfo(ic, ireq);
16168a1b9b6aSSam Leffler 		break;
16178a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
16188a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
16198a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
16208a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
16218a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
16228a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
16238a1b9b6aSSam Leffler 		error = ieee80211_ioctl_getwmeparam(ic, ireq);
16248a1b9b6aSSam Leffler 		break;
16258a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
16268a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_dtim_period;
16278a1b9b6aSSam Leffler 		break;
16288a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
16298a1b9b6aSSam Leffler 		/* NB: get from ic_bss for station mode */
16308a1b9b6aSSam Leffler 		ireq->i_val = ic->ic_bss->ni_intval;
16312e79ca97SSam Leffler 		break;
1632c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
1633c4f040c3SSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_PUREG) != 0;
1634c4f040c3SSam Leffler 		break;
163564353cb0SSam Leffler 	case IEEE80211_IOC_MCAST_RATE:
163664353cb0SSam Leffler 		ireq->i_val = ic->ic_mcast_rate;
163764353cb0SSam Leffler 		break;
163870231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
163970231e3dSSam Leffler 		ireq->i_val = ic->ic_fragthreshold;
164070231e3dSSam Leffler 		break;
1641188757f5SSam Leffler 	case IEEE80211_IOC_MACCMD:
1642188757f5SSam Leffler 		error = ieee80211_ioctl_getmaccmd(ic, ireq);
1643188757f5SSam Leffler 		break;
1644c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
1645c27e4e31SSam Leffler 		ireq->i_val = (ic->ic_flags & IEEE80211_F_BURST) != 0;
1646c27e4e31SSam Leffler 		break;
16471a1e1d21SSam Leffler 	default:
16481a1e1d21SSam Leffler 		error = EINVAL;
1649b2e95691SSam Leffler 		break;
16501a1e1d21SSam Leffler 	}
16518a1b9b6aSSam Leffler 	return error;
16528a1b9b6aSSam Leffler }
16538a1b9b6aSSam Leffler 
16548a1b9b6aSSam Leffler static int
16558a1b9b6aSSam Leffler ieee80211_ioctl_setoptie(struct ieee80211com *ic, struct ieee80211req *ireq)
16568a1b9b6aSSam Leffler {
16578a1b9b6aSSam Leffler 	int error;
1658d3b3a464SSam Leffler 	void *ie, *oie;
16598a1b9b6aSSam Leffler 
16608a1b9b6aSSam Leffler 	/*
16618a1b9b6aSSam Leffler 	 * NB: Doing this for ap operation could be useful (e.g. for
16628a1b9b6aSSam Leffler 	 *     WPA and/or WME) except that it typically is worthless
16638a1b9b6aSSam Leffler 	 *     without being able to intervene when processing
16648a1b9b6aSSam Leffler 	 *     association response frames--so disallow it for now.
16658a1b9b6aSSam Leffler 	 */
16668a1b9b6aSSam Leffler 	if (ic->ic_opmode != IEEE80211_M_STA)
16678a1b9b6aSSam Leffler 		return EINVAL;
16688a1b9b6aSSam Leffler 	if (ireq->i_len > IEEE80211_MAX_OPT_IE)
16698a1b9b6aSSam Leffler 		return EINVAL;
1670d3b3a464SSam Leffler 	if (ireq->i_len > 0) {
1671c2544286SSam Leffler 		MALLOC(ie, void *, ireq->i_len, M_DEVBUF, M_NOWAIT);
16728a1b9b6aSSam Leffler 		if (ie == NULL)
16738a1b9b6aSSam Leffler 			return ENOMEM;
16748a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, ie, ireq->i_len);
1675d3b3a464SSam Leffler 		if (error) {
1676d3b3a464SSam Leffler 			FREE(ie, M_DEVBUF);
1677d3b3a464SSam Leffler 			return error;
1678d3b3a464SSam Leffler 		}
1679d3b3a464SSam Leffler 	} else {
1680d3b3a464SSam Leffler 		ie = NULL;
1681d3b3a464SSam Leffler 		ireq->i_len = 0;
1682d3b3a464SSam Leffler 	}
16838a1b9b6aSSam Leffler 	/* XXX sanity check data? */
1684d3b3a464SSam Leffler 	oie = ic->ic_opt_ie;
16858a1b9b6aSSam Leffler 	ic->ic_opt_ie = ie;
16868a1b9b6aSSam Leffler 	ic->ic_opt_ie_len = ireq->i_len;
1687d3b3a464SSam Leffler 	if (oie != NULL)
1688d3b3a464SSam Leffler 		FREE(oie, M_DEVBUF);
16898a1b9b6aSSam Leffler 	return 0;
16908a1b9b6aSSam Leffler }
16918a1b9b6aSSam Leffler 
16928a1b9b6aSSam Leffler static int
16938a1b9b6aSSam Leffler ieee80211_ioctl_setkey(struct ieee80211com *ic, struct ieee80211req *ireq)
16948a1b9b6aSSam Leffler {
16958a1b9b6aSSam Leffler 	struct ieee80211req_key ik;
16968a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
16978a1b9b6aSSam Leffler 	struct ieee80211_key *wk;
16988a1b9b6aSSam Leffler 	u_int16_t kid;
16998a1b9b6aSSam Leffler 	int error;
17008a1b9b6aSSam Leffler 
17018a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(ik))
17028a1b9b6aSSam Leffler 		return EINVAL;
17038a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &ik, sizeof(ik));
17041a1e1d21SSam Leffler 	if (error)
17058a1b9b6aSSam Leffler 		return error;
17068a1b9b6aSSam Leffler 	/* NB: cipher support is verified by ieee80211_crypt_newkey */
17078a1b9b6aSSam Leffler 	/* NB: this also checks ik->ik_keylen > sizeof(wk->wk_key) */
17088a1b9b6aSSam Leffler 	if (ik.ik_keylen > sizeof(ik.ik_keydata))
17098a1b9b6aSSam Leffler 		return E2BIG;
17108a1b9b6aSSam Leffler 	kid = ik.ik_keyix;
17118a1b9b6aSSam Leffler 	if (kid == IEEE80211_KEYIX_NONE) {
17128a1b9b6aSSam Leffler 		/* XXX unicast keys currently must be tx/rx */
17138a1b9b6aSSam Leffler 		if (ik.ik_flags != (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
17148a1b9b6aSSam Leffler 			return EINVAL;
17158a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1716b5d4660fSSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1717b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(ik.ik_macaddr, ni->ni_bssid)) {
1718b5d4660fSSam Leffler 				ieee80211_free_node(ni);
17198a1b9b6aSSam Leffler 				return EADDRNOTAVAIL;
1720b5d4660fSSam Leffler 			}
17218a1b9b6aSSam Leffler 		} else {
1722acc4f7f5SSam Leffler 			ni = ieee80211_find_node(&ic->ic_sta, ik.ik_macaddr);
17238a1b9b6aSSam Leffler 			if (ni == NULL)
17248a1b9b6aSSam Leffler 				return ENOENT;
17258a1b9b6aSSam Leffler 		}
17268a1b9b6aSSam Leffler 		wk = &ni->ni_ucastkey;
17278a1b9b6aSSam Leffler 	} else {
17288a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
17298a1b9b6aSSam Leffler 			return EINVAL;
17308a1b9b6aSSam Leffler 		wk = &ic->ic_nw_keys[kid];
1731386d84f6SSam Leffler 		/*
1732386d84f6SSam Leffler 		 * Global slots start off w/o any assigned key index.
1733386d84f6SSam Leffler 		 * Force one here for consistency with IEEE80211_IOC_WEPKEY.
1734386d84f6SSam Leffler 		 */
1735386d84f6SSam Leffler 		if (wk->wk_keyix == IEEE80211_KEYIX_NONE)
1736386d84f6SSam Leffler 			wk->wk_keyix = kid;
17378a1b9b6aSSam Leffler 		ni = NULL;
17388a1b9b6aSSam Leffler 	}
17398a1b9b6aSSam Leffler 	error = 0;
17408a1b9b6aSSam Leffler 	ieee80211_key_update_begin(ic);
1741dd70e17bSSam Leffler 	if (ieee80211_crypto_newkey(ic, ik.ik_type, ik.ik_flags, wk)) {
17428a1b9b6aSSam Leffler 		wk->wk_keylen = ik.ik_keylen;
17438a1b9b6aSSam Leffler 		/* NB: MIC presence is implied by cipher type */
17448a1b9b6aSSam Leffler 		if (wk->wk_keylen > IEEE80211_KEYBUF_SIZE)
17458a1b9b6aSSam Leffler 			wk->wk_keylen = IEEE80211_KEYBUF_SIZE;
17468a1b9b6aSSam Leffler 		wk->wk_keyrsc = ik.ik_keyrsc;
17478a1b9b6aSSam Leffler 		wk->wk_keytsc = 0;			/* new key, reset */
17488a1b9b6aSSam Leffler 		memset(wk->wk_key, 0, sizeof(wk->wk_key));
17498a1b9b6aSSam Leffler 		memcpy(wk->wk_key, ik.ik_keydata, ik.ik_keylen);
17508a1b9b6aSSam Leffler 		if (!ieee80211_crypto_setkey(ic, wk,
17518a1b9b6aSSam Leffler 		    ni != NULL ? ni->ni_macaddr : ik.ik_macaddr))
17528a1b9b6aSSam Leffler 			error = EIO;
17538a1b9b6aSSam Leffler 		else if ((ik.ik_flags & IEEE80211_KEY_DEFAULT))
17548a1b9b6aSSam Leffler 			ic->ic_def_txkey = kid;
17558a1b9b6aSSam Leffler 	} else
17568a1b9b6aSSam Leffler 		error = ENXIO;
17578a1b9b6aSSam Leffler 	ieee80211_key_update_end(ic);
17588a1b9b6aSSam Leffler 	if (ni != NULL)
17598a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
17608a1b9b6aSSam Leffler 	return error;
17618a1b9b6aSSam Leffler }
17628a1b9b6aSSam Leffler 
17638a1b9b6aSSam Leffler static int
17648a1b9b6aSSam Leffler ieee80211_ioctl_delkey(struct ieee80211com *ic, struct ieee80211req *ireq)
17658a1b9b6aSSam Leffler {
17668a1b9b6aSSam Leffler 	struct ieee80211req_del_key dk;
17678a1b9b6aSSam Leffler 	int kid, error;
17688a1b9b6aSSam Leffler 
17698a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(dk))
17708a1b9b6aSSam Leffler 		return EINVAL;
17718a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &dk, sizeof(dk));
17728a1b9b6aSSam Leffler 	if (error)
17738a1b9b6aSSam Leffler 		return error;
17748a1b9b6aSSam Leffler 	kid = dk.idk_keyix;
17758a1b9b6aSSam Leffler 	/* XXX u_int8_t -> u_int16_t */
17768a1b9b6aSSam Leffler 	if (dk.idk_keyix == (u_int8_t) IEEE80211_KEYIX_NONE) {
17778a1b9b6aSSam Leffler 		struct ieee80211_node *ni;
17788a1b9b6aSSam Leffler 
1779b5d4660fSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_STA) {
1780b5d4660fSSam Leffler 			ni = ieee80211_ref_node(ic->ic_bss);
1781b5d4660fSSam Leffler 			if (!IEEE80211_ADDR_EQ(dk.idk_macaddr, ni->ni_bssid)) {
1782b5d4660fSSam Leffler 				ieee80211_free_node(ni);
1783b5d4660fSSam Leffler 				return EADDRNOTAVAIL;
1784b5d4660fSSam Leffler 			}
1785b5d4660fSSam Leffler 		} else {
1786acc4f7f5SSam Leffler 			ni = ieee80211_find_node(&ic->ic_sta, dk.idk_macaddr);
17878a1b9b6aSSam Leffler 			if (ni == NULL)
1788b5d4660fSSam Leffler 				return ENOENT;
1789b5d4660fSSam Leffler 		}
17908a1b9b6aSSam Leffler 		/* XXX error return */
1791c1225b52SSam Leffler 		ieee80211_node_delucastkey(ni);
17928a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
17938a1b9b6aSSam Leffler 	} else {
17948a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
17958a1b9b6aSSam Leffler 			return EINVAL;
17968a1b9b6aSSam Leffler 		/* XXX error return */
17978a1b9b6aSSam Leffler 		ieee80211_crypto_delkey(ic, &ic->ic_nw_keys[kid]);
17988a1b9b6aSSam Leffler 	}
17998a1b9b6aSSam Leffler 	return 0;
18008a1b9b6aSSam Leffler }
18018a1b9b6aSSam Leffler 
18028a1b9b6aSSam Leffler static void
18038a1b9b6aSSam Leffler domlme(void *arg, struct ieee80211_node *ni)
18048a1b9b6aSSam Leffler {
18058a1b9b6aSSam Leffler 	struct ieee80211com *ic = ni->ni_ic;
18068a1b9b6aSSam Leffler 	struct ieee80211req_mlme *mlme = arg;
18078a1b9b6aSSam Leffler 
18088a1b9b6aSSam Leffler 	if (ni->ni_associd != 0) {
18098a1b9b6aSSam Leffler 		IEEE80211_SEND_MGMT(ic, ni,
18108a1b9b6aSSam Leffler 			mlme->im_op == IEEE80211_MLME_DEAUTH ?
18118a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_DEAUTH :
18128a1b9b6aSSam Leffler 				IEEE80211_FC0_SUBTYPE_DISASSOC,
18138a1b9b6aSSam Leffler 			mlme->im_reason);
18148a1b9b6aSSam Leffler 	}
18158a1b9b6aSSam Leffler 	ieee80211_node_leave(ic, ni);
18168a1b9b6aSSam Leffler }
18178a1b9b6aSSam Leffler 
18188a1b9b6aSSam Leffler static int
18198a1b9b6aSSam Leffler ieee80211_ioctl_setmlme(struct ieee80211com *ic, struct ieee80211req *ireq)
18208a1b9b6aSSam Leffler {
18218a1b9b6aSSam Leffler 	struct ieee80211req_mlme mlme;
18228a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
18238a1b9b6aSSam Leffler 	int error;
18248a1b9b6aSSam Leffler 
18258a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mlme))
18268a1b9b6aSSam Leffler 		return EINVAL;
18278a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &mlme, sizeof(mlme));
18288a1b9b6aSSam Leffler 	if (error)
18298a1b9b6aSSam Leffler 		return error;
18308a1b9b6aSSam Leffler 	switch (mlme.im_op) {
18318a1b9b6aSSam Leffler 	case IEEE80211_MLME_ASSOC:
18328a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_STA)
18338a1b9b6aSSam Leffler 			return EINVAL;
18348a1b9b6aSSam Leffler 		/* XXX must be in S_SCAN state? */
18358a1b9b6aSSam Leffler 
1836f02a0bd2SSam Leffler 		if (mlme.im_ssid_len != 0) {
18378a1b9b6aSSam Leffler 			/*
18388a1b9b6aSSam Leffler 			 * Desired ssid specified; must match both bssid and
18398a1b9b6aSSam Leffler 			 * ssid to distinguish ap advertising multiple ssid's.
18408a1b9b6aSSam Leffler 			 */
18418a1b9b6aSSam Leffler 			ni = ieee80211_find_node_with_ssid(&ic->ic_scan,
18428a1b9b6aSSam Leffler 				mlme.im_macaddr,
1843f02a0bd2SSam Leffler 				mlme.im_ssid_len, mlme.im_ssid);
18448a1b9b6aSSam Leffler 		} else {
18458a1b9b6aSSam Leffler 			/*
18468a1b9b6aSSam Leffler 			 * Normal case; just match bssid.
18478a1b9b6aSSam Leffler 			 */
18488a1b9b6aSSam Leffler 			ni = ieee80211_find_node(&ic->ic_scan, mlme.im_macaddr);
18498a1b9b6aSSam Leffler 		}
18508a1b9b6aSSam Leffler 		if (ni == NULL)
18518a1b9b6aSSam Leffler 			return EINVAL;
18528a1b9b6aSSam Leffler 		if (!ieee80211_sta_join(ic, ni)) {
18538a1b9b6aSSam Leffler 			ieee80211_free_node(ni);
18548a1b9b6aSSam Leffler 			return EINVAL;
18558a1b9b6aSSam Leffler 		}
18561a1e1d21SSam Leffler 		break;
18578a1b9b6aSSam Leffler 	case IEEE80211_MLME_DISASSOC:
18588a1b9b6aSSam Leffler 	case IEEE80211_MLME_DEAUTH:
18598a1b9b6aSSam Leffler 		switch (ic->ic_opmode) {
18608a1b9b6aSSam Leffler 		case IEEE80211_M_STA:
18618a1b9b6aSSam Leffler 			/* XXX not quite right */
18628a1b9b6aSSam Leffler 			ieee80211_new_state(ic, IEEE80211_S_INIT,
18638a1b9b6aSSam Leffler 				mlme.im_reason);
18648a1b9b6aSSam Leffler 			break;
18658a1b9b6aSSam Leffler 		case IEEE80211_M_HOSTAP:
18668a1b9b6aSSam Leffler 			/* NB: the broadcast address means do 'em all */
18678a1b9b6aSSam Leffler 			if (!IEEE80211_ADDR_EQ(mlme.im_macaddr, ic->ic_ifp->if_broadcastaddr)) {
1868acc4f7f5SSam Leffler 				if ((ni = ieee80211_find_node(&ic->ic_sta,
18698a1b9b6aSSam Leffler 						mlme.im_macaddr)) == NULL)
18708a1b9b6aSSam Leffler 					return EINVAL;
18718a1b9b6aSSam Leffler 				domlme(&mlme, ni);
18728a1b9b6aSSam Leffler 				ieee80211_free_node(ni);
18738a1b9b6aSSam Leffler 			} else {
1874acc4f7f5SSam Leffler 				ieee80211_iterate_nodes(&ic->ic_sta,
18758a1b9b6aSSam Leffler 						domlme, &mlme);
18768a1b9b6aSSam Leffler 			}
18778a1b9b6aSSam Leffler 			break;
18788a1b9b6aSSam Leffler 		default:
18798a1b9b6aSSam Leffler 			return EINVAL;
18808a1b9b6aSSam Leffler 		}
18818a1b9b6aSSam Leffler 		break;
18828a1b9b6aSSam Leffler 	case IEEE80211_MLME_AUTHORIZE:
18838a1b9b6aSSam Leffler 	case IEEE80211_MLME_UNAUTHORIZE:
18848a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP)
18858a1b9b6aSSam Leffler 			return EINVAL;
1886acc4f7f5SSam Leffler 		ni = ieee80211_find_node(&ic->ic_sta, mlme.im_macaddr);
18878a1b9b6aSSam Leffler 		if (ni == NULL)
18888a1b9b6aSSam Leffler 			return EINVAL;
18898a1b9b6aSSam Leffler 		if (mlme.im_op == IEEE80211_MLME_AUTHORIZE)
1890e4918ecdSSam Leffler 			ieee80211_node_authorize(ni);
18918a1b9b6aSSam Leffler 		else
1892e4918ecdSSam Leffler 			ieee80211_node_unauthorize(ni);
18938a1b9b6aSSam Leffler 		ieee80211_free_node(ni);
18948a1b9b6aSSam Leffler 		break;
18958a1b9b6aSSam Leffler 	default:
18968a1b9b6aSSam Leffler 		return EINVAL;
18978a1b9b6aSSam Leffler 	}
18988a1b9b6aSSam Leffler 	return 0;
18998a1b9b6aSSam Leffler }
19008a1b9b6aSSam Leffler 
19018a1b9b6aSSam Leffler static int
19028a1b9b6aSSam Leffler ieee80211_ioctl_macmac(struct ieee80211com *ic, struct ieee80211req *ireq)
19038a1b9b6aSSam Leffler {
19048a1b9b6aSSam Leffler 	u_int8_t mac[IEEE80211_ADDR_LEN];
19058a1b9b6aSSam Leffler 	const struct ieee80211_aclator *acl = ic->ic_acl;
19068a1b9b6aSSam Leffler 	int error;
19078a1b9b6aSSam Leffler 
19088a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(mac))
19098a1b9b6aSSam Leffler 		return EINVAL;
19108a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, mac, ireq->i_len);
19118a1b9b6aSSam Leffler 	if (error)
19128a1b9b6aSSam Leffler 		return error;
19138a1b9b6aSSam Leffler 	if (acl == NULL) {
19148a1b9b6aSSam Leffler 		acl = ieee80211_aclator_get("mac");
19158a1b9b6aSSam Leffler 		if (acl == NULL || !acl->iac_attach(ic))
19168a1b9b6aSSam Leffler 			return EINVAL;
19178a1b9b6aSSam Leffler 		ic->ic_acl = acl;
19188a1b9b6aSSam Leffler 	}
19198a1b9b6aSSam Leffler 	if (ireq->i_type == IEEE80211_IOC_ADDMAC)
19208a1b9b6aSSam Leffler 		acl->iac_add(ic, mac);
19218a1b9b6aSSam Leffler 	else
19228a1b9b6aSSam Leffler 		acl->iac_remove(ic, mac);
19238a1b9b6aSSam Leffler 	return 0;
19248a1b9b6aSSam Leffler }
19258a1b9b6aSSam Leffler 
19268a1b9b6aSSam Leffler static int
1927188757f5SSam Leffler ieee80211_ioctl_setmaccmd(struct ieee80211com *ic, struct ieee80211req *ireq)
19288a1b9b6aSSam Leffler {
19298a1b9b6aSSam Leffler 	const struct ieee80211_aclator *acl = ic->ic_acl;
19308a1b9b6aSSam Leffler 
19318a1b9b6aSSam Leffler 	switch (ireq->i_val) {
19328a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_OPEN:
19338a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_ALLOW:
19348a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_POLICY_DENY:
19358a1b9b6aSSam Leffler 		if (acl == NULL) {
19368a1b9b6aSSam Leffler 			acl = ieee80211_aclator_get("mac");
19378a1b9b6aSSam Leffler 			if (acl == NULL || !acl->iac_attach(ic))
19388a1b9b6aSSam Leffler 				return EINVAL;
19398a1b9b6aSSam Leffler 			ic->ic_acl = acl;
19408a1b9b6aSSam Leffler 		}
19418a1b9b6aSSam Leffler 		acl->iac_setpolicy(ic, ireq->i_val);
19428a1b9b6aSSam Leffler 		break;
19438a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_FLUSH:
19448a1b9b6aSSam Leffler 		if (acl != NULL)
19458a1b9b6aSSam Leffler 			acl->iac_flush(ic);
19468a1b9b6aSSam Leffler 		/* NB: silently ignore when not in use */
19478a1b9b6aSSam Leffler 		break;
19488a1b9b6aSSam Leffler 	case IEEE80211_MACCMD_DETACH:
19498a1b9b6aSSam Leffler 		if (acl != NULL) {
19508a1b9b6aSSam Leffler 			ic->ic_acl = NULL;
19518a1b9b6aSSam Leffler 			acl->iac_detach(ic);
19528a1b9b6aSSam Leffler 		}
19538a1b9b6aSSam Leffler 		break;
19548a1b9b6aSSam Leffler 	default:
1955188757f5SSam Leffler 		if (acl == NULL)
19568a1b9b6aSSam Leffler 			return EINVAL;
1957188757f5SSam Leffler 		else
1958188757f5SSam Leffler 			return acl->iac_setioctl(ic, ireq);
19598a1b9b6aSSam Leffler 	}
19608a1b9b6aSSam Leffler 	return 0;
19618a1b9b6aSSam Leffler }
19628a1b9b6aSSam Leffler 
19638a1b9b6aSSam Leffler static int
19648a1b9b6aSSam Leffler ieee80211_ioctl_setchanlist(struct ieee80211com *ic, struct ieee80211req *ireq)
19658a1b9b6aSSam Leffler {
19668a1b9b6aSSam Leffler 	struct ieee80211req_chanlist list;
19678a1b9b6aSSam Leffler 	u_char chanlist[IEEE80211_CHAN_BYTES];
19688a1b9b6aSSam Leffler 	int i, j, error;
19698a1b9b6aSSam Leffler 
19708a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(list))
19718a1b9b6aSSam Leffler 		return EINVAL;
19728a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &list, sizeof(list));
19738a1b9b6aSSam Leffler 	if (error)
19748a1b9b6aSSam Leffler 		return error;
19758a1b9b6aSSam Leffler 	memset(chanlist, 0, sizeof(chanlist));
19768a1b9b6aSSam Leffler 	/*
19778a1b9b6aSSam Leffler 	 * Since channel 0 is not available for DS, channel 1
19788a1b9b6aSSam Leffler 	 * is assigned to LSB on WaveLAN.
19798a1b9b6aSSam Leffler 	 */
19808a1b9b6aSSam Leffler 	if (ic->ic_phytype == IEEE80211_T_DS)
19818a1b9b6aSSam Leffler 		i = 1;
19828a1b9b6aSSam Leffler 	else
19838a1b9b6aSSam Leffler 		i = 0;
19848a1b9b6aSSam Leffler 	for (j = 0; i <= IEEE80211_CHAN_MAX; i++, j++) {
19858a1b9b6aSSam Leffler 		/*
19868a1b9b6aSSam Leffler 		 * NB: silently discard unavailable channels so users
19878a1b9b6aSSam Leffler 		 *     can specify 1-255 to get all available channels.
19888a1b9b6aSSam Leffler 		 */
19898a1b9b6aSSam Leffler 		if (isset(list.ic_channels, j) && isset(ic->ic_chan_avail, i))
19908a1b9b6aSSam Leffler 			setbit(chanlist, i);
19918a1b9b6aSSam Leffler 	}
19928a1b9b6aSSam Leffler 	if (ic->ic_ibss_chan == NULL ||
19938a1b9b6aSSam Leffler 	    isclr(chanlist, ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
19948a1b9b6aSSam Leffler 		for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
19958a1b9b6aSSam Leffler 			if (isset(chanlist, i)) {
19968a1b9b6aSSam Leffler 				ic->ic_ibss_chan = &ic->ic_channels[i];
19978a1b9b6aSSam Leffler 				goto found;
19988a1b9b6aSSam Leffler 			}
19998a1b9b6aSSam Leffler 		return EINVAL;			/* no active channels */
20008a1b9b6aSSam Leffler found:
20018a1b9b6aSSam Leffler 		;
20028a1b9b6aSSam Leffler 	}
20038a1b9b6aSSam Leffler 	memcpy(ic->ic_chan_active, chanlist, sizeof(ic->ic_chan_active));
20048a1b9b6aSSam Leffler 	return IS_UP_AUTO(ic) ? ENETRESET : 0;
20058a1b9b6aSSam Leffler }
20068a1b9b6aSSam Leffler 
20078a1b9b6aSSam Leffler static int
20088a1b9b6aSSam Leffler ieee80211_ioctl_setstatxpow(struct ieee80211com *ic, struct ieee80211req *ireq)
20098a1b9b6aSSam Leffler {
20108a1b9b6aSSam Leffler 	struct ieee80211_node *ni;
20118a1b9b6aSSam Leffler 	struct ieee80211req_sta_txpow txpow;
20128a1b9b6aSSam Leffler 	int error;
20138a1b9b6aSSam Leffler 
20148a1b9b6aSSam Leffler 	if (ireq->i_len != sizeof(txpow))
20158a1b9b6aSSam Leffler 		return EINVAL;
20168a1b9b6aSSam Leffler 	error = copyin(ireq->i_data, &txpow, sizeof(txpow));
20178a1b9b6aSSam Leffler 	if (error != 0)
20188a1b9b6aSSam Leffler 		return error;
2019acc4f7f5SSam Leffler 	ni = ieee80211_find_node(&ic->ic_sta, txpow.it_macaddr);
20208a1b9b6aSSam Leffler 	if (ni == NULL)
20218a1b9b6aSSam Leffler 		return EINVAL;		/* XXX */
20228a1b9b6aSSam Leffler 	ni->ni_txpower = txpow.it_txpow;
20238a1b9b6aSSam Leffler 	ieee80211_free_node(ni);
20248a1b9b6aSSam Leffler 	return error;
20258a1b9b6aSSam Leffler }
20268a1b9b6aSSam Leffler 
20278a1b9b6aSSam Leffler static int
20288a1b9b6aSSam Leffler ieee80211_ioctl_setwmeparam(struct ieee80211com *ic, struct ieee80211req *ireq)
20298a1b9b6aSSam Leffler {
20308a1b9b6aSSam Leffler 	struct ieee80211_wme_state *wme = &ic->ic_wme;
20318a1b9b6aSSam Leffler 	struct wmeParams *wmep, *chanp;
20328a1b9b6aSSam Leffler 	int isbss, ac;
20338a1b9b6aSSam Leffler 
20348a1b9b6aSSam Leffler 	if ((ic->ic_caps & IEEE80211_C_WME) == 0)
20358a1b9b6aSSam Leffler 		return EINVAL;
20368a1b9b6aSSam Leffler 
20378a1b9b6aSSam Leffler 	isbss = (ireq->i_len & IEEE80211_WMEPARAM_BSS);
20388a1b9b6aSSam Leffler 	ac = (ireq->i_len & IEEE80211_WMEPARAM_VAL);
20398a1b9b6aSSam Leffler 	if (ac >= WME_NUM_AC)
20408a1b9b6aSSam Leffler 		ac = WME_AC_BE;
20418a1b9b6aSSam Leffler 	if (isbss) {
20428a1b9b6aSSam Leffler 		chanp = &wme->wme_bssChanParams.cap_wmeParams[ac];
20438a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeBssChanParams.cap_wmeParams[ac];
20448a1b9b6aSSam Leffler 	} else {
20458a1b9b6aSSam Leffler 		chanp = &wme->wme_chanParams.cap_wmeParams[ac];
20468a1b9b6aSSam Leffler 		wmep = &wme->wme_wmeChanParams.cap_wmeParams[ac];
20478a1b9b6aSSam Leffler 	}
20488a1b9b6aSSam Leffler 	switch (ireq->i_type) {
20498a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
20508a1b9b6aSSam Leffler 		if (isbss) {
20518a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = ireq->i_val;
20528a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
20538a1b9b6aSSam Leffler 				chanp->wmep_logcwmin = ireq->i_val;
20548a1b9b6aSSam Leffler 		} else {
20558a1b9b6aSSam Leffler 			wmep->wmep_logcwmin = chanp->wmep_logcwmin =
20568a1b9b6aSSam Leffler 				ireq->i_val;
20578a1b9b6aSSam Leffler 		}
20588a1b9b6aSSam Leffler 		break;
20598a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
20608a1b9b6aSSam Leffler 		if (isbss) {
20618a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = ireq->i_val;
20628a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
20638a1b9b6aSSam Leffler 				chanp->wmep_logcwmax = ireq->i_val;
20648a1b9b6aSSam Leffler 		} else {
20658a1b9b6aSSam Leffler 			wmep->wmep_logcwmax = chanp->wmep_logcwmax =
20668a1b9b6aSSam Leffler 				ireq->i_val;
20678a1b9b6aSSam Leffler 		}
20688a1b9b6aSSam Leffler 		break;
20698a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
20708a1b9b6aSSam Leffler 		if (isbss) {
20718a1b9b6aSSam Leffler 			wmep->wmep_aifsn = ireq->i_val;
20728a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
20738a1b9b6aSSam Leffler 				chanp->wmep_aifsn = ireq->i_val;
20748a1b9b6aSSam Leffler 		} else {
20758a1b9b6aSSam Leffler 			wmep->wmep_aifsn = chanp->wmep_aifsn = ireq->i_val;
20768a1b9b6aSSam Leffler 		}
20778a1b9b6aSSam Leffler 		break;
20788a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
20798a1b9b6aSSam Leffler 		if (isbss) {
20808a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = ireq->i_val;
20818a1b9b6aSSam Leffler 			if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
20828a1b9b6aSSam Leffler 				chanp->wmep_txopLimit = ireq->i_val;
20838a1b9b6aSSam Leffler 		} else {
20848a1b9b6aSSam Leffler 			wmep->wmep_txopLimit = chanp->wmep_txopLimit =
20858a1b9b6aSSam Leffler 				ireq->i_val;
20868a1b9b6aSSam Leffler 		}
20878a1b9b6aSSam Leffler 		break;
20888a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
20898a1b9b6aSSam Leffler 		wmep->wmep_acm = ireq->i_val;
20908a1b9b6aSSam Leffler 		if ((wme->wme_flags & WME_F_AGGRMODE) == 0)
20918a1b9b6aSSam Leffler 			chanp->wmep_acm = ireq->i_val;
20928a1b9b6aSSam Leffler 		break;
20938a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (!bss only)*/
20948a1b9b6aSSam Leffler 		wmep->wmep_noackPolicy = chanp->wmep_noackPolicy =
20958a1b9b6aSSam Leffler 			(ireq->i_val) == 0;
20968a1b9b6aSSam Leffler 		break;
20978a1b9b6aSSam Leffler 	}
20988a1b9b6aSSam Leffler 	ieee80211_wme_updateparams(ic);
20998a1b9b6aSSam Leffler 	return 0;
21008a1b9b6aSSam Leffler }
21018a1b9b6aSSam Leffler 
21028a1b9b6aSSam Leffler static int
21038a1b9b6aSSam Leffler cipher2cap(int cipher)
21048a1b9b6aSSam Leffler {
21058a1b9b6aSSam Leffler 	switch (cipher) {
21068a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_WEP:	return IEEE80211_C_WEP;
21078a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_AES_OCB:	return IEEE80211_C_AES;
21088a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_AES_CCM:	return IEEE80211_C_AES_CCM;
21098a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_CKIP:	return IEEE80211_C_CKIP;
21108a1b9b6aSSam Leffler 	case IEEE80211_CIPHER_TKIP:	return IEEE80211_C_TKIP;
21118a1b9b6aSSam Leffler 	}
21128a1b9b6aSSam Leffler 	return 0;
21138a1b9b6aSSam Leffler }
21148a1b9b6aSSam Leffler 
21158a1b9b6aSSam Leffler static int
21168a1b9b6aSSam Leffler ieee80211_ioctl_set80211(struct ieee80211com *ic, u_long cmd, struct ieee80211req *ireq)
21178a1b9b6aSSam Leffler {
21188a1b9b6aSSam Leffler 	static const u_int8_t zerobssid[IEEE80211_ADDR_LEN];
21198a1b9b6aSSam Leffler 	struct ieee80211_rsnparms *rsn = &ic->ic_bss->ni_rsn;
21208a1b9b6aSSam Leffler 	int error;
21218a1b9b6aSSam Leffler 	const struct ieee80211_authenticator *auth;
21228a1b9b6aSSam Leffler 	u_int8_t tmpkey[IEEE80211_KEYBUF_SIZE];
21238a1b9b6aSSam Leffler 	char tmpssid[IEEE80211_NWID_LEN];
21248a1b9b6aSSam Leffler 	u_int8_t tmpbssid[IEEE80211_ADDR_LEN];
21258a1b9b6aSSam Leffler 	struct ieee80211_key *k;
21268a1b9b6aSSam Leffler 	int j, caps;
21278a1b9b6aSSam Leffler 	u_int kid;
21288a1b9b6aSSam Leffler 
21298a1b9b6aSSam Leffler 	error = 0;
21301a1e1d21SSam Leffler 	switch (ireq->i_type) {
21311a1e1d21SSam Leffler 	case IEEE80211_IOC_SSID:
21321a1e1d21SSam Leffler 		if (ireq->i_val != 0 ||
21338a1b9b6aSSam Leffler 		    ireq->i_len > IEEE80211_NWID_LEN)
21348a1b9b6aSSam Leffler 			return EINVAL;
21351a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpssid, ireq->i_len);
21361a1e1d21SSam Leffler 		if (error)
21371a1e1d21SSam Leffler 			break;
21381a1e1d21SSam Leffler 		memset(ic->ic_des_essid, 0, IEEE80211_NWID_LEN);
21391a1e1d21SSam Leffler 		ic->ic_des_esslen = ireq->i_len;
21401a1e1d21SSam Leffler 		memcpy(ic->ic_des_essid, tmpssid, ireq->i_len);
21411a1e1d21SSam Leffler 		error = ENETRESET;
21421a1e1d21SSam Leffler 		break;
21431a1e1d21SSam Leffler 	case IEEE80211_IOC_WEP:
21448a1b9b6aSSam Leffler 		switch (ireq->i_val) {
21458a1b9b6aSSam Leffler 		case IEEE80211_WEP_OFF:
21468a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
21478a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
21488a1b9b6aSSam Leffler 			break;
21498a1b9b6aSSam Leffler 		case IEEE80211_WEP_ON:
21508a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
21518a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
21528a1b9b6aSSam Leffler 			break;
21538a1b9b6aSSam Leffler 		case IEEE80211_WEP_MIXED:
21548a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
21558a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
21568a1b9b6aSSam Leffler 			break;
21571a1e1d21SSam Leffler 		}
21581a1e1d21SSam Leffler 		error = ENETRESET;
21591a1e1d21SSam Leffler 		break;
21601a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPKEY:
21611a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
21628a1b9b6aSSam Leffler 		if (kid >= IEEE80211_WEP_NKID)
21638a1b9b6aSSam Leffler 			return EINVAL;
21648a1b9b6aSSam Leffler 		k = &ic->ic_nw_keys[kid];
21658a1b9b6aSSam Leffler 		if (ireq->i_len == 0) {
21668a1b9b6aSSam Leffler 			/* zero-len =>'s delete any existing key */
21678a1b9b6aSSam Leffler 			(void) ieee80211_crypto_delkey(ic, k);
21681a1e1d21SSam Leffler 			break;
21691a1e1d21SSam Leffler 		}
21708a1b9b6aSSam Leffler 		if (ireq->i_len > sizeof(tmpkey))
21718a1b9b6aSSam Leffler 			return EINVAL;
21721a1e1d21SSam Leffler 		memset(tmpkey, 0, sizeof(tmpkey));
21731a1e1d21SSam Leffler 		error = copyin(ireq->i_data, tmpkey, ireq->i_len);
21741a1e1d21SSam Leffler 		if (error)
21751a1e1d21SSam Leffler 			break;
21768a1b9b6aSSam Leffler 		ieee80211_key_update_begin(ic);
2177dd70e17bSSam Leffler 		k->wk_keyix = kid;	/* NB: force fixed key id */
2178dd70e17bSSam Leffler 		if (ieee80211_crypto_newkey(ic, IEEE80211_CIPHER_WEP,
2179dd70e17bSSam Leffler 		    IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV, k)) {
21808a1b9b6aSSam Leffler 			k->wk_keylen = ireq->i_len;
21818a1b9b6aSSam Leffler 			memcpy(k->wk_key, tmpkey, sizeof(tmpkey));
21828a1b9b6aSSam Leffler 			if  (!ieee80211_crypto_setkey(ic, k, ic->ic_myaddr))
21838a1b9b6aSSam Leffler 				error = EINVAL;
21848a1b9b6aSSam Leffler 		} else
21858a1b9b6aSSam Leffler 			error = EINVAL;
21868a1b9b6aSSam Leffler 		ieee80211_key_update_end(ic);
2187b5d4660fSSam Leffler 		if (!error)			/* NB: for compatibility */
2188b5d4660fSSam Leffler 			error = ENETRESET;
21891a1e1d21SSam Leffler 		break;
21901a1e1d21SSam Leffler 	case IEEE80211_IOC_WEPTXKEY:
21911a1e1d21SSam Leffler 		kid = (u_int) ireq->i_val;
2192380c5fa9SSam Leffler 		if (kid >= IEEE80211_WEP_NKID &&
2193380c5fa9SSam Leffler 		    (u_int16_t) kid != IEEE80211_KEYIX_NONE)
21948a1b9b6aSSam Leffler 			return EINVAL;
21958a1b9b6aSSam Leffler 		ic->ic_def_txkey = kid;
2196b8d2606bSSam Leffler 		error = ENETRESET;	/* push to hardware */
21978a1b9b6aSSam Leffler 		break;
21988a1b9b6aSSam Leffler 	case IEEE80211_IOC_AUTHMODE:
21998a1b9b6aSSam Leffler 		switch (ireq->i_val) {
22008a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:
22018a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
22028a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
22038a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
22048a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
22058a1b9b6aSSam Leffler 			auth = ieee80211_authenticator_get(ireq->i_val);
22068a1b9b6aSSam Leffler 			if (auth == NULL)
22078a1b9b6aSSam Leffler 				return EINVAL;
22088a1b9b6aSSam Leffler 			break;
22098a1b9b6aSSam Leffler 		default:
22108a1b9b6aSSam Leffler 			return EINVAL;
22118a1b9b6aSSam Leffler 		}
22128a1b9b6aSSam Leffler 		switch (ireq->i_val) {
22138a1b9b6aSSam Leffler 		case IEEE80211_AUTH_WPA:	/* WPA w/ 802.1x */
22148a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
22158a1b9b6aSSam Leffler 			ireq->i_val = IEEE80211_AUTH_8021X;
22168a1b9b6aSSam Leffler 			break;
22178a1b9b6aSSam Leffler 		case IEEE80211_AUTH_OPEN:	/* open */
22188a1b9b6aSSam Leffler 			ic->ic_flags &= ~(IEEE80211_F_WPA|IEEE80211_F_PRIVACY);
22198a1b9b6aSSam Leffler 			break;
22208a1b9b6aSSam Leffler 		case IEEE80211_AUTH_SHARED:	/* shared-key */
22218a1b9b6aSSam Leffler 		case IEEE80211_AUTH_8021X:	/* 802.1x */
22228a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WPA;
22238a1b9b6aSSam Leffler 			/* both require a key so mark the PRIVACY capability */
22248a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
22258a1b9b6aSSam Leffler 			break;
22268a1b9b6aSSam Leffler 		case IEEE80211_AUTH_AUTO:	/* auto */
22278a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WPA;
22288a1b9b6aSSam Leffler 			/* XXX PRIVACY handling? */
22298a1b9b6aSSam Leffler 			/* XXX what's the right way to do this? */
22301a1e1d21SSam Leffler 			break;
22311a1e1d21SSam Leffler 		}
22328a1b9b6aSSam Leffler 		/* NB: authenticator attach/detach happens on state change */
22338a1b9b6aSSam Leffler 		ic->ic_bss->ni_authmode = ireq->i_val;
22348a1b9b6aSSam Leffler 		/* XXX mixed/mode/usage? */
22358a1b9b6aSSam Leffler 		ic->ic_auth = auth;
22361a1e1d21SSam Leffler 		error = ENETRESET;
22371a1e1d21SSam Leffler 		break;
22381a1e1d21SSam Leffler 	case IEEE80211_IOC_CHANNEL:
22391a1e1d21SSam Leffler 		/* XXX 0xffff overflows 16-bit signed */
22401a1e1d21SSam Leffler 		if (ireq->i_val == 0 ||
22411a1e1d21SSam Leffler 		    ireq->i_val == (int16_t) IEEE80211_CHAN_ANY)
22421a1e1d21SSam Leffler 			ic->ic_des_chan = IEEE80211_CHAN_ANYC;
22431a1e1d21SSam Leffler 		else if ((u_int) ireq->i_val > IEEE80211_CHAN_MAX ||
22441a1e1d21SSam Leffler 		    isclr(ic->ic_chan_active, ireq->i_val)) {
22458a1b9b6aSSam Leffler 			return EINVAL;
22461a1e1d21SSam Leffler 		} else
22471a1e1d21SSam Leffler 			ic->ic_ibss_chan = ic->ic_des_chan =
22481a1e1d21SSam Leffler 				&ic->ic_channels[ireq->i_val];
22491a1e1d21SSam Leffler 		switch (ic->ic_state) {
22501a1e1d21SSam Leffler 		case IEEE80211_S_INIT:
22511a1e1d21SSam Leffler 		case IEEE80211_S_SCAN:
22521a1e1d21SSam Leffler 			error = ENETRESET;
22531a1e1d21SSam Leffler 			break;
22541a1e1d21SSam Leffler 		default:
22558a1b9b6aSSam Leffler 			/*
22568a1b9b6aSSam Leffler 			 * If the desired channel has changed (to something
22578a1b9b6aSSam Leffler 			 * other than any) and we're not already scanning,
22588a1b9b6aSSam Leffler 			 * then kick the state machine.
22598a1b9b6aSSam Leffler 			 */
22601a1e1d21SSam Leffler 			if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
22618a1b9b6aSSam Leffler 			    ic->ic_bss->ni_chan != ic->ic_des_chan &&
22628a1b9b6aSSam Leffler 			    (ic->ic_flags & IEEE80211_F_SCAN) == 0)
22631a1e1d21SSam Leffler 				error = ENETRESET;
22641a1e1d21SSam Leffler 			break;
22651a1e1d21SSam Leffler 		}
22668157976aSTai-hwa Liang 		if (error == ENETRESET &&
22678157976aSTai-hwa Liang 			ic->ic_opmode == IEEE80211_M_MONITOR) {
22688157976aSTai-hwa Liang 			if (IS_UP(ic)) {
22698157976aSTai-hwa Liang 				/*
22708157976aSTai-hwa Liang 				 * Monitor mode can switch directly.
22718157976aSTai-hwa Liang 				 */
22728157976aSTai-hwa Liang 				if (ic->ic_des_chan != IEEE80211_CHAN_ANYC)
22738157976aSTai-hwa Liang 					ic->ic_curchan = ic->ic_des_chan;
22748157976aSTai-hwa Liang 				error = ic->ic_reset(ic->ic_ifp);
22758157976aSTai-hwa Liang 			} else
22768157976aSTai-hwa Liang 				error = 0;
22778157976aSTai-hwa Liang 		}
22781a1e1d21SSam Leffler 		break;
22791a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVE:
22801a1e1d21SSam Leffler 		switch (ireq->i_val) {
22811a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_OFF:
22821a1e1d21SSam Leffler 			if (ic->ic_flags & IEEE80211_F_PMGTON) {
22831a1e1d21SSam Leffler 				ic->ic_flags &= ~IEEE80211_F_PMGTON;
22841a1e1d21SSam Leffler 				error = ENETRESET;
22851a1e1d21SSam Leffler 			}
22861a1e1d21SSam Leffler 			break;
22871a1e1d21SSam Leffler 		case IEEE80211_POWERSAVE_ON:
22881a1e1d21SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_PMGT) == 0)
22891a1e1d21SSam Leffler 				error = EINVAL;
22901a1e1d21SSam Leffler 			else if ((ic->ic_flags & IEEE80211_F_PMGTON) == 0) {
22911a1e1d21SSam Leffler 				ic->ic_flags |= IEEE80211_F_PMGTON;
22921a1e1d21SSam Leffler 				error = ENETRESET;
22931a1e1d21SSam Leffler 			}
22941a1e1d21SSam Leffler 			break;
22951a1e1d21SSam Leffler 		default:
22961a1e1d21SSam Leffler 			error = EINVAL;
22971a1e1d21SSam Leffler 			break;
22981a1e1d21SSam Leffler 		}
22991a1e1d21SSam Leffler 		break;
23001a1e1d21SSam Leffler 	case IEEE80211_IOC_POWERSAVESLEEP:
23018a1b9b6aSSam Leffler 		if (ireq->i_val < 0)
23028a1b9b6aSSam Leffler 			return EINVAL;
23031a1e1d21SSam Leffler 		ic->ic_lintval = ireq->i_val;
23048a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
23051a1e1d21SSam Leffler 		break;
230613604e6bSSam Leffler 	case IEEE80211_IOC_RTSTHRESHOLD:
230770231e3dSSam Leffler 		if (!(IEEE80211_RTS_MIN <= ireq->i_val &&
230870231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_RTS_MAX))
23098a1b9b6aSSam Leffler 			return EINVAL;
23101a1e1d21SSam Leffler 		ic->ic_rtsthreshold = ireq->i_val;
23118a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
23121a1e1d21SSam Leffler 		break;
23132e79ca97SSam Leffler 	case IEEE80211_IOC_PROTMODE:
23148a1b9b6aSSam Leffler 		if (ireq->i_val > IEEE80211_PROT_RTSCTS)
23158a1b9b6aSSam Leffler 			return EINVAL;
23162e79ca97SSam Leffler 		ic->ic_protmode = ireq->i_val;
23172e79ca97SSam Leffler 		/* NB: if not operating in 11g this can wait */
23182e79ca97SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
23198a1b9b6aSSam Leffler 			error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
23202e79ca97SSam Leffler 		break;
23212e79ca97SSam Leffler 	case IEEE80211_IOC_TXPOWER:
23228a1b9b6aSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXPMGT) == 0)
23238a1b9b6aSSam Leffler 			return EINVAL;
23242e79ca97SSam Leffler 		if (!(IEEE80211_TXPOWER_MIN < ireq->i_val &&
23258a1b9b6aSSam Leffler 		      ireq->i_val < IEEE80211_TXPOWER_MAX))
23268a1b9b6aSSam Leffler 			return EINVAL;
23278a1b9b6aSSam Leffler 		ic->ic_txpowlimit = ireq->i_val;
23288a1b9b6aSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
23298a1b9b6aSSam Leffler 		break;
23308a1b9b6aSSam Leffler 	case IEEE80211_IOC_ROAMING:
23318a1b9b6aSSam Leffler 		if (!(IEEE80211_ROAMING_DEVICE <= ireq->i_val &&
23328a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_ROAMING_MANUAL))
23338a1b9b6aSSam Leffler 			return EINVAL;
23348a1b9b6aSSam Leffler 		ic->ic_roaming = ireq->i_val;
23358a1b9b6aSSam Leffler 		/* XXXX reset? */
23368a1b9b6aSSam Leffler 		break;
23378a1b9b6aSSam Leffler 	case IEEE80211_IOC_PRIVACY:
23388a1b9b6aSSam Leffler 		if (ireq->i_val) {
23398a1b9b6aSSam Leffler 			/* XXX check for key state? */
23408a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_PRIVACY;
23418a1b9b6aSSam Leffler 		} else
23428a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PRIVACY;
23438a1b9b6aSSam Leffler 		break;
23448a1b9b6aSSam Leffler 	case IEEE80211_IOC_DROPUNENCRYPTED:
23458a1b9b6aSSam Leffler 		if (ireq->i_val)
23468a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DROPUNENC;
23478a1b9b6aSSam Leffler 		else
23488a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
23498a1b9b6aSSam Leffler 		break;
23508a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPAKEY:
23518a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setkey(ic, ireq);
23528a1b9b6aSSam Leffler 		break;
23538a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELKEY:
23548a1b9b6aSSam Leffler 		error = ieee80211_ioctl_delkey(ic, ireq);
23558a1b9b6aSSam Leffler 		break;
23568a1b9b6aSSam Leffler 	case IEEE80211_IOC_MLME:
23578a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setmlme(ic, ireq);
23588a1b9b6aSSam Leffler 		break;
23598a1b9b6aSSam Leffler 	case IEEE80211_IOC_OPTIE:
23608a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setoptie(ic, ireq);
23618a1b9b6aSSam Leffler 		break;
23628a1b9b6aSSam Leffler 	case IEEE80211_IOC_COUNTERMEASURES:
23638a1b9b6aSSam Leffler 		if (ireq->i_val) {
23648a1b9b6aSSam Leffler 			if ((ic->ic_flags & IEEE80211_F_WPA) == 0)
23658a1b9b6aSSam Leffler 				return EINVAL;
23668a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_COUNTERM;
23678a1b9b6aSSam Leffler 		} else
23688a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_COUNTERM;
23698a1b9b6aSSam Leffler 		break;
23708a1b9b6aSSam Leffler 	case IEEE80211_IOC_WPA:
23718a1b9b6aSSam Leffler 		if (ireq->i_val > 3)
23728a1b9b6aSSam Leffler 			return EINVAL;
23738a1b9b6aSSam Leffler 		/* XXX verify ciphers available */
23748a1b9b6aSSam Leffler 		ic->ic_flags &= ~IEEE80211_F_WPA;
23758a1b9b6aSSam Leffler 		switch (ireq->i_val) {
23768a1b9b6aSSam Leffler 		case 1:
23778a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA1;
23788a1b9b6aSSam Leffler 			break;
23798a1b9b6aSSam Leffler 		case 2:
23808a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA2;
23818a1b9b6aSSam Leffler 			break;
23828a1b9b6aSSam Leffler 		case 3:
23838a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WPA1 | IEEE80211_F_WPA2;
23842e79ca97SSam Leffler 			break;
23852e79ca97SSam Leffler 		}
23868a1b9b6aSSam Leffler 		error = ENETRESET;		/* XXX? */
23878a1b9b6aSSam Leffler 		break;
23888a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME:
23898a1b9b6aSSam Leffler 		if (ireq->i_val) {
23908a1b9b6aSSam Leffler 			if ((ic->ic_caps & IEEE80211_C_WME) == 0)
23918a1b9b6aSSam Leffler 				return EINVAL;
23928a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_WME;
23938a1b9b6aSSam Leffler 		} else
23948a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_WME;
23958a1b9b6aSSam Leffler 		error = ENETRESET;		/* XXX maybe not for station? */
23968a1b9b6aSSam Leffler 		break;
23978a1b9b6aSSam Leffler 	case IEEE80211_IOC_HIDESSID:
23988a1b9b6aSSam Leffler 		if (ireq->i_val)
23998a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_HIDESSID;
24008a1b9b6aSSam Leffler 		else
24018a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_HIDESSID;
24022e79ca97SSam Leffler 		error = ENETRESET;
24032e79ca97SSam Leffler 		break;
24048a1b9b6aSSam Leffler 	case IEEE80211_IOC_APBRIDGE:
24058a1b9b6aSSam Leffler 		if (ireq->i_val == 0)
24068a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_NOBRIDGE;
24078a1b9b6aSSam Leffler 		else
24088a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_NOBRIDGE;
24098a1b9b6aSSam Leffler 		break;
24108a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTCIPHER:
24118a1b9b6aSSam Leffler 		if ((ic->ic_caps & cipher2cap(ireq->i_val)) == 0 &&
24128a1b9b6aSSam Leffler 		    !ieee80211_crypto_available(ireq->i_val))
24138a1b9b6aSSam Leffler 			return EINVAL;
24148a1b9b6aSSam Leffler 		rsn->rsn_mcastcipher = ireq->i_val;
24158a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
24168a1b9b6aSSam Leffler 		break;
24178a1b9b6aSSam Leffler 	case IEEE80211_IOC_MCASTKEYLEN:
24188a1b9b6aSSam Leffler 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
24198a1b9b6aSSam Leffler 			return EINVAL;
24208a1b9b6aSSam Leffler 		/* XXX no way to verify driver capability */
24218a1b9b6aSSam Leffler 		rsn->rsn_mcastkeylen = ireq->i_val;
24228a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
24238a1b9b6aSSam Leffler 		break;
24248a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHERS:
24258a1b9b6aSSam Leffler 		/*
24268a1b9b6aSSam Leffler 		 * Convert user-specified cipher set to the set
24278a1b9b6aSSam Leffler 		 * we can support (via hardware or software).
24288a1b9b6aSSam Leffler 		 * NB: this logic intentionally ignores unknown and
24298a1b9b6aSSam Leffler 		 * unsupported ciphers so folks can specify 0xff or
24308a1b9b6aSSam Leffler 		 * similar and get all available ciphers.
24318a1b9b6aSSam Leffler 		 */
24328a1b9b6aSSam Leffler 		caps = 0;
24338a1b9b6aSSam Leffler 		for (j = 1; j < 32; j++)	/* NB: skip WEP */
24348a1b9b6aSSam Leffler 			if ((ireq->i_val & (1<<j)) &&
24358a1b9b6aSSam Leffler 			    ((ic->ic_caps & cipher2cap(j)) ||
24368a1b9b6aSSam Leffler 			     ieee80211_crypto_available(j)))
24378a1b9b6aSSam Leffler 				caps |= 1<<j;
24388a1b9b6aSSam Leffler 		if (caps == 0)			/* nothing available */
24398a1b9b6aSSam Leffler 			return EINVAL;
24408a1b9b6aSSam Leffler 		/* XXX verify ciphers ok for unicast use? */
24418a1b9b6aSSam Leffler 		/* XXX disallow if running as it'll have no effect */
24428a1b9b6aSSam Leffler 		rsn->rsn_ucastcipherset = caps;
24438a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
24448a1b9b6aSSam Leffler 		break;
24458a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTCIPHER:
24468a1b9b6aSSam Leffler 		if ((rsn->rsn_ucastcipherset & cipher2cap(ireq->i_val)) == 0)
24478a1b9b6aSSam Leffler 			return EINVAL;
24488a1b9b6aSSam Leffler 		rsn->rsn_ucastcipher = ireq->i_val;
24498a1b9b6aSSam Leffler 		break;
24508a1b9b6aSSam Leffler 	case IEEE80211_IOC_UCASTKEYLEN:
24518a1b9b6aSSam Leffler 		if (!(0 < ireq->i_val && ireq->i_val < IEEE80211_KEYBUF_SIZE))
24528a1b9b6aSSam Leffler 			return EINVAL;
24538a1b9b6aSSam Leffler 		/* XXX no way to verify driver capability */
24548a1b9b6aSSam Leffler 		rsn->rsn_ucastkeylen = ireq->i_val;
24558a1b9b6aSSam Leffler 		break;
24568a1b9b6aSSam Leffler 	case IEEE80211_IOC_DRIVER_CAPS:
24578a1b9b6aSSam Leffler 		/* NB: for testing */
24588a1b9b6aSSam Leffler 		ic->ic_caps = (((u_int16_t) ireq->i_val) << 16) |
24598a1b9b6aSSam Leffler 			       ((u_int16_t) ireq->i_len);
24608a1b9b6aSSam Leffler 		break;
24618a1b9b6aSSam Leffler 	case IEEE80211_IOC_KEYMGTALGS:
24628a1b9b6aSSam Leffler 		/* XXX check */
24638a1b9b6aSSam Leffler 		rsn->rsn_keymgmtset = ireq->i_val;
24648a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
24658a1b9b6aSSam Leffler 		break;
24668a1b9b6aSSam Leffler 	case IEEE80211_IOC_RSNCAPS:
24678a1b9b6aSSam Leffler 		/* XXX check */
24688a1b9b6aSSam Leffler 		rsn->rsn_caps = ireq->i_val;
24698a1b9b6aSSam Leffler 		error = (ic->ic_flags & IEEE80211_F_WPA) ? ENETRESET : 0;
24708a1b9b6aSSam Leffler 		break;
24718a1b9b6aSSam Leffler 	case IEEE80211_IOC_BSSID:
24728a1b9b6aSSam Leffler 		if (ireq->i_len != sizeof(tmpbssid))
24738a1b9b6aSSam Leffler 			return EINVAL;
24748a1b9b6aSSam Leffler 		error = copyin(ireq->i_data, tmpbssid, ireq->i_len);
24758a1b9b6aSSam Leffler 		if (error)
24768a1b9b6aSSam Leffler 			break;
24778a1b9b6aSSam Leffler 		IEEE80211_ADDR_COPY(ic->ic_des_bssid, tmpbssid);
24788a1b9b6aSSam Leffler 		if (IEEE80211_ADDR_EQ(ic->ic_des_bssid, zerobssid))
24798a1b9b6aSSam Leffler 			ic->ic_flags &= ~IEEE80211_F_DESBSSID;
24808a1b9b6aSSam Leffler 		else
24818a1b9b6aSSam Leffler 			ic->ic_flags |= IEEE80211_F_DESBSSID;
24828a1b9b6aSSam Leffler 		error = ENETRESET;
24838a1b9b6aSSam Leffler 		break;
24848a1b9b6aSSam Leffler 	case IEEE80211_IOC_CHANLIST:
24858a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setchanlist(ic, ireq);
24868a1b9b6aSSam Leffler 		break;
24878a1b9b6aSSam Leffler 	case IEEE80211_IOC_SCAN_REQ:
24888a1b9b6aSSam Leffler 		if (ic->ic_opmode == IEEE80211_M_HOSTAP)	/* XXX ignore */
24898a1b9b6aSSam Leffler 			break;
24908a1b9b6aSSam Leffler 		error = ieee80211_setupscan(ic, ic->ic_chan_avail);
24918a1b9b6aSSam Leffler 		if (error == 0)		/* XXX background scan */
24928a1b9b6aSSam Leffler 			error = ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
24938a1b9b6aSSam Leffler 		break;
24948a1b9b6aSSam Leffler 	case IEEE80211_IOC_ADDMAC:
24958a1b9b6aSSam Leffler 	case IEEE80211_IOC_DELMAC:
24968a1b9b6aSSam Leffler 		error = ieee80211_ioctl_macmac(ic, ireq);
24978a1b9b6aSSam Leffler 		break;
24988a1b9b6aSSam Leffler 	case IEEE80211_IOC_MACCMD:
2499188757f5SSam Leffler 		error = ieee80211_ioctl_setmaccmd(ic, ireq);
25008a1b9b6aSSam Leffler 		break;
25018a1b9b6aSSam Leffler 	case IEEE80211_IOC_STA_TXPOW:
25028a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setstatxpow(ic, ireq);
25038a1b9b6aSSam Leffler 		break;
25048a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMIN:		/* WME: CWmin */
25058a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_CWMAX:		/* WME: CWmax */
25068a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_AIFS:		/* WME: AIFS */
25078a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_TXOPLIMIT:	/* WME: txops limit */
25088a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACM:		/* WME: ACM (bss only) */
25098a1b9b6aSSam Leffler 	case IEEE80211_IOC_WME_ACKPOLICY:	/* WME: ACK policy (bss only) */
25108a1b9b6aSSam Leffler 		error = ieee80211_ioctl_setwmeparam(ic, ireq);
25118a1b9b6aSSam Leffler 		break;
25128a1b9b6aSSam Leffler 	case IEEE80211_IOC_DTIM_PERIOD:
25138a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
25148a1b9b6aSSam Leffler 		    ic->ic_opmode != IEEE80211_M_IBSS)
25158a1b9b6aSSam Leffler 			return EINVAL;
25168a1b9b6aSSam Leffler 		if (IEEE80211_DTIM_MIN <= ireq->i_val &&
25178a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_DTIM_MAX) {
25188a1b9b6aSSam Leffler 			ic->ic_dtim_period = ireq->i_val;
25198a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
25208a1b9b6aSSam Leffler 		} else
25218a1b9b6aSSam Leffler 			error = EINVAL;
25228a1b9b6aSSam Leffler 		break;
25238a1b9b6aSSam Leffler 	case IEEE80211_IOC_BEACON_INTERVAL:
25248a1b9b6aSSam Leffler 		if (ic->ic_opmode != IEEE80211_M_HOSTAP &&
25258a1b9b6aSSam Leffler 		    ic->ic_opmode != IEEE80211_M_IBSS)
25268a1b9b6aSSam Leffler 			return EINVAL;
25278a1b9b6aSSam Leffler 		if (IEEE80211_BINTVAL_MIN <= ireq->i_val &&
25288a1b9b6aSSam Leffler 		    ireq->i_val <= IEEE80211_BINTVAL_MAX) {
2529d365f9c7SSam Leffler 			ic->ic_bintval = ireq->i_val;
25308a1b9b6aSSam Leffler 			error = ENETRESET;		/* requires restart */
25318a1b9b6aSSam Leffler 		} else
25328a1b9b6aSSam Leffler 			error = EINVAL;
25338a1b9b6aSSam Leffler 		break;
2534c4f040c3SSam Leffler 	case IEEE80211_IOC_PUREG:
2535c4f040c3SSam Leffler 		if (ireq->i_val)
2536c4f040c3SSam Leffler 			ic->ic_flags |= IEEE80211_F_PUREG;
2537c4f040c3SSam Leffler 		else
2538c4f040c3SSam Leffler 			ic->ic_flags &= ~IEEE80211_F_PUREG;
2539c4f040c3SSam Leffler 		/* NB: reset only if we're operating on an 11g channel */
2540c4f040c3SSam Leffler 		if (ic->ic_curmode == IEEE80211_MODE_11G)
2541c4f040c3SSam Leffler 			error = ENETRESET;
2542c4f040c3SSam Leffler 		break;
254364353cb0SSam Leffler 	case IEEE80211_IOC_MCAST_RATE:
254464353cb0SSam Leffler 		ic->ic_mcast_rate = ireq->i_val & IEEE80211_RATE_VAL;
254564353cb0SSam Leffler 		break;
254670231e3dSSam Leffler 	case IEEE80211_IOC_FRAGTHRESHOLD:
254770231e3dSSam Leffler 		if ((ic->ic_caps & IEEE80211_C_TXFRAG) == 0 &&
254870231e3dSSam Leffler 		    ireq->i_val != IEEE80211_FRAG_MAX)
254970231e3dSSam Leffler 			return EINVAL;
255070231e3dSSam Leffler 		if (!(IEEE80211_FRAG_MIN <= ireq->i_val &&
255170231e3dSSam Leffler 		      ireq->i_val <= IEEE80211_FRAG_MAX))
255270231e3dSSam Leffler 			return EINVAL;
255370231e3dSSam Leffler 		ic->ic_fragthreshold = ireq->i_val;
255470231e3dSSam Leffler 		error = IS_UP(ic) ? ic->ic_reset(ic->ic_ifp) : 0;
255570231e3dSSam Leffler 		break;
2556c27e4e31SSam Leffler 	case IEEE80211_IOC_BURST:
2557c27e4e31SSam Leffler 		if (ireq->i_val) {
2558c27e4e31SSam Leffler 			if ((ic->ic_caps & IEEE80211_C_BURST) == 0)
2559c27e4e31SSam Leffler 				return EINVAL;
2560c27e4e31SSam Leffler 			ic->ic_flags |= IEEE80211_F_BURST;
2561c27e4e31SSam Leffler 		} else
2562c27e4e31SSam Leffler 			ic->ic_flags &= ~IEEE80211_F_BURST;
2563c27e4e31SSam Leffler 		error = ENETRESET;		/* XXX maybe not for station? */
2564c27e4e31SSam Leffler 		break;
25651a1e1d21SSam Leffler 	default:
25661a1e1d21SSam Leffler 		error = EINVAL;
25671a1e1d21SSam Leffler 		break;
25681a1e1d21SSam Leffler 	}
25698a1b9b6aSSam Leffler 	if (error == ENETRESET && !IS_UP_AUTO(ic))
25708a1b9b6aSSam Leffler 		error = 0;
25718a1b9b6aSSam Leffler 	return error;
25728a1b9b6aSSam Leffler }
25738a1b9b6aSSam Leffler 
25748a1b9b6aSSam Leffler int
25758a1b9b6aSSam Leffler ieee80211_ioctl(struct ieee80211com *ic, u_long cmd, caddr_t data)
25768a1b9b6aSSam Leffler {
25778a1b9b6aSSam Leffler 	struct ifnet *ifp = ic->ic_ifp;
25788a1b9b6aSSam Leffler 	int error = 0;
25798a1b9b6aSSam Leffler 	struct ifreq *ifr;
25808a1b9b6aSSam Leffler 	struct ifaddr *ifa;			/* XXX */
25818a1b9b6aSSam Leffler 
25828a1b9b6aSSam Leffler 	switch (cmd) {
25838a1b9b6aSSam Leffler 	case SIOCSIFMEDIA:
25848a1b9b6aSSam Leffler 	case SIOCGIFMEDIA:
25858a1b9b6aSSam Leffler 		error = ifmedia_ioctl(ifp, (struct ifreq *) data,
25868a1b9b6aSSam Leffler 				&ic->ic_media, cmd);
25878a1b9b6aSSam Leffler 		break;
25888a1b9b6aSSam Leffler 	case SIOCG80211:
25898a1b9b6aSSam Leffler 		error = ieee80211_ioctl_get80211(ic, cmd,
25908a1b9b6aSSam Leffler 				(struct ieee80211req *) data);
25918a1b9b6aSSam Leffler 		break;
25928a1b9b6aSSam Leffler 	case SIOCS80211:
25938a1b9b6aSSam Leffler 		error = suser(curthread);
25948a1b9b6aSSam Leffler 		if (error == 0)
25958a1b9b6aSSam Leffler 			error = ieee80211_ioctl_set80211(ic, cmd,
25968a1b9b6aSSam Leffler 					(struct ieee80211req *) data);
25971a1e1d21SSam Leffler 		break;
25981a1e1d21SSam Leffler 	case SIOCGIFGENERIC:
25998a1b9b6aSSam Leffler 		error = ieee80211_cfgget(ic, cmd, data);
26001a1e1d21SSam Leffler 		break;
26011a1e1d21SSam Leffler 	case SIOCSIFGENERIC:
26021a1e1d21SSam Leffler 		error = suser(curthread);
26031a1e1d21SSam Leffler 		if (error)
26041a1e1d21SSam Leffler 			break;
26058a1b9b6aSSam Leffler 		error = ieee80211_cfgset(ic, cmd, data);
26061a1e1d21SSam Leffler 		break;
26071be50176SSam Leffler 	case SIOCG80211STATS:
26081be50176SSam Leffler 		ifr = (struct ifreq *)data;
26091be50176SSam Leffler 		copyout(&ic->ic_stats, ifr->ifr_data, sizeof (ic->ic_stats));
26101be50176SSam Leffler 		break;
26116f161f03SSam Leffler 	case SIOCSIFMTU:
26126f161f03SSam Leffler 		ifr = (struct ifreq *)data;
26136f161f03SSam Leffler 		if (!(IEEE80211_MTU_MIN <= ifr->ifr_mtu &&
26146f161f03SSam Leffler 		    ifr->ifr_mtu <= IEEE80211_MTU_MAX))
26156f161f03SSam Leffler 			error = EINVAL;
26166f161f03SSam Leffler 		else
26176f161f03SSam Leffler 			ifp->if_mtu = ifr->ifr_mtu;
26186f161f03SSam Leffler 		break;
2619b2e95691SSam Leffler 	case SIOCSIFADDR:
2620b2e95691SSam Leffler 		/*
2621b2e95691SSam Leffler 		 * XXX Handle this directly so we can supress if_init calls.
2622b2e95691SSam Leffler 		 * XXX This should be done in ether_ioctl but for the moment
2623b2e95691SSam Leffler 		 * XXX there are too many other parts of the system that
2624b2e95691SSam Leffler 		 * XXX set IFF_UP and so supress if_init being called when
2625b2e95691SSam Leffler 		 * XXX it should be.
2626b2e95691SSam Leffler 		 */
2627b2e95691SSam Leffler 		ifa = (struct ifaddr *) data;
2628b2e95691SSam Leffler 		switch (ifa->ifa_addr->sa_family) {
2629b2e95691SSam Leffler #ifdef INET
2630b2e95691SSam Leffler 		case AF_INET:
2631b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
2632b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
2633b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
2634b2e95691SSam Leffler 			}
2635b2e95691SSam Leffler 			arp_ifinit(ifp, ifa);
2636b2e95691SSam Leffler 			break;
2637b2e95691SSam Leffler #endif
2638b2e95691SSam Leffler #ifdef IPX
2639b2e95691SSam Leffler 		/*
2640b2e95691SSam Leffler 		 * XXX - This code is probably wrong,
2641b2e95691SSam Leffler 		 *	 but has been copied many times.
2642b2e95691SSam Leffler 		 */
2643b2e95691SSam Leffler 		case AF_IPX: {
2644b2e95691SSam Leffler 			struct ipx_addr *ina = &(IA_SIPX(ifa)->sipx_addr);
2645b2e95691SSam Leffler 
2646b2e95691SSam Leffler 			if (ipx_nullhost(*ina))
2647fc74a9f9SBrooks Davis 				ina->x_host = *(union ipx_host *)
26484a0d6638SRuslan Ermilov 				    IF_LLADDR(ifp);
2649b2e95691SSam Leffler 			else
2650b2e95691SSam Leffler 				bcopy((caddr_t) ina->x_host.c_host,
26514a0d6638SRuslan Ermilov 				      (caddr_t) IF_LLADDR(ifp),
2652fc74a9f9SBrooks Davis 				      ETHER_ADDR_LEN);
2653b2e95691SSam Leffler 			/* fall thru... */
2654b2e95691SSam Leffler 		}
2655b2e95691SSam Leffler #endif
2656b2e95691SSam Leffler 		default:
2657b2e95691SSam Leffler 			if ((ifp->if_flags & IFF_UP) == 0) {
2658b2e95691SSam Leffler 				ifp->if_flags |= IFF_UP;
2659b2e95691SSam Leffler 				ifp->if_init(ifp->if_softc);
2660b2e95691SSam Leffler 			}
2661b2e95691SSam Leffler 			break;
2662b2e95691SSam Leffler 		}
2663b2e95691SSam Leffler 		break;
26641a1e1d21SSam Leffler 	default:
26651a1e1d21SSam Leffler 		error = ether_ioctl(ifp, cmd, data);
26661a1e1d21SSam Leffler 		break;
26671a1e1d21SSam Leffler 	}
26681a1e1d21SSam Leffler 	return error;
26691a1e1d21SSam Leffler }
2670