[MAC80211]: Add SIOCGIWTXPOWER routine
[powerpc.git] / net / mac80211 / ieee80211_ioctl.c
1 /*
2  * Copyright 2002-2005, Instant802 Networks, Inc.
3  * Copyright 2005-2006, Devicescape Software, Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/etherdevice.h>
17 #include <linux/if_arp.h>
18 #include <linux/wireless.h>
19 #include <net/iw_handler.h>
20 #include <asm/uaccess.h>
21
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "hostapd_ioctl.h"
25 #include "ieee80211_rate.h"
26 #include "wpa.h"
27 #include "aes_ccm.h"
28 #include "debugfs_key.h"
29
30 static void ieee80211_set_hw_encryption(struct net_device *dev,
31                                         struct sta_info *sta, u8 addr[ETH_ALEN],
32                                         struct ieee80211_key *key)
33 {
34         struct ieee80211_key_conf *keyconf = NULL;
35         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
36
37         /* default to sw encryption; this will be cleared by low-level
38          * driver if the hw supports requested encryption */
39         if (key)
40                 key->force_sw_encrypt = 1;
41
42         if (key && local->ops->set_key &&
43             (keyconf = ieee80211_key_data2conf(local, key))) {
44                 if (local->ops->set_key(local_to_hw(local), SET_KEY, addr,
45                                        keyconf, sta ? sta->aid : 0)) {
46                         key->force_sw_encrypt = 1;
47                         key->hw_key_idx = HW_KEY_IDX_INVALID;
48                 } else {
49                         key->force_sw_encrypt =
50                                 !!(keyconf->flags & IEEE80211_KEY_FORCE_SW_ENCRYPT);
51                         key->hw_key_idx =
52                                 keyconf->hw_key_idx;
53
54                 }
55         }
56         kfree(keyconf);
57 }
58
59
60 static int ieee80211_set_encryption(struct net_device *dev, u8 *sta_addr,
61                                     int idx, int alg, int set_tx_key,
62                                     const u8 *_key, size_t key_len)
63 {
64         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
65         int ret = 0;
66         struct sta_info *sta;
67         struct ieee80211_key *key, *old_key;
68         int try_hwaccel = 1;
69         struct ieee80211_key_conf *keyconf;
70         struct ieee80211_sub_if_data *sdata;
71
72         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
73
74         if (is_broadcast_ether_addr(sta_addr)) {
75                 sta = NULL;
76                 if (idx >= NUM_DEFAULT_KEYS) {
77                         printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
78                                dev->name, idx);
79                         return -EINVAL;
80                 }
81                 key = sdata->keys[idx];
82
83                 /* TODO: consider adding hwaccel support for these; at least
84                  * Atheros key cache should be able to handle this since AP is
85                  * only transmitting frames with default keys. */
86                 /* FIX: hw key cache can be used when only one virtual
87                  * STA is associated with each AP. If more than one STA
88                  * is associated to the same AP, software encryption
89                  * must be used. This should be done automatically
90                  * based on configured station devices. For the time
91                  * being, this can be only set at compile time. */
92         } else {
93                 set_tx_key = 0;
94                 if (idx != 0) {
95                         printk(KERN_DEBUG "%s: set_encrypt - non-zero idx for "
96                                "individual key\n", dev->name);
97                         return -EINVAL;
98                 }
99
100                 sta = sta_info_get(local, sta_addr);
101                 if (!sta) {
102 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
103                         printk(KERN_DEBUG "%s: set_encrypt - unknown addr "
104                                MAC_FMT "\n",
105                                dev->name, MAC_ARG(sta_addr));
106 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
107
108                         return -ENOENT;
109                 }
110
111                 key = sta->key;
112         }
113
114         /* FIX:
115          * Cannot configure default hwaccel keys with WEP algorithm, if
116          * any of the virtual interfaces is using static WEP
117          * configuration because hwaccel would otherwise try to decrypt
118          * these frames.
119          *
120          * For now, just disable WEP hwaccel for broadcast when there is
121          * possibility of conflict with default keys. This can maybe later be
122          * optimized by using non-default keys (at least with Atheros ar521x).
123          */
124         if (!sta && alg == ALG_WEP && !local->default_wep_only &&
125             sdata->type != IEEE80211_IF_TYPE_IBSS &&
126             sdata->type != IEEE80211_IF_TYPE_AP) {
127                 try_hwaccel = 0;
128         }
129
130         if (local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP) {
131                 /* Software encryption cannot be used with devices that hide
132                  * encryption from the host system, so always try to use
133                  * hardware acceleration with such devices. */
134                 try_hwaccel = 1;
135         }
136
137         if ((local->hw.flags & IEEE80211_HW_NO_TKIP_WMM_HWACCEL) &&
138             alg == ALG_TKIP) {
139                 if (sta && (sta->flags & WLAN_STA_WME)) {
140                 /* Hardware does not support hwaccel with TKIP when using WMM.
141                  */
142                         try_hwaccel = 0;
143                 }
144                 else if (sdata->type == IEEE80211_IF_TYPE_STA) {
145                         sta = sta_info_get(local, sdata->u.sta.bssid);
146                         if (sta) {
147                                 if (sta->flags & WLAN_STA_WME) {
148                                         try_hwaccel = 0;
149                                 }
150                                 sta_info_put(sta);
151                                 sta = NULL;
152                         }
153                 }
154         }
155
156         if (alg == ALG_NONE) {
157                 keyconf = NULL;
158                 if (try_hwaccel && key &&
159                     key->hw_key_idx != HW_KEY_IDX_INVALID &&
160                     local->ops->set_key &&
161                     (keyconf = ieee80211_key_data2conf(local, key)) != NULL &&
162                     local->ops->set_key(local_to_hw(local), DISABLE_KEY,
163                                        sta_addr, keyconf, sta ? sta->aid : 0)) {
164                         printk(KERN_DEBUG "%s: set_encrypt - low-level disable"
165                                " failed\n", dev->name);
166                         ret = -EINVAL;
167                 }
168                 kfree(keyconf);
169
170                 if (set_tx_key || sdata->default_key == key) {
171                         ieee80211_debugfs_key_remove_default(sdata);
172                         sdata->default_key = NULL;
173                 }
174                 ieee80211_debugfs_key_remove(key);
175                 if (sta)
176                         sta->key = NULL;
177                 else
178                         sdata->keys[idx] = NULL;
179                 ieee80211_key_free(key);
180                 key = NULL;
181         } else {
182                 old_key = key;
183                 key = ieee80211_key_alloc(sta ? NULL : sdata, idx, key_len,
184                                           GFP_KERNEL);
185                 if (!key) {
186                         ret = -ENOMEM;
187                         goto err_out;
188                 }
189
190                 /* default to sw encryption; low-level driver sets these if the
191                  * requested encryption is supported */
192                 key->hw_key_idx = HW_KEY_IDX_INVALID;
193                 key->force_sw_encrypt = 1;
194
195                 key->alg = alg;
196                 key->keyidx = idx;
197                 key->keylen = key_len;
198                 memcpy(key->key, _key, key_len);
199                 if (set_tx_key)
200                         key->default_tx_key = 1;
201
202                 if (alg == ALG_CCMP) {
203                         /* Initialize AES key state here as an optimization
204                          * so that it does not need to be initialized for every
205                          * packet. */
206                         key->u.ccmp.tfm = ieee80211_aes_key_setup_encrypt(
207                                 key->key);
208                         if (!key->u.ccmp.tfm) {
209                                 ret = -ENOMEM;
210                                 goto err_free;
211                         }
212                 }
213
214                 if (set_tx_key || sdata->default_key == old_key) {
215                         ieee80211_debugfs_key_remove_default(sdata);
216                         sdata->default_key = NULL;
217                 }
218                 ieee80211_debugfs_key_remove(old_key);
219                 if (sta)
220                         sta->key = key;
221                 else
222                         sdata->keys[idx] = key;
223                 ieee80211_key_free(old_key);
224                 ieee80211_debugfs_key_add(local, key);
225                 if (sta)
226                         ieee80211_debugfs_key_sta_link(key, sta);
227
228                 if (try_hwaccel &&
229                     (alg == ALG_WEP || alg == ALG_TKIP || alg == ALG_CCMP))
230                         ieee80211_set_hw_encryption(dev, sta, sta_addr, key);
231         }
232
233         if (set_tx_key || (!sta && !sdata->default_key && key)) {
234                 sdata->default_key = key;
235                 if (key)
236                         ieee80211_debugfs_key_add_default(sdata);
237
238                 if (local->ops->set_key_idx &&
239                     local->ops->set_key_idx(local_to_hw(local), idx))
240                         printk(KERN_DEBUG "%s: failed to set TX key idx for "
241                                "low-level driver\n", dev->name);
242         }
243
244         if (sta)
245                 sta_info_put(sta);
246
247         return 0;
248
249 err_free:
250         ieee80211_key_free(key);
251 err_out:
252         if (sta)
253                 sta_info_put(sta);
254         return ret;
255 }
256
257 static int ieee80211_ioctl_siwgenie(struct net_device *dev,
258                                     struct iw_request_info *info,
259                                     struct iw_point *data, char *extra)
260 {
261         struct ieee80211_sub_if_data *sdata;
262         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
263
264         if (local->user_space_mlme)
265                 return -EOPNOTSUPP;
266
267         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
268         if (sdata->type == IEEE80211_IF_TYPE_STA ||
269             sdata->type == IEEE80211_IF_TYPE_IBSS) {
270                 int ret = ieee80211_sta_set_extra_ie(dev, extra, data->length);
271                 if (ret)
272                         return ret;
273                 sdata->u.sta.auto_bssid_sel = 0;
274                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
275                 return 0;
276         }
277
278         if (sdata->type == IEEE80211_IF_TYPE_AP) {
279                 kfree(sdata->u.ap.generic_elem);
280                 sdata->u.ap.generic_elem = kmalloc(data->length, GFP_KERNEL);
281                 if (!sdata->u.ap.generic_elem)
282                         return -ENOMEM;
283                 memcpy(sdata->u.ap.generic_elem, extra, data->length);
284                 sdata->u.ap.generic_elem_len = data->length;
285                 return ieee80211_if_config(dev);
286         }
287         return -EOPNOTSUPP;
288 }
289
290 static int ieee80211_ioctl_set_radio_enabled(struct net_device *dev,
291                                              int val)
292 {
293         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
294         struct ieee80211_conf *conf = &local->hw.conf;
295
296         conf->radio_enabled = val;
297         return ieee80211_hw_config(wdev_priv(dev->ieee80211_ptr));
298 }
299
300 static int ieee80211_ioctl_giwname(struct net_device *dev,
301                                    struct iw_request_info *info,
302                                    char *name, char *extra)
303 {
304         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
305
306         switch (local->hw.conf.phymode) {
307         case MODE_IEEE80211A:
308                 strcpy(name, "IEEE 802.11a");
309                 break;
310         case MODE_IEEE80211B:
311                 strcpy(name, "IEEE 802.11b");
312                 break;
313         case MODE_IEEE80211G:
314                 strcpy(name, "IEEE 802.11g");
315                 break;
316         case MODE_ATHEROS_TURBO:
317                 strcpy(name, "5GHz Turbo");
318                 break;
319         default:
320                 strcpy(name, "IEEE 802.11");
321                 break;
322         }
323
324         return 0;
325 }
326
327
328 static int ieee80211_ioctl_giwrange(struct net_device *dev,
329                                  struct iw_request_info *info,
330                                  struct iw_point *data, char *extra)
331 {
332         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
333         struct iw_range *range = (struct iw_range *) extra;
334         struct ieee80211_hw_mode *mode = NULL;
335         int c = 0;
336
337         data->length = sizeof(struct iw_range);
338         memset(range, 0, sizeof(struct iw_range));
339
340         range->we_version_compiled = WIRELESS_EXT;
341         range->we_version_source = 21;
342         range->retry_capa = IW_RETRY_LIMIT;
343         range->retry_flags = IW_RETRY_LIMIT;
344         range->min_retry = 0;
345         range->max_retry = 255;
346         range->min_rts = 0;
347         range->max_rts = 2347;
348         range->min_frag = 256;
349         range->max_frag = 2346;
350
351         range->encoding_size[0] = 5;
352         range->encoding_size[1] = 13;
353         range->num_encoding_sizes = 2;
354         range->max_encoding_tokens = NUM_DEFAULT_KEYS;
355
356         range->max_qual.qual = local->hw.max_signal;
357         range->max_qual.level = local->hw.max_rssi;
358         range->max_qual.noise = local->hw.max_noise;
359         range->max_qual.updated = local->wstats_flags;
360
361         range->avg_qual.qual = local->hw.max_signal/2;
362         range->avg_qual.level = 0;
363         range->avg_qual.noise = 0;
364         range->avg_qual.updated = local->wstats_flags;
365
366         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
367                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
368
369         list_for_each_entry(mode, &local->modes_list, list) {
370                 int i = 0;
371
372                 if (!(local->enabled_modes & (1 << mode->mode)) ||
373                     (local->hw_modes & local->enabled_modes &
374                      (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
375                         continue;
376
377                 while (i < mode->num_channels && c < IW_MAX_FREQUENCIES) {
378                         struct ieee80211_channel *chan = &mode->channels[i];
379
380                         if (chan->flag & IEEE80211_CHAN_W_SCAN) {
381                                 range->freq[c].i = chan->chan;
382                                 range->freq[c].m = chan->freq * 100000;
383                                 range->freq[c].e = 1;
384                                 c++;
385                         }
386                         i++;
387                 }
388         }
389         range->num_channels = c;
390         range->num_frequency = c;
391
392         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
393         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
394         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
395         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
396
397         return 0;
398 }
399
400
401 static int ieee80211_ioctl_siwmode(struct net_device *dev,
402                                    struct iw_request_info *info,
403                                    __u32 *mode, char *extra)
404 {
405         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
406         int type;
407
408         if (sdata->type == IEEE80211_IF_TYPE_VLAN)
409                 return -EOPNOTSUPP;
410
411         switch (*mode) {
412         case IW_MODE_INFRA:
413                 type = IEEE80211_IF_TYPE_STA;
414                 break;
415         case IW_MODE_ADHOC:
416                 type = IEEE80211_IF_TYPE_IBSS;
417                 break;
418         case IW_MODE_MONITOR:
419                 type = IEEE80211_IF_TYPE_MNTR;
420                 break;
421         default:
422                 return -EINVAL;
423         }
424
425         if (type == sdata->type)
426                 return 0;
427         if (netif_running(dev))
428                 return -EBUSY;
429
430         ieee80211_if_reinit(dev);
431         ieee80211_if_set_type(dev, type);
432
433         return 0;
434 }
435
436
437 static int ieee80211_ioctl_giwmode(struct net_device *dev,
438                                    struct iw_request_info *info,
439                                    __u32 *mode, char *extra)
440 {
441         struct ieee80211_sub_if_data *sdata;
442
443         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
444         switch (sdata->type) {
445         case IEEE80211_IF_TYPE_AP:
446                 *mode = IW_MODE_MASTER;
447                 break;
448         case IEEE80211_IF_TYPE_STA:
449                 *mode = IW_MODE_INFRA;
450                 break;
451         case IEEE80211_IF_TYPE_IBSS:
452                 *mode = IW_MODE_ADHOC;
453                 break;
454         case IEEE80211_IF_TYPE_MNTR:
455                 *mode = IW_MODE_MONITOR;
456                 break;
457         case IEEE80211_IF_TYPE_WDS:
458                 *mode = IW_MODE_REPEAT;
459                 break;
460         case IEEE80211_IF_TYPE_VLAN:
461                 *mode = IW_MODE_SECOND;         /* FIXME */
462                 break;
463         default:
464                 *mode = IW_MODE_AUTO;
465                 break;
466         }
467         return 0;
468 }
469
470 int ieee80211_set_channel(struct ieee80211_local *local, int channel, int freq)
471 {
472         struct ieee80211_hw_mode *mode;
473         int c, set = 0;
474         int ret = -EINVAL;
475
476         list_for_each_entry(mode, &local->modes_list, list) {
477                 if (!(local->enabled_modes & (1 << mode->mode)))
478                         continue;
479                 for (c = 0; c < mode->num_channels; c++) {
480                         struct ieee80211_channel *chan = &mode->channels[c];
481                         if (chan->flag & IEEE80211_CHAN_W_SCAN &&
482                             ((chan->chan == channel) || (chan->freq == freq))) {
483                                 /* Use next_mode as the mode preference to
484                                  * resolve non-unique channel numbers. */
485                                 if (set && mode->mode != local->next_mode)
486                                         continue;
487
488                                 local->oper_channel = chan;
489                                 local->oper_hw_mode = mode;
490                                 set++;
491                         }
492                 }
493         }
494
495         if (set) {
496                 if (local->sta_scanning)
497                         ret = 0;
498                 else
499                         ret = ieee80211_hw_config(local);
500
501                 rate_control_clear(local);
502         }
503
504         return ret;
505 }
506
507 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
508                                    struct iw_request_info *info,
509                                    struct iw_freq *freq, char *extra)
510 {
511         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
512         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
513
514         if (sdata->type == IEEE80211_IF_TYPE_STA)
515                 sdata->u.sta.auto_channel_sel = 0;
516
517         /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
518         if (freq->e == 0) {
519                 if (freq->m < 0) {
520                         if (sdata->type == IEEE80211_IF_TYPE_STA)
521                                 sdata->u.sta.auto_channel_sel = 1;
522                         return 0;
523                 } else
524                         return ieee80211_set_channel(local, freq->m, -1);
525         } else {
526                 int i, div = 1000000;
527                 for (i = 0; i < freq->e; i++)
528                         div /= 10;
529                 if (div > 0)
530                         return ieee80211_set_channel(local, -1, freq->m / div);
531                 else
532                         return -EINVAL;
533         }
534 }
535
536
537 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
538                                    struct iw_request_info *info,
539                                    struct iw_freq *freq, char *extra)
540 {
541         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
542
543         /* TODO: in station mode (Managed/Ad-hoc) might need to poll low-level
544          * driver for the current channel with firmware-based management */
545
546         freq->m = local->hw.conf.freq;
547         freq->e = 6;
548
549         return 0;
550 }
551
552
553 static int ieee80211_ioctl_siwessid(struct net_device *dev,
554                                     struct iw_request_info *info,
555                                     struct iw_point *data, char *ssid)
556 {
557         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
558         struct ieee80211_sub_if_data *sdata;
559         size_t len = data->length;
560
561         /* iwconfig uses nul termination in SSID.. */
562         if (len > 0 && ssid[len - 1] == '\0')
563                 len--;
564
565         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
566         if (sdata->type == IEEE80211_IF_TYPE_STA ||
567             sdata->type == IEEE80211_IF_TYPE_IBSS) {
568                 int ret;
569                 if (local->user_space_mlme) {
570                         if (len > IEEE80211_MAX_SSID_LEN)
571                                 return -EINVAL;
572                         memcpy(sdata->u.sta.ssid, ssid, len);
573                         sdata->u.sta.ssid_len = len;
574                         return 0;
575                 }
576                 sdata->u.sta.auto_ssid_sel = !data->flags;
577                 ret = ieee80211_sta_set_ssid(dev, ssid, len);
578                 if (ret)
579                         return ret;
580                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
581                 return 0;
582         }
583
584         if (sdata->type == IEEE80211_IF_TYPE_AP) {
585                 memcpy(sdata->u.ap.ssid, ssid, len);
586                 memset(sdata->u.ap.ssid + len, 0,
587                        IEEE80211_MAX_SSID_LEN - len);
588                 sdata->u.ap.ssid_len = len;
589                 return ieee80211_if_config(dev);
590         }
591         return -EOPNOTSUPP;
592 }
593
594
595 static int ieee80211_ioctl_giwessid(struct net_device *dev,
596                                     struct iw_request_info *info,
597                                     struct iw_point *data, char *ssid)
598 {
599         size_t len;
600
601         struct ieee80211_sub_if_data *sdata;
602         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
603         if (sdata->type == IEEE80211_IF_TYPE_STA ||
604             sdata->type == IEEE80211_IF_TYPE_IBSS) {
605                 int res = ieee80211_sta_get_ssid(dev, ssid, &len);
606                 if (res == 0) {
607                         data->length = len;
608                         data->flags = 1;
609                 } else
610                         data->flags = 0;
611                 return res;
612         }
613
614         if (sdata->type == IEEE80211_IF_TYPE_AP) {
615                 len = sdata->u.ap.ssid_len;
616                 if (len > IW_ESSID_MAX_SIZE)
617                         len = IW_ESSID_MAX_SIZE;
618                 memcpy(ssid, sdata->u.ap.ssid, len);
619                 data->length = len;
620                 data->flags = 1;
621                 return 0;
622         }
623         return -EOPNOTSUPP;
624 }
625
626
627 static int ieee80211_ioctl_siwap(struct net_device *dev,
628                                  struct iw_request_info *info,
629                                  struct sockaddr *ap_addr, char *extra)
630 {
631         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
632         struct ieee80211_sub_if_data *sdata;
633
634         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
635         if (sdata->type == IEEE80211_IF_TYPE_STA ||
636             sdata->type == IEEE80211_IF_TYPE_IBSS) {
637                 int ret;
638                 if (local->user_space_mlme) {
639                         memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
640                                ETH_ALEN);
641                         return 0;
642                 }
643                 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data)) {
644                         sdata->u.sta.auto_bssid_sel = 1;
645                         sdata->u.sta.auto_channel_sel = 1;
646                 } else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
647                         sdata->u.sta.auto_bssid_sel = 1;
648                 else
649                         sdata->u.sta.auto_bssid_sel = 0;
650                 ret = ieee80211_sta_set_bssid(dev, (u8 *) &ap_addr->sa_data);
651                 if (ret)
652                         return ret;
653                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
654                 return 0;
655         } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
656                 if (memcmp(sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
657                            ETH_ALEN) == 0)
658                         return 0;
659                 return ieee80211_if_update_wds(dev, (u8 *) &ap_addr->sa_data);
660         }
661
662         return -EOPNOTSUPP;
663 }
664
665
666 static int ieee80211_ioctl_giwap(struct net_device *dev,
667                                  struct iw_request_info *info,
668                                  struct sockaddr *ap_addr, char *extra)
669 {
670         struct ieee80211_sub_if_data *sdata;
671
672         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
673         if (sdata->type == IEEE80211_IF_TYPE_STA ||
674             sdata->type == IEEE80211_IF_TYPE_IBSS) {
675                 ap_addr->sa_family = ARPHRD_ETHER;
676                 memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
677                 return 0;
678         } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
679                 ap_addr->sa_family = ARPHRD_ETHER;
680                 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
681                 return 0;
682         }
683
684         return -EOPNOTSUPP;
685 }
686
687
688 static int ieee80211_ioctl_siwscan(struct net_device *dev,
689                                    struct iw_request_info *info,
690                                    struct iw_point *data, char *extra)
691 {
692         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
693         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
694         u8 *ssid = NULL;
695         size_t ssid_len = 0;
696
697         if (!netif_running(dev))
698                 return -ENETDOWN;
699
700         switch (sdata->type) {
701         case IEEE80211_IF_TYPE_STA:
702         case IEEE80211_IF_TYPE_IBSS:
703                 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID) {
704                         ssid = sdata->u.sta.ssid;
705                         ssid_len = sdata->u.sta.ssid_len;
706                 }
707                 break;
708         case IEEE80211_IF_TYPE_AP:
709                 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID) {
710                         ssid = sdata->u.ap.ssid;
711                         ssid_len = sdata->u.ap.ssid_len;
712                 }
713                 break;
714         default:
715                 return -EOPNOTSUPP;
716         }
717
718         return ieee80211_sta_req_scan(dev, ssid, ssid_len);
719 }
720
721
722 static int ieee80211_ioctl_giwscan(struct net_device *dev,
723                                    struct iw_request_info *info,
724                                    struct iw_point *data, char *extra)
725 {
726         int res;
727         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
728         if (local->sta_scanning)
729                 return -EAGAIN;
730         res = ieee80211_sta_scan_results(dev, extra, data->length);
731         if (res >= 0) {
732                 data->length = res;
733                 return 0;
734         }
735         data->length = 0;
736         return res;
737 }
738
739
740 static int ieee80211_ioctl_siwrate(struct net_device *dev,
741                                   struct iw_request_info *info,
742                                   struct iw_param *rate, char *extra)
743 {
744         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
745         struct ieee80211_hw_mode *mode;
746         int i;
747         u32 target_rate = rate->value / 100000;
748         struct ieee80211_sub_if_data *sdata;
749
750         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
751         if (!sdata->bss)
752                 return -ENODEV;
753         mode = local->oper_hw_mode;
754         /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
755          * target_rate = X, rate->fixed = 1 means only rate X
756          * target_rate = X, rate->fixed = 0 means all rates <= X */
757         sdata->bss->max_ratectrl_rateidx = -1;
758         sdata->bss->force_unicast_rateidx = -1;
759         if (rate->value < 0)
760                 return 0;
761         for (i=0; i< mode->num_rates; i++) {
762                 struct ieee80211_rate *rates = &mode->rates[i];
763                 int this_rate = rates->rate;
764
765                 if (mode->mode == MODE_ATHEROS_TURBO ||
766                     mode->mode == MODE_ATHEROS_TURBOG)
767                         this_rate *= 2;
768                 if (target_rate == this_rate) {
769                         sdata->bss->max_ratectrl_rateidx = i;
770                         if (rate->fixed)
771                                 sdata->bss->force_unicast_rateidx = i;
772                         break;
773                 }
774         }
775         return 0;
776 }
777
778 static int ieee80211_ioctl_giwrate(struct net_device *dev,
779                                   struct iw_request_info *info,
780                                   struct iw_param *rate, char *extra)
781 {
782         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
783         struct sta_info *sta;
784         struct ieee80211_sub_if_data *sdata;
785
786         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
787         if (sdata->type == IEEE80211_IF_TYPE_STA)
788                 sta = sta_info_get(local, sdata->u.sta.bssid);
789         else
790                 return -EOPNOTSUPP;
791         if (!sta)
792                 return -ENODEV;
793         if (sta->txrate < local->oper_hw_mode->num_rates)
794                 rate->value = local->oper_hw_mode->rates[sta->txrate].rate * 100000;
795         else
796                 rate->value = 0;
797         sta_info_put(sta);
798         return 0;
799 }
800
801 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
802                                    struct iw_request_info *info,
803                                    union iwreq_data *data, char *extra)
804 {
805         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
806
807         data->txpower.fixed = 1;
808         data->txpower.disabled = !(local->hw.conf.radio_enabled);
809         data->txpower.value = local->hw.conf.power_level;
810         data->txpower.flags = IW_TXPOW_DBM;
811
812         return 0;
813 }
814
815 static int ieee80211_ioctl_siwrts(struct net_device *dev,
816                                   struct iw_request_info *info,
817                                   struct iw_param *rts, char *extra)
818 {
819         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
820
821         if (rts->disabled)
822                 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
823         else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
824                 return -EINVAL;
825         else
826                 local->rts_threshold = rts->value;
827
828         /* If the wlan card performs RTS/CTS in hardware/firmware,
829          * configure it here */
830
831         if (local->ops->set_rts_threshold)
832                 local->ops->set_rts_threshold(local_to_hw(local),
833                                              local->rts_threshold);
834
835         return 0;
836 }
837
838 static int ieee80211_ioctl_giwrts(struct net_device *dev,
839                                   struct iw_request_info *info,
840                                   struct iw_param *rts, char *extra)
841 {
842         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
843
844         rts->value = local->rts_threshold;
845         rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
846         rts->fixed = 1;
847
848         return 0;
849 }
850
851
852 static int ieee80211_ioctl_siwfrag(struct net_device *dev,
853                                    struct iw_request_info *info,
854                                    struct iw_param *frag, char *extra)
855 {
856         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
857
858         if (frag->disabled)
859                 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
860         else if (frag->value < 256 ||
861                  frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
862                 return -EINVAL;
863         else {
864                 /* Fragment length must be even, so strip LSB. */
865                 local->fragmentation_threshold = frag->value & ~0x1;
866         }
867
868         /* If the wlan card performs fragmentation in hardware/firmware,
869          * configure it here */
870
871         if (local->ops->set_frag_threshold)
872                 local->ops->set_frag_threshold(
873                         local_to_hw(local),
874                         local->fragmentation_threshold);
875
876         return 0;
877 }
878
879 static int ieee80211_ioctl_giwfrag(struct net_device *dev,
880                                    struct iw_request_info *info,
881                                    struct iw_param *frag, char *extra)
882 {
883         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
884
885         frag->value = local->fragmentation_threshold;
886         frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
887         frag->fixed = 1;
888
889         return 0;
890 }
891
892
893 static int ieee80211_ioctl_siwretry(struct net_device *dev,
894                                     struct iw_request_info *info,
895                                     struct iw_param *retry, char *extra)
896 {
897         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
898
899         if (retry->disabled ||
900             (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
901                 return -EINVAL;
902
903         if (retry->flags & IW_RETRY_MAX)
904                 local->long_retry_limit = retry->value;
905         else if (retry->flags & IW_RETRY_MIN)
906                 local->short_retry_limit = retry->value;
907         else {
908                 local->long_retry_limit = retry->value;
909                 local->short_retry_limit = retry->value;
910         }
911
912         if (local->ops->set_retry_limit) {
913                 return local->ops->set_retry_limit(
914                         local_to_hw(local),
915                         local->short_retry_limit,
916                         local->long_retry_limit);
917         }
918
919         return 0;
920 }
921
922
923 static int ieee80211_ioctl_giwretry(struct net_device *dev,
924                                     struct iw_request_info *info,
925                                     struct iw_param *retry, char *extra)
926 {
927         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
928
929         retry->disabled = 0;
930         if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
931                 /* first return min value, iwconfig will ask max value
932                  * later if needed */
933                 retry->flags |= IW_RETRY_LIMIT;
934                 retry->value = local->short_retry_limit;
935                 if (local->long_retry_limit != local->short_retry_limit)
936                         retry->flags |= IW_RETRY_MIN;
937                 return 0;
938         }
939         if (retry->flags & IW_RETRY_MAX) {
940                 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
941                 retry->value = local->long_retry_limit;
942         }
943
944         return 0;
945 }
946
947 static void ieee80211_key_enable_hwaccel(struct ieee80211_local *local,
948                                          struct ieee80211_key *key)
949 {
950         struct ieee80211_key_conf *keyconf;
951         u8 addr[ETH_ALEN];
952
953         if (!key || key->alg != ALG_WEP || !key->force_sw_encrypt ||
954             (local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP))
955                 return;
956
957         memset(addr, 0xff, ETH_ALEN);
958         keyconf = ieee80211_key_data2conf(local, key);
959         if (keyconf && local->ops->set_key &&
960             local->ops->set_key(local_to_hw(local),
961                                SET_KEY, addr, keyconf, 0) == 0) {
962                 key->force_sw_encrypt =
963                         !!(keyconf->flags & IEEE80211_KEY_FORCE_SW_ENCRYPT);
964                 key->hw_key_idx = keyconf->hw_key_idx;
965         }
966         kfree(keyconf);
967 }
968
969
970 static void ieee80211_key_disable_hwaccel(struct ieee80211_local *local,
971                                           struct ieee80211_key *key)
972 {
973         struct ieee80211_key_conf *keyconf;
974         u8 addr[ETH_ALEN];
975
976         if (!key || key->alg != ALG_WEP || key->force_sw_encrypt ||
977             (local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP))
978                 return;
979
980         memset(addr, 0xff, ETH_ALEN);
981         keyconf = ieee80211_key_data2conf(local, key);
982         if (keyconf && local->ops->set_key)
983                 local->ops->set_key(local_to_hw(local), DISABLE_KEY,
984                                    addr, keyconf, 0);
985         kfree(keyconf);
986         key->force_sw_encrypt = 1;
987 }
988
989
990 static int ieee80211_ioctl_default_wep_only(struct ieee80211_local *local,
991                                             int value)
992 {
993         int i;
994         struct ieee80211_sub_if_data *sdata;
995
996         local->default_wep_only = value;
997         read_lock(&local->sub_if_lock);
998         list_for_each_entry(sdata, &local->sub_if_list, list)
999                 for (i = 0; i < NUM_DEFAULT_KEYS; i++)
1000                         if (value)
1001                                 ieee80211_key_enable_hwaccel(local,
1002                                                              sdata->keys[i]);
1003                         else
1004                                 ieee80211_key_disable_hwaccel(local,
1005                                                               sdata->keys[i]);
1006         read_unlock(&local->sub_if_lock);
1007
1008         return 0;
1009 }
1010
1011
1012 void ieee80211_update_default_wep_only(struct ieee80211_local *local)
1013 {
1014         int i = 0;
1015         struct ieee80211_sub_if_data *sdata;
1016
1017         read_lock(&local->sub_if_lock);
1018         list_for_each_entry(sdata, &local->sub_if_list, list) {
1019
1020                 if (sdata->dev == local->mdev)
1021                         continue;
1022
1023                 /* If there is an AP interface then depend on userspace to
1024                    set default_wep_only correctly. */
1025                 if (sdata->type == IEEE80211_IF_TYPE_AP) {
1026                         read_unlock(&local->sub_if_lock);
1027                         return;
1028                 }
1029
1030                 i++;
1031         }
1032
1033         read_unlock(&local->sub_if_lock);
1034
1035         if (i <= 1)
1036                 ieee80211_ioctl_default_wep_only(local, 1);
1037         else
1038                 ieee80211_ioctl_default_wep_only(local, 0);
1039 }
1040
1041
1042 static int ieee80211_ioctl_prism2_param(struct net_device *dev,
1043                                         struct iw_request_info *info,
1044                                         void *wrqu, char *extra)
1045 {
1046         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1047         struct ieee80211_sub_if_data *sdata;
1048         int *i = (int *) extra;
1049         int param = *i;
1050         int value = *(i + 1);
1051         int ret = 0;
1052
1053         if (!capable(CAP_NET_ADMIN))
1054                 return -EPERM;
1055
1056         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1057
1058         switch (param) {
1059         case PRISM2_PARAM_IEEE_802_1X:
1060                 if (local->ops->set_ieee8021x)
1061                         ret = local->ops->set_ieee8021x(local_to_hw(local),
1062                                                         value);
1063                 if (ret)
1064                         printk(KERN_DEBUG "%s: failed to set IEEE 802.1X (%d) "
1065                                "for low-level driver\n", dev->name, value);
1066                 else
1067                         sdata->ieee802_1x = value;
1068                 break;
1069
1070         case PRISM2_PARAM_CTS_PROTECT_ERP_FRAMES:
1071                 if (sdata->type == IEEE80211_IF_TYPE_AP) {
1072                         sdata->use_protection = !!value;
1073                         ieee80211_erp_info_change_notify(dev, IEEE80211_ERP_CHANGE_PROTECTION);
1074                 } else {
1075                         ret = -ENOENT;
1076                 }
1077                 break;
1078
1079         case PRISM2_PARAM_PREAMBLE:
1080                 if (sdata->type != IEEE80211_IF_TYPE_AP) {
1081                         sdata->short_preamble = !!value;
1082                         ieee80211_erp_info_change_notify(dev, IEEE80211_ERP_CHANGE_PREAMBLE);
1083                 } else {
1084                         ret = -ENOENT;
1085                 }
1086                 break;
1087
1088         case PRISM2_PARAM_STAT_TIME:
1089                 if (!local->stat_time && value) {
1090                         local->stat_timer.expires = jiffies + HZ * value / 100;
1091                         add_timer(&local->stat_timer);
1092                 } else if (local->stat_time && !value) {
1093                         del_timer_sync(&local->stat_timer);
1094                 }
1095                 local->stat_time = value;
1096                 break;
1097         case PRISM2_PARAM_SHORT_SLOT_TIME:
1098                 if (value)
1099                         local->hw.conf.flags |= IEEE80211_CONF_SHORT_SLOT_TIME;
1100                 else
1101                         local->hw.conf.flags &= ~IEEE80211_CONF_SHORT_SLOT_TIME;
1102                 if (ieee80211_hw_config(local))
1103                         ret = -EINVAL;
1104                 break;
1105
1106         case PRISM2_PARAM_NEXT_MODE:
1107                 local->next_mode = value;
1108                 break;
1109
1110         case PRISM2_PARAM_RADIO_ENABLED:
1111                 ret = ieee80211_ioctl_set_radio_enabled(dev, value);
1112                 break;
1113
1114         case PRISM2_PARAM_ANTENNA_MODE:
1115                 local->hw.conf.antenna_mode = value;
1116                 if (ieee80211_hw_config(local))
1117                         ret = -EINVAL;
1118                 break;
1119
1120         case PRISM2_PARAM_STA_ANTENNA_SEL:
1121                 local->sta_antenna_sel = value;
1122                 break;
1123
1124         case PRISM2_PARAM_TX_POWER_REDUCTION:
1125                 if (value < 0)
1126                         ret = -EINVAL;
1127                 else
1128                         local->hw.conf.tx_power_reduction = value;
1129                 break;
1130
1131         case PRISM2_PARAM_KEY_TX_RX_THRESHOLD:
1132                 local->key_tx_rx_threshold = value;
1133                 break;
1134
1135         case PRISM2_PARAM_DEFAULT_WEP_ONLY:
1136                 ret = ieee80211_ioctl_default_wep_only(local, value);
1137                 break;
1138
1139         case PRISM2_PARAM_WIFI_WME_NOACK_TEST:
1140                 local->wifi_wme_noack_test = value;
1141                 break;
1142
1143         case PRISM2_PARAM_SCAN_FLAGS:
1144                 local->scan_flags = value;
1145                 break;
1146
1147         case PRISM2_PARAM_MIXED_CELL:
1148                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1149                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1150                         ret = -EINVAL;
1151                 else
1152                         sdata->u.sta.mixed_cell = !!value;
1153                 break;
1154
1155         case PRISM2_PARAM_HW_MODES:
1156                 local->enabled_modes = value;
1157                 break;
1158
1159         case PRISM2_PARAM_CREATE_IBSS:
1160                 if (sdata->type != IEEE80211_IF_TYPE_IBSS)
1161                         ret = -EINVAL;
1162                 else
1163                         sdata->u.sta.create_ibss = !!value;
1164                 break;
1165         case PRISM2_PARAM_WMM_ENABLED:
1166                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1167                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1168                         ret = -EINVAL;
1169                 else
1170                         sdata->u.sta.wmm_enabled = !!value;
1171                 break;
1172         case PRISM2_PARAM_RADAR_DETECT:
1173                 local->hw.conf.radar_detect = value;
1174                 break;
1175         case PRISM2_PARAM_SPECTRUM_MGMT:
1176                 local->hw.conf.spect_mgmt = value;
1177                 break;
1178         default:
1179                 ret = -EOPNOTSUPP;
1180                 break;
1181         }
1182
1183         return ret;
1184 }
1185
1186
1187 static int ieee80211_ioctl_get_prism2_param(struct net_device *dev,
1188                                             struct iw_request_info *info,
1189                                             void *wrqu, char *extra)
1190 {
1191         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1192         struct ieee80211_sub_if_data *sdata;
1193         int *param = (int *) extra;
1194         int ret = 0;
1195
1196         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1197
1198         switch (*param) {
1199         case PRISM2_PARAM_IEEE_802_1X:
1200                 *param = sdata->ieee802_1x;
1201                 break;
1202
1203         case PRISM2_PARAM_CTS_PROTECT_ERP_FRAMES:
1204                 *param = sdata->use_protection;
1205                 break;
1206
1207         case PRISM2_PARAM_PREAMBLE:
1208                 *param = sdata->short_preamble;
1209                 break;
1210
1211         case PRISM2_PARAM_STAT_TIME:
1212                 *param = local->stat_time;
1213                 break;
1214         case PRISM2_PARAM_SHORT_SLOT_TIME:
1215                 *param = !!(local->hw.conf.flags & IEEE80211_CONF_SHORT_SLOT_TIME);
1216                 break;
1217
1218         case PRISM2_PARAM_NEXT_MODE:
1219                 *param = local->next_mode;
1220                 break;
1221
1222         case PRISM2_PARAM_ANTENNA_MODE:
1223                 *param = local->hw.conf.antenna_mode;
1224                 break;
1225
1226         case PRISM2_PARAM_STA_ANTENNA_SEL:
1227                 *param = local->sta_antenna_sel;
1228                 break;
1229
1230         case PRISM2_PARAM_TX_POWER_REDUCTION:
1231                 *param = local->hw.conf.tx_power_reduction;
1232                 break;
1233
1234         case PRISM2_PARAM_KEY_TX_RX_THRESHOLD:
1235                 *param = local->key_tx_rx_threshold;
1236                 break;
1237
1238         case PRISM2_PARAM_DEFAULT_WEP_ONLY:
1239                 *param = local->default_wep_only;
1240                 break;
1241
1242         case PRISM2_PARAM_WIFI_WME_NOACK_TEST:
1243                 *param = local->wifi_wme_noack_test;
1244                 break;
1245
1246         case PRISM2_PARAM_SCAN_FLAGS:
1247                 *param = local->scan_flags;
1248                 break;
1249
1250         case PRISM2_PARAM_HW_MODES:
1251                 *param = local->enabled_modes;
1252                 break;
1253
1254         case PRISM2_PARAM_CREATE_IBSS:
1255                 if (sdata->type != IEEE80211_IF_TYPE_IBSS)
1256                         ret = -EINVAL;
1257                 else
1258                         *param = !!sdata->u.sta.create_ibss;
1259                 break;
1260
1261         case PRISM2_PARAM_MIXED_CELL:
1262                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1263                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1264                         ret = -EINVAL;
1265                 else
1266                         *param = !!sdata->u.sta.mixed_cell;
1267                 break;
1268         case PRISM2_PARAM_WMM_ENABLED:
1269                 if (sdata->type != IEEE80211_IF_TYPE_STA &&
1270                     sdata->type != IEEE80211_IF_TYPE_IBSS)
1271                         ret = -EINVAL;
1272                 else
1273                         *param = !!sdata->u.sta.wmm_enabled;
1274                 break;
1275         default:
1276                 ret = -EOPNOTSUPP;
1277                 break;
1278         }
1279
1280         return ret;
1281 }
1282
1283 static int ieee80211_ioctl_siwmlme(struct net_device *dev,
1284                                    struct iw_request_info *info,
1285                                    struct iw_point *data, char *extra)
1286 {
1287         struct ieee80211_sub_if_data *sdata;
1288         struct iw_mlme *mlme = (struct iw_mlme *) extra;
1289
1290         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1291         if (sdata->type != IEEE80211_IF_TYPE_STA &&
1292             sdata->type != IEEE80211_IF_TYPE_IBSS)
1293                 return -EINVAL;
1294
1295         switch (mlme->cmd) {
1296         case IW_MLME_DEAUTH:
1297                 /* TODO: mlme->addr.sa_data */
1298                 return ieee80211_sta_deauthenticate(dev, mlme->reason_code);
1299         case IW_MLME_DISASSOC:
1300                 /* TODO: mlme->addr.sa_data */
1301                 return ieee80211_sta_disassociate(dev, mlme->reason_code);
1302         default:
1303                 return -EOPNOTSUPP;
1304         }
1305 }
1306
1307
1308 static int ieee80211_ioctl_siwencode(struct net_device *dev,
1309                                      struct iw_request_info *info,
1310                                      struct iw_point *erq, char *keybuf)
1311 {
1312         struct ieee80211_sub_if_data *sdata;
1313         int idx, i, alg = ALG_WEP;
1314         u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
1315
1316         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1317
1318         idx = erq->flags & IW_ENCODE_INDEX;
1319         if (idx == 0) {
1320                 if (sdata->default_key)
1321                         for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1322                                 if (sdata->default_key == sdata->keys[i]) {
1323                                         idx = i;
1324                                         break;
1325                                 }
1326                         }
1327         } else if (idx < 1 || idx > 4)
1328                 return -EINVAL;
1329         else
1330                 idx--;
1331
1332         if (erq->flags & IW_ENCODE_DISABLED)
1333                 alg = ALG_NONE;
1334         else if (erq->length == 0) {
1335                 /* No key data - just set the default TX key index */
1336                 if (sdata->default_key != sdata->keys[idx]) {
1337                         ieee80211_debugfs_key_remove_default(sdata);
1338                         sdata->default_key = sdata->keys[idx];
1339                         if (sdata->default_key)
1340                                 ieee80211_debugfs_key_add_default(sdata);
1341                 }
1342                 return 0;
1343         }
1344
1345         return ieee80211_set_encryption(
1346                 dev, bcaddr,
1347                 idx, alg,
1348                 !sdata->default_key,
1349                 keybuf, erq->length);
1350 }
1351
1352
1353 static int ieee80211_ioctl_giwencode(struct net_device *dev,
1354                                      struct iw_request_info *info,
1355                                      struct iw_point *erq, char *key)
1356 {
1357         struct ieee80211_sub_if_data *sdata;
1358         int idx, i;
1359
1360         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1361
1362         idx = erq->flags & IW_ENCODE_INDEX;
1363         if (idx < 1 || idx > 4) {
1364                 idx = -1;
1365                 if (!sdata->default_key)
1366                         idx = 0;
1367                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1368                         if (sdata->default_key == sdata->keys[i]) {
1369                                 idx = i;
1370                                 break;
1371                         }
1372                 }
1373                 if (idx < 0)
1374                         return -EINVAL;
1375         } else
1376                 idx--;
1377
1378         erq->flags = idx + 1;
1379
1380         if (!sdata->keys[idx]) {
1381                 erq->length = 0;
1382                 erq->flags |= IW_ENCODE_DISABLED;
1383                 return 0;
1384         }
1385
1386         memcpy(key, sdata->keys[idx]->key,
1387                min((int)erq->length, sdata->keys[idx]->keylen));
1388         erq->length = sdata->keys[idx]->keylen;
1389         erq->flags |= IW_ENCODE_ENABLED;
1390
1391         return 0;
1392 }
1393
1394 static int ieee80211_ioctl_siwauth(struct net_device *dev,
1395                                    struct iw_request_info *info,
1396                                    struct iw_param *data, char *extra)
1397 {
1398         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1399         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1400         int ret = 0;
1401
1402         switch (data->flags & IW_AUTH_INDEX) {
1403         case IW_AUTH_WPA_VERSION:
1404         case IW_AUTH_CIPHER_PAIRWISE:
1405         case IW_AUTH_CIPHER_GROUP:
1406         case IW_AUTH_WPA_ENABLED:
1407         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1408                 break;
1409         case IW_AUTH_KEY_MGMT:
1410                 if (sdata->type != IEEE80211_IF_TYPE_STA)
1411                         ret = -EINVAL;
1412                 else {
1413                         /*
1414                          * TODO: sdata->u.sta.key_mgmt does not match with WE18
1415                          * value completely; could consider modifying this to
1416                          * be closer to WE18. For now, this value is not really
1417                          * used for anything else than Privacy matching, so the
1418                          * current code here should be more or less OK.
1419                          */
1420                         if (data->value & IW_AUTH_KEY_MGMT_802_1X) {
1421                                 sdata->u.sta.key_mgmt =
1422                                         IEEE80211_KEY_MGMT_WPA_EAP;
1423                         } else if (data->value & IW_AUTH_KEY_MGMT_PSK) {
1424                                 sdata->u.sta.key_mgmt =
1425                                         IEEE80211_KEY_MGMT_WPA_PSK;
1426                         } else {
1427                                 sdata->u.sta.key_mgmt =
1428                                         IEEE80211_KEY_MGMT_NONE;
1429                         }
1430                 }
1431                 break;
1432         case IW_AUTH_80211_AUTH_ALG:
1433                 if (sdata->type == IEEE80211_IF_TYPE_STA ||
1434                     sdata->type == IEEE80211_IF_TYPE_IBSS)
1435                         sdata->u.sta.auth_algs = data->value;
1436                 else
1437                         ret = -EOPNOTSUPP;
1438                 break;
1439         case IW_AUTH_PRIVACY_INVOKED:
1440                 if (local->ops->set_privacy_invoked)
1441                         ret = local->ops->set_privacy_invoked(
1442                                         local_to_hw(local), data->value);
1443                 break;
1444         default:
1445                 ret = -EOPNOTSUPP;
1446                 break;
1447         }
1448         return ret;
1449 }
1450
1451 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
1452 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
1453 {
1454         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1455         struct iw_statistics *wstats = &local->wstats;
1456         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1457         struct sta_info *sta = NULL;
1458
1459         if (sdata->type == IEEE80211_IF_TYPE_STA ||
1460             sdata->type == IEEE80211_IF_TYPE_IBSS)
1461                 sta = sta_info_get(local, sdata->u.sta.bssid);
1462         if (!sta) {
1463                 wstats->discard.fragment = 0;
1464                 wstats->discard.misc = 0;
1465                 wstats->qual.qual = 0;
1466                 wstats->qual.level = 0;
1467                 wstats->qual.noise = 0;
1468                 wstats->qual.updated = IW_QUAL_ALL_INVALID;
1469         } else {
1470                 wstats->qual.level = sta->last_rssi;
1471                 wstats->qual.qual = sta->last_signal;
1472                 wstats->qual.noise = sta->last_noise;
1473                 wstats->qual.updated = local->wstats_flags;
1474                 sta_info_put(sta);
1475         }
1476         return wstats;
1477 }
1478
1479 static int ieee80211_ioctl_giwauth(struct net_device *dev,
1480                                    struct iw_request_info *info,
1481                                    struct iw_param *data, char *extra)
1482 {
1483         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1484         int ret = 0;
1485
1486         switch (data->flags & IW_AUTH_INDEX) {
1487         case IW_AUTH_80211_AUTH_ALG:
1488                 if (sdata->type == IEEE80211_IF_TYPE_STA ||
1489                     sdata->type == IEEE80211_IF_TYPE_IBSS)
1490                         data->value = sdata->u.sta.auth_algs;
1491                 else
1492                         ret = -EOPNOTSUPP;
1493                 break;
1494         default:
1495                 ret = -EOPNOTSUPP;
1496                 break;
1497         }
1498         return ret;
1499 }
1500
1501
1502 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1503                                         struct iw_request_info *info,
1504                                         struct iw_point *erq, char *extra)
1505 {
1506         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1507         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
1508         int alg, idx, i;
1509
1510         switch (ext->alg) {
1511         case IW_ENCODE_ALG_NONE:
1512                 alg = ALG_NONE;
1513                 break;
1514         case IW_ENCODE_ALG_WEP:
1515                 alg = ALG_WEP;
1516                 break;
1517         case IW_ENCODE_ALG_TKIP:
1518                 alg = ALG_TKIP;
1519                 break;
1520         case IW_ENCODE_ALG_CCMP:
1521                 alg = ALG_CCMP;
1522                 break;
1523         default:
1524                 return -EOPNOTSUPP;
1525         }
1526
1527         if (erq->flags & IW_ENCODE_DISABLED)
1528                 alg = ALG_NONE;
1529
1530         idx = erq->flags & IW_ENCODE_INDEX;
1531         if (idx < 1 || idx > 4) {
1532                 idx = -1;
1533                 if (!sdata->default_key)
1534                         idx = 0;
1535                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1536                         if (sdata->default_key == sdata->keys[i]) {
1537                                 idx = i;
1538                                 break;
1539                         }
1540                 }
1541                 if (idx < 0)
1542                         return -EINVAL;
1543         } else
1544                 idx--;
1545
1546         return ieee80211_set_encryption(dev, ext->addr.sa_data, idx, alg,
1547                                         ext->ext_flags &
1548                                         IW_ENCODE_EXT_SET_TX_KEY,
1549                                         ext->key, ext->key_len);
1550 }
1551
1552
1553 static const struct iw_priv_args ieee80211_ioctl_priv[] = {
1554         { PRISM2_IOCTL_PRISM2_PARAM,
1555           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 2, 0, "param" },
1556         { PRISM2_IOCTL_GET_PRISM2_PARAM,
1557           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1,
1558           IW_PRIV_TYPE_INT | IW_PRIV_SIZE_FIXED | 1, "get_param" },
1559 };
1560
1561 /* Structures to export the Wireless Handlers */
1562
1563 static const iw_handler ieee80211_handler[] =
1564 {
1565         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
1566         (iw_handler) ieee80211_ioctl_giwname,           /* SIOCGIWNAME */
1567         (iw_handler) NULL,                              /* SIOCSIWNWID */
1568         (iw_handler) NULL,                              /* SIOCGIWNWID */
1569         (iw_handler) ieee80211_ioctl_siwfreq,           /* SIOCSIWFREQ */
1570         (iw_handler) ieee80211_ioctl_giwfreq,           /* SIOCGIWFREQ */
1571         (iw_handler) ieee80211_ioctl_siwmode,           /* SIOCSIWMODE */
1572         (iw_handler) ieee80211_ioctl_giwmode,           /* SIOCGIWMODE */
1573         (iw_handler) NULL,                              /* SIOCSIWSENS */
1574         (iw_handler) NULL,                              /* SIOCGIWSENS */
1575         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
1576         (iw_handler) ieee80211_ioctl_giwrange,          /* SIOCGIWRANGE */
1577         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
1578         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
1579         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
1580         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
1581         iw_handler_set_spy,                             /* SIOCSIWSPY */
1582         iw_handler_get_spy,                             /* SIOCGIWSPY */
1583         iw_handler_set_thrspy,                          /* SIOCSIWTHRSPY */
1584         iw_handler_get_thrspy,                          /* SIOCGIWTHRSPY */
1585         (iw_handler) ieee80211_ioctl_siwap,             /* SIOCSIWAP */
1586         (iw_handler) ieee80211_ioctl_giwap,             /* SIOCGIWAP */
1587         (iw_handler) ieee80211_ioctl_siwmlme,           /* SIOCSIWMLME */
1588         (iw_handler) NULL,                              /* SIOCGIWAPLIST */
1589         (iw_handler) ieee80211_ioctl_siwscan,           /* SIOCSIWSCAN */
1590         (iw_handler) ieee80211_ioctl_giwscan,           /* SIOCGIWSCAN */
1591         (iw_handler) ieee80211_ioctl_siwessid,          /* SIOCSIWESSID */
1592         (iw_handler) ieee80211_ioctl_giwessid,          /* SIOCGIWESSID */
1593         (iw_handler) NULL,                              /* SIOCSIWNICKN */
1594         (iw_handler) NULL,                              /* SIOCGIWNICKN */
1595         (iw_handler) NULL,                              /* -- hole -- */
1596         (iw_handler) NULL,                              /* -- hole -- */
1597         (iw_handler) ieee80211_ioctl_siwrate,           /* SIOCSIWRATE */
1598         (iw_handler) ieee80211_ioctl_giwrate,           /* SIOCGIWRATE */
1599         (iw_handler) ieee80211_ioctl_siwrts,            /* SIOCSIWRTS */
1600         (iw_handler) ieee80211_ioctl_giwrts,            /* SIOCGIWRTS */
1601         (iw_handler) ieee80211_ioctl_siwfrag,           /* SIOCSIWFRAG */
1602         (iw_handler) ieee80211_ioctl_giwfrag,           /* SIOCGIWFRAG */
1603         (iw_handler) NULL,                              /* SIOCSIWTXPOW */
1604         (iw_handler) ieee80211_ioctl_giwtxpower,        /* SIOCGIWTXPOW */
1605         (iw_handler) ieee80211_ioctl_siwretry,          /* SIOCSIWRETRY */
1606         (iw_handler) ieee80211_ioctl_giwretry,          /* SIOCGIWRETRY */
1607         (iw_handler) ieee80211_ioctl_siwencode,         /* SIOCSIWENCODE */
1608         (iw_handler) ieee80211_ioctl_giwencode,         /* SIOCGIWENCODE */
1609         (iw_handler) NULL,                              /* SIOCSIWPOWER */
1610         (iw_handler) NULL,                              /* SIOCGIWPOWER */
1611         (iw_handler) NULL,                              /* -- hole -- */
1612         (iw_handler) NULL,                              /* -- hole -- */
1613         (iw_handler) ieee80211_ioctl_siwgenie,          /* SIOCSIWGENIE */
1614         (iw_handler) NULL,                              /* SIOCGIWGENIE */
1615         (iw_handler) ieee80211_ioctl_siwauth,           /* SIOCSIWAUTH */
1616         (iw_handler) ieee80211_ioctl_giwauth,           /* SIOCGIWAUTH */
1617         (iw_handler) ieee80211_ioctl_siwencodeext,      /* SIOCSIWENCODEEXT */
1618         (iw_handler) NULL,                              /* SIOCGIWENCODEEXT */
1619         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
1620         (iw_handler) NULL,                              /* -- hole -- */
1621 };
1622
1623 static const iw_handler ieee80211_private_handler[] =
1624 {                                                       /* SIOCIWFIRSTPRIV + */
1625         (iw_handler) ieee80211_ioctl_prism2_param,      /* 0 */
1626         (iw_handler) ieee80211_ioctl_get_prism2_param,  /* 1 */
1627 };
1628
1629 const struct iw_handler_def ieee80211_iw_handler_def =
1630 {
1631         .num_standard   = ARRAY_SIZE(ieee80211_handler),
1632         .num_private    = ARRAY_SIZE(ieee80211_private_handler),
1633         .num_private_args = ARRAY_SIZE(ieee80211_ioctl_priv),
1634         .standard       = (iw_handler *) ieee80211_handler,
1635         .private        = (iw_handler *) ieee80211_private_handler,
1636         .private_args   = (struct iw_priv_args *) ieee80211_ioctl_priv,
1637         .get_wireless_stats = ieee80211_get_wireless_stats,
1638 };