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