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