ide: remove IDE_TFLAG_NO_SELECT_MASK taskfile flag
[linux-2.6] / net / mac80211 / wext.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 "led.h"
25 #include "rate.h"
26 #include "wpa.h"
27 #include "aes_ccm.h"
28
29
30 static int ieee80211_set_encryption(struct net_device *dev, u8 *sta_addr,
31                                     int idx, int alg, int remove,
32                                     int set_tx_key, const u8 *_key,
33                                     size_t key_len)
34 {
35         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
36         struct sta_info *sta;
37         struct ieee80211_key *key;
38         struct ieee80211_sub_if_data *sdata;
39         int err;
40
41         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
42
43         if (idx < 0 || idx >= NUM_DEFAULT_KEYS) {
44                 printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
45                        dev->name, idx);
46                 return -EINVAL;
47         }
48
49         if (remove) {
50                 rcu_read_lock();
51
52                 err = 0;
53
54                 if (is_broadcast_ether_addr(sta_addr)) {
55                         key = sdata->keys[idx];
56                 } else {
57                         sta = sta_info_get(local, sta_addr);
58                         if (!sta) {
59                                 err = -ENOENT;
60                                 goto out_unlock;
61                         }
62                         key = sta->key;
63                 }
64
65                 ieee80211_key_free(key);
66         } else {
67                 key = ieee80211_key_alloc(alg, idx, key_len, _key);
68                 if (!key)
69                         return -ENOMEM;
70
71                 sta = NULL;
72                 err = 0;
73
74                 rcu_read_lock();
75
76                 if (!is_broadcast_ether_addr(sta_addr)) {
77                         set_tx_key = 0;
78                         /*
79                          * According to the standard, the key index of a
80                          * pairwise key must be zero. However, some AP are
81                          * broken when it comes to WEP key indices, so we
82                          * work around this.
83                          */
84                         if (idx != 0 && alg != ALG_WEP) {
85                                 ieee80211_key_free(key);
86                                 err = -EINVAL;
87                                 goto out_unlock;
88                         }
89
90                         sta = sta_info_get(local, sta_addr);
91                         if (!sta) {
92                                 ieee80211_key_free(key);
93                                 err = -ENOENT;
94                                 goto out_unlock;
95                         }
96                 }
97
98                 if (alg == ALG_WEP &&
99                         key_len != LEN_WEP40 && key_len != LEN_WEP104) {
100                         ieee80211_key_free(key);
101                         err = -EINVAL;
102                         goto out_unlock;
103                 }
104
105                 ieee80211_key_link(key, sdata, sta);
106
107                 if (set_tx_key || (!sta && !sdata->default_key && key))
108                         ieee80211_set_default_key(sdata, idx);
109         }
110
111  out_unlock:
112         rcu_read_unlock();
113
114         return err;
115 }
116
117 static int ieee80211_ioctl_siwgenie(struct net_device *dev,
118                                     struct iw_request_info *info,
119                                     struct iw_point *data, char *extra)
120 {
121         struct ieee80211_sub_if_data *sdata;
122
123         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
124
125         if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)
126                 return -EOPNOTSUPP;
127
128         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
129             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
130                 int ret = ieee80211_sta_set_extra_ie(dev, extra, data->length);
131                 if (ret)
132                         return ret;
133                 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
134                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
135                 return 0;
136         }
137
138         return -EOPNOTSUPP;
139 }
140
141 static int ieee80211_ioctl_giwname(struct net_device *dev,
142                                    struct iw_request_info *info,
143                                    char *name, char *extra)
144 {
145         strcpy(name, "IEEE 802.11");
146
147         return 0;
148 }
149
150
151 static int ieee80211_ioctl_giwrange(struct net_device *dev,
152                                  struct iw_request_info *info,
153                                  struct iw_point *data, char *extra)
154 {
155         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
156         struct iw_range *range = (struct iw_range *) extra;
157         enum ieee80211_band band;
158         int c = 0;
159
160         data->length = sizeof(struct iw_range);
161         memset(range, 0, sizeof(struct iw_range));
162
163         range->we_version_compiled = WIRELESS_EXT;
164         range->we_version_source = 21;
165         range->retry_capa = IW_RETRY_LIMIT;
166         range->retry_flags = IW_RETRY_LIMIT;
167         range->min_retry = 0;
168         range->max_retry = 255;
169         range->min_rts = 0;
170         range->max_rts = 2347;
171         range->min_frag = 256;
172         range->max_frag = 2346;
173
174         range->encoding_size[0] = 5;
175         range->encoding_size[1] = 13;
176         range->num_encoding_sizes = 2;
177         range->max_encoding_tokens = NUM_DEFAULT_KEYS;
178
179         range->max_qual.qual = local->hw.max_signal;
180         range->max_qual.level = local->hw.max_rssi;
181         range->max_qual.noise = local->hw.max_noise;
182         range->max_qual.updated = local->wstats_flags;
183
184         range->avg_qual.qual = local->hw.max_signal/2;
185         range->avg_qual.level = 0;
186         range->avg_qual.noise = 0;
187         range->avg_qual.updated = local->wstats_flags;
188
189         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
190                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
191
192
193         for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
194                 int i;
195                 struct ieee80211_supported_band *sband;
196
197                 sband = local->hw.wiphy->bands[band];
198
199                 if (!sband)
200                         continue;
201
202                 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
203                         struct ieee80211_channel *chan = &sband->channels[i];
204
205                         if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
206                                 range->freq[c].i =
207                                         ieee80211_frequency_to_channel(
208                                                 chan->center_freq);
209                                 range->freq[c].m = chan->center_freq;
210                                 range->freq[c].e = 6;
211                                 c++;
212                         }
213                 }
214         }
215         range->num_channels = c;
216         range->num_frequency = c;
217
218         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
219         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
220         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
221
222         range->scan_capa |= IW_SCAN_CAPA_ESSID;
223
224         return 0;
225 }
226
227
228 static int ieee80211_ioctl_siwmode(struct net_device *dev,
229                                    struct iw_request_info *info,
230                                    __u32 *mode, char *extra)
231 {
232         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
233         int type;
234
235         if (sdata->vif.type == IEEE80211_IF_TYPE_VLAN)
236                 return -EOPNOTSUPP;
237
238         switch (*mode) {
239         case IW_MODE_INFRA:
240                 type = IEEE80211_IF_TYPE_STA;
241                 break;
242         case IW_MODE_ADHOC:
243                 type = IEEE80211_IF_TYPE_IBSS;
244                 break;
245         case IW_MODE_REPEAT:
246                 type = IEEE80211_IF_TYPE_WDS;
247                 break;
248         case IW_MODE_MONITOR:
249                 type = IEEE80211_IF_TYPE_MNTR;
250                 break;
251         default:
252                 return -EINVAL;
253         }
254
255         if (type == sdata->vif.type)
256                 return 0;
257         if (netif_running(dev))
258                 return -EBUSY;
259
260         ieee80211_if_reinit(dev);
261         ieee80211_if_set_type(dev, type);
262
263         return 0;
264 }
265
266
267 static int ieee80211_ioctl_giwmode(struct net_device *dev,
268                                    struct iw_request_info *info,
269                                    __u32 *mode, char *extra)
270 {
271         struct ieee80211_sub_if_data *sdata;
272
273         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
274         switch (sdata->vif.type) {
275         case IEEE80211_IF_TYPE_AP:
276                 *mode = IW_MODE_MASTER;
277                 break;
278         case IEEE80211_IF_TYPE_STA:
279                 *mode = IW_MODE_INFRA;
280                 break;
281         case IEEE80211_IF_TYPE_IBSS:
282                 *mode = IW_MODE_ADHOC;
283                 break;
284         case IEEE80211_IF_TYPE_MNTR:
285                 *mode = IW_MODE_MONITOR;
286                 break;
287         case IEEE80211_IF_TYPE_WDS:
288                 *mode = IW_MODE_REPEAT;
289                 break;
290         case IEEE80211_IF_TYPE_VLAN:
291                 *mode = IW_MODE_SECOND;         /* FIXME */
292                 break;
293         default:
294                 *mode = IW_MODE_AUTO;
295                 break;
296         }
297         return 0;
298 }
299
300 int ieee80211_set_freq(struct net_device *dev, int freqMHz)
301 {
302         int ret = -EINVAL;
303         struct ieee80211_channel *chan;
304         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
305         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
306
307         chan = ieee80211_get_channel(local->hw.wiphy, freqMHz);
308
309         if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
310                 if (sdata->vif.type == IEEE80211_IF_TYPE_IBSS &&
311                     chan->flags & IEEE80211_CHAN_NO_IBSS) {
312                         printk(KERN_DEBUG "%s: IBSS not allowed on frequency "
313                                 "%d MHz\n", dev->name, chan->center_freq);
314                         return ret;
315                 }
316                 local->oper_channel = chan;
317
318                 if (local->sta_sw_scanning || local->sta_hw_scanning)
319                         ret = 0;
320                 else
321                         ret = ieee80211_hw_config(local);
322
323                 rate_control_clear(local);
324         }
325
326         return ret;
327 }
328
329 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
330                                    struct iw_request_info *info,
331                                    struct iw_freq *freq, char *extra)
332 {
333         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
334
335         if (sdata->vif.type == IEEE80211_IF_TYPE_STA)
336                 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
337
338         /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
339         if (freq->e == 0) {
340                 if (freq->m < 0) {
341                         if (sdata->vif.type == IEEE80211_IF_TYPE_STA)
342                                 sdata->u.sta.flags |=
343                                         IEEE80211_STA_AUTO_CHANNEL_SEL;
344                         return 0;
345                 } else
346                         return ieee80211_set_freq(dev,
347                                 ieee80211_channel_to_frequency(freq->m));
348         } else {
349                 int i, div = 1000000;
350                 for (i = 0; i < freq->e; i++)
351                         div /= 10;
352                 if (div > 0)
353                         return ieee80211_set_freq(dev, freq->m / div);
354                 else
355                         return -EINVAL;
356         }
357 }
358
359
360 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
361                                    struct iw_request_info *info,
362                                    struct iw_freq *freq, char *extra)
363 {
364         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
365
366         freq->m = local->hw.conf.channel->center_freq;
367         freq->e = 6;
368
369         return 0;
370 }
371
372
373 static int ieee80211_ioctl_siwessid(struct net_device *dev,
374                                     struct iw_request_info *info,
375                                     struct iw_point *data, char *ssid)
376 {
377         struct ieee80211_sub_if_data *sdata;
378         size_t len = data->length;
379
380         /* iwconfig uses nul termination in SSID.. */
381         if (len > 0 && ssid[len - 1] == '\0')
382                 len--;
383
384         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
385         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
386             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
387                 int ret;
388                 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
389                         if (len > IEEE80211_MAX_SSID_LEN)
390                                 return -EINVAL;
391                         memcpy(sdata->u.sta.ssid, ssid, len);
392                         sdata->u.sta.ssid_len = len;
393                         return 0;
394                 }
395                 if (data->flags)
396                         sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
397                 else
398                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_SSID_SEL;
399                 ret = ieee80211_sta_set_ssid(dev, ssid, len);
400                 if (ret)
401                         return ret;
402                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
403                 return 0;
404         }
405
406         if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
407                 memcpy(sdata->u.ap.ssid, ssid, len);
408                 memset(sdata->u.ap.ssid + len, 0,
409                        IEEE80211_MAX_SSID_LEN - len);
410                 sdata->u.ap.ssid_len = len;
411                 return ieee80211_if_config(dev);
412         }
413         return -EOPNOTSUPP;
414 }
415
416
417 static int ieee80211_ioctl_giwessid(struct net_device *dev,
418                                     struct iw_request_info *info,
419                                     struct iw_point *data, char *ssid)
420 {
421         size_t len;
422
423         struct ieee80211_sub_if_data *sdata;
424         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
425         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
426             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
427                 int res = ieee80211_sta_get_ssid(dev, ssid, &len);
428                 if (res == 0) {
429                         data->length = len;
430                         data->flags = 1;
431                 } else
432                         data->flags = 0;
433                 return res;
434         }
435
436         if (sdata->vif.type == IEEE80211_IF_TYPE_AP) {
437                 len = sdata->u.ap.ssid_len;
438                 if (len > IW_ESSID_MAX_SIZE)
439                         len = IW_ESSID_MAX_SIZE;
440                 memcpy(ssid, sdata->u.ap.ssid, len);
441                 data->length = len;
442                 data->flags = 1;
443                 return 0;
444         }
445         return -EOPNOTSUPP;
446 }
447
448
449 static int ieee80211_ioctl_siwap(struct net_device *dev,
450                                  struct iw_request_info *info,
451                                  struct sockaddr *ap_addr, char *extra)
452 {
453         struct ieee80211_sub_if_data *sdata;
454
455         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
456         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
457             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
458                 int ret;
459                 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
460                         memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
461                                ETH_ALEN);
462                         return 0;
463                 }
464                 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
465                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
466                                 IEEE80211_STA_AUTO_CHANNEL_SEL;
467                 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
468                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
469                 else
470                         sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
471                 ret = ieee80211_sta_set_bssid(dev, (u8 *) &ap_addr->sa_data);
472                 if (ret)
473                         return ret;
474                 ieee80211_sta_req_auth(dev, &sdata->u.sta);
475                 return 0;
476         } else if (sdata->vif.type == IEEE80211_IF_TYPE_WDS) {
477                 /*
478                  * If it is necessary to update the WDS peer address
479                  * while the interface is running, then we need to do
480                  * more work here, namely if it is running we need to
481                  * add a new and remove the old STA entry, this is
482                  * normally handled by _open() and _stop().
483                  */
484                 if (netif_running(dev))
485                         return -EBUSY;
486
487                 memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
488                        ETH_ALEN);
489
490                 return 0;
491         }
492
493         return -EOPNOTSUPP;
494 }
495
496
497 static int ieee80211_ioctl_giwap(struct net_device *dev,
498                                  struct iw_request_info *info,
499                                  struct sockaddr *ap_addr, char *extra)
500 {
501         struct ieee80211_sub_if_data *sdata;
502
503         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
504         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
505             sdata->vif.type == IEEE80211_IF_TYPE_IBSS) {
506                 if (sdata->u.sta.state == IEEE80211_ASSOCIATED ||
507                     sdata->u.sta.state == IEEE80211_IBSS_JOINED) {
508                         ap_addr->sa_family = ARPHRD_ETHER;
509                         memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
510                         return 0;
511                 } else {
512                         memset(&ap_addr->sa_data, 0, ETH_ALEN);
513                         return 0;
514                 }
515         } else if (sdata->vif.type == IEEE80211_IF_TYPE_WDS) {
516                 ap_addr->sa_family = ARPHRD_ETHER;
517                 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
518                 return 0;
519         }
520
521         return -EOPNOTSUPP;
522 }
523
524
525 static int ieee80211_ioctl_siwscan(struct net_device *dev,
526                                    struct iw_request_info *info,
527                                    union iwreq_data *wrqu, char *extra)
528 {
529         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
530         struct iw_scan_req *req = NULL;
531         u8 *ssid = NULL;
532         size_t ssid_len = 0;
533
534         if (!netif_running(dev))
535                 return -ENETDOWN;
536
537         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
538             sdata->vif.type != IEEE80211_IF_TYPE_IBSS &&
539             sdata->vif.type != IEEE80211_IF_TYPE_MESH_POINT &&
540             sdata->vif.type != IEEE80211_IF_TYPE_AP)
541                 return -EOPNOTSUPP;
542
543         /* if SSID was specified explicitly then use that */
544         if (wrqu->data.length == sizeof(struct iw_scan_req) &&
545             wrqu->data.flags & IW_SCAN_THIS_ESSID) {
546                 req = (struct iw_scan_req *)extra;
547                 ssid = req->essid;
548                 ssid_len = req->essid_len;
549         }
550
551         return ieee80211_sta_req_scan(dev, ssid, ssid_len);
552 }
553
554
555 static int ieee80211_ioctl_giwscan(struct net_device *dev,
556                                    struct iw_request_info *info,
557                                    struct iw_point *data, char *extra)
558 {
559         int res;
560         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
561
562         if (local->sta_sw_scanning || local->sta_hw_scanning)
563                 return -EAGAIN;
564
565         res = ieee80211_sta_scan_results(dev, extra, data->length);
566         if (res >= 0) {
567                 data->length = res;
568                 return 0;
569         }
570         data->length = 0;
571         return res;
572 }
573
574
575 static int ieee80211_ioctl_siwrate(struct net_device *dev,
576                                   struct iw_request_info *info,
577                                   struct iw_param *rate, char *extra)
578 {
579         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
580         int i, err = -EINVAL;
581         u32 target_rate = rate->value / 100000;
582         struct ieee80211_sub_if_data *sdata;
583         struct ieee80211_supported_band *sband;
584
585         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
586         if (!sdata->bss)
587                 return -ENODEV;
588
589         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
590
591         /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
592          * target_rate = X, rate->fixed = 1 means only rate X
593          * target_rate = X, rate->fixed = 0 means all rates <= X */
594         sdata->bss->max_ratectrl_rateidx = -1;
595         sdata->bss->force_unicast_rateidx = -1;
596         if (rate->value < 0)
597                 return 0;
598
599         for (i=0; i< sband->n_bitrates; i++) {
600                 struct ieee80211_rate *brate = &sband->bitrates[i];
601                 int this_rate = brate->bitrate;
602
603                 if (target_rate == this_rate) {
604                         sdata->bss->max_ratectrl_rateidx = i;
605                         if (rate->fixed)
606                                 sdata->bss->force_unicast_rateidx = i;
607                         err = 0;
608                         break;
609                 }
610         }
611         return err;
612 }
613
614 static int ieee80211_ioctl_giwrate(struct net_device *dev,
615                                   struct iw_request_info *info,
616                                   struct iw_param *rate, char *extra)
617 {
618         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
619         struct sta_info *sta;
620         struct ieee80211_sub_if_data *sdata;
621         struct ieee80211_supported_band *sband;
622
623         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
624
625         if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
626                 return -EOPNOTSUPP;
627
628         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
629
630         rcu_read_lock();
631
632         sta = sta_info_get(local, sdata->u.sta.bssid);
633
634         if (sta && sta->txrate_idx < sband->n_bitrates)
635                 rate->value = sband->bitrates[sta->txrate_idx].bitrate;
636         else
637                 rate->value = 0;
638
639         rcu_read_unlock();
640
641         if (!sta)
642                 return -ENODEV;
643
644         rate->value *= 100000;
645
646         return 0;
647 }
648
649 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
650                                       struct iw_request_info *info,
651                                       union iwreq_data *data, char *extra)
652 {
653         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
654         bool need_reconfig = 0;
655         int new_power_level;
656
657         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
658                 return -EINVAL;
659         if (data->txpower.flags & IW_TXPOW_RANGE)
660                 return -EINVAL;
661
662         if (data->txpower.fixed) {
663                 new_power_level = data->txpower.value;
664         } else {
665                 /*
666                  * Automatic power level. Use maximum power for the current
667                  * channel. Should be part of rate control.
668                  */
669                 struct ieee80211_channel* chan = local->hw.conf.channel;
670                 if (!chan)
671                         return -EINVAL;
672
673                 new_power_level = chan->max_power;
674         }
675
676         if (local->hw.conf.power_level != new_power_level) {
677                 local->hw.conf.power_level = new_power_level;
678                 need_reconfig = 1;
679         }
680
681         if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
682                 local->hw.conf.radio_enabled = !(data->txpower.disabled);
683                 need_reconfig = 1;
684                 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
685         }
686
687         if (need_reconfig) {
688                 ieee80211_hw_config(local);
689                 /* The return value of hw_config is not of big interest here,
690                  * as it doesn't say that it failed because of _this_ config
691                  * change or something else. Ignore it. */
692         }
693
694         return 0;
695 }
696
697 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
698                                    struct iw_request_info *info,
699                                    union iwreq_data *data, char *extra)
700 {
701         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
702
703         data->txpower.fixed = 1;
704         data->txpower.disabled = !(local->hw.conf.radio_enabled);
705         data->txpower.value = local->hw.conf.power_level;
706         data->txpower.flags = IW_TXPOW_DBM;
707
708         return 0;
709 }
710
711 static int ieee80211_ioctl_siwrts(struct net_device *dev,
712                                   struct iw_request_info *info,
713                                   struct iw_param *rts, char *extra)
714 {
715         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
716
717         if (rts->disabled)
718                 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
719         else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
720                 return -EINVAL;
721         else
722                 local->rts_threshold = rts->value;
723
724         /* If the wlan card performs RTS/CTS in hardware/firmware,
725          * configure it here */
726
727         if (local->ops->set_rts_threshold)
728                 local->ops->set_rts_threshold(local_to_hw(local),
729                                              local->rts_threshold);
730
731         return 0;
732 }
733
734 static int ieee80211_ioctl_giwrts(struct net_device *dev,
735                                   struct iw_request_info *info,
736                                   struct iw_param *rts, char *extra)
737 {
738         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
739
740         rts->value = local->rts_threshold;
741         rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
742         rts->fixed = 1;
743
744         return 0;
745 }
746
747
748 static int ieee80211_ioctl_siwfrag(struct net_device *dev,
749                                    struct iw_request_info *info,
750                                    struct iw_param *frag, char *extra)
751 {
752         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
753
754         if (frag->disabled)
755                 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
756         else if (frag->value < 256 ||
757                  frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
758                 return -EINVAL;
759         else {
760                 /* Fragment length must be even, so strip LSB. */
761                 local->fragmentation_threshold = frag->value & ~0x1;
762         }
763
764         /* If the wlan card performs fragmentation in hardware/firmware,
765          * configure it here */
766
767         if (local->ops->set_frag_threshold)
768                 local->ops->set_frag_threshold(
769                         local_to_hw(local),
770                         local->fragmentation_threshold);
771
772         return 0;
773 }
774
775 static int ieee80211_ioctl_giwfrag(struct net_device *dev,
776                                    struct iw_request_info *info,
777                                    struct iw_param *frag, char *extra)
778 {
779         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
780
781         frag->value = local->fragmentation_threshold;
782         frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
783         frag->fixed = 1;
784
785         return 0;
786 }
787
788
789 static int ieee80211_ioctl_siwretry(struct net_device *dev,
790                                     struct iw_request_info *info,
791                                     struct iw_param *retry, char *extra)
792 {
793         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
794
795         if (retry->disabled ||
796             (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
797                 return -EINVAL;
798
799         if (retry->flags & IW_RETRY_MAX)
800                 local->long_retry_limit = retry->value;
801         else if (retry->flags & IW_RETRY_MIN)
802                 local->short_retry_limit = retry->value;
803         else {
804                 local->long_retry_limit = retry->value;
805                 local->short_retry_limit = retry->value;
806         }
807
808         if (local->ops->set_retry_limit) {
809                 return local->ops->set_retry_limit(
810                         local_to_hw(local),
811                         local->short_retry_limit,
812                         local->long_retry_limit);
813         }
814
815         return 0;
816 }
817
818
819 static int ieee80211_ioctl_giwretry(struct net_device *dev,
820                                     struct iw_request_info *info,
821                                     struct iw_param *retry, char *extra)
822 {
823         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
824
825         retry->disabled = 0;
826         if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
827                 /* first return min value, iwconfig will ask max value
828                  * later if needed */
829                 retry->flags |= IW_RETRY_LIMIT;
830                 retry->value = local->short_retry_limit;
831                 if (local->long_retry_limit != local->short_retry_limit)
832                         retry->flags |= IW_RETRY_MIN;
833                 return 0;
834         }
835         if (retry->flags & IW_RETRY_MAX) {
836                 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
837                 retry->value = local->long_retry_limit;
838         }
839
840         return 0;
841 }
842
843 static int ieee80211_ioctl_siwmlme(struct net_device *dev,
844                                    struct iw_request_info *info,
845                                    struct iw_point *data, char *extra)
846 {
847         struct ieee80211_sub_if_data *sdata;
848         struct iw_mlme *mlme = (struct iw_mlme *) extra;
849
850         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
851         if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
852             sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
853                 return -EINVAL;
854
855         switch (mlme->cmd) {
856         case IW_MLME_DEAUTH:
857                 /* TODO: mlme->addr.sa_data */
858                 return ieee80211_sta_deauthenticate(dev, mlme->reason_code);
859         case IW_MLME_DISASSOC:
860                 /* TODO: mlme->addr.sa_data */
861                 return ieee80211_sta_disassociate(dev, mlme->reason_code);
862         default:
863                 return -EOPNOTSUPP;
864         }
865 }
866
867
868 static int ieee80211_ioctl_siwencode(struct net_device *dev,
869                                      struct iw_request_info *info,
870                                      struct iw_point *erq, char *keybuf)
871 {
872         struct ieee80211_sub_if_data *sdata;
873         int idx, i, alg = ALG_WEP;
874         u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
875         int remove = 0;
876
877         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
878
879         idx = erq->flags & IW_ENCODE_INDEX;
880         if (idx == 0) {
881                 if (sdata->default_key)
882                         for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
883                                 if (sdata->default_key == sdata->keys[i]) {
884                                         idx = i;
885                                         break;
886                                 }
887                         }
888         } else if (idx < 1 || idx > 4)
889                 return -EINVAL;
890         else
891                 idx--;
892
893         if (erq->flags & IW_ENCODE_DISABLED)
894                 remove = 1;
895         else if (erq->length == 0) {
896                 /* No key data - just set the default TX key index */
897                 ieee80211_set_default_key(sdata, idx);
898                 return 0;
899         }
900
901         return ieee80211_set_encryption(
902                 dev, bcaddr,
903                 idx, alg, remove,
904                 !sdata->default_key,
905                 keybuf, erq->length);
906 }
907
908
909 static int ieee80211_ioctl_giwencode(struct net_device *dev,
910                                      struct iw_request_info *info,
911                                      struct iw_point *erq, char *key)
912 {
913         struct ieee80211_sub_if_data *sdata;
914         int idx, i;
915
916         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
917
918         idx = erq->flags & IW_ENCODE_INDEX;
919         if (idx < 1 || idx > 4) {
920                 idx = -1;
921                 if (!sdata->default_key)
922                         idx = 0;
923                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
924                         if (sdata->default_key == sdata->keys[i]) {
925                                 idx = i;
926                                 break;
927                         }
928                 }
929                 if (idx < 0)
930                         return -EINVAL;
931         } else
932                 idx--;
933
934         erq->flags = idx + 1;
935
936         if (!sdata->keys[idx]) {
937                 erq->length = 0;
938                 erq->flags |= IW_ENCODE_DISABLED;
939                 return 0;
940         }
941
942         memcpy(key, sdata->keys[idx]->conf.key,
943                min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
944         erq->length = sdata->keys[idx]->conf.keylen;
945         erq->flags |= IW_ENCODE_ENABLED;
946
947         return 0;
948 }
949
950 static int ieee80211_ioctl_siwauth(struct net_device *dev,
951                                    struct iw_request_info *info,
952                                    struct iw_param *data, char *extra)
953 {
954         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
955         int ret = 0;
956
957         switch (data->flags & IW_AUTH_INDEX) {
958         case IW_AUTH_WPA_VERSION:
959         case IW_AUTH_CIPHER_PAIRWISE:
960         case IW_AUTH_CIPHER_GROUP:
961         case IW_AUTH_WPA_ENABLED:
962         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
963         case IW_AUTH_KEY_MGMT:
964                 break;
965         case IW_AUTH_DROP_UNENCRYPTED:
966                 sdata->drop_unencrypted = !!data->value;
967                 break;
968         case IW_AUTH_PRIVACY_INVOKED:
969                 if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
970                         ret = -EINVAL;
971                 else {
972                         sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
973                         /*
974                          * Privacy invoked by wpa_supplicant, store the
975                          * value and allow associating to a protected
976                          * network without having a key up front.
977                          */
978                         if (data->value)
979                                 sdata->u.sta.flags |=
980                                         IEEE80211_STA_PRIVACY_INVOKED;
981                 }
982                 break;
983         case IW_AUTH_80211_AUTH_ALG:
984                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
985                     sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
986                         sdata->u.sta.auth_algs = data->value;
987                 else
988                         ret = -EOPNOTSUPP;
989                 break;
990         default:
991                 ret = -EOPNOTSUPP;
992                 break;
993         }
994         return ret;
995 }
996
997 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
998 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
999 {
1000         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1001         struct iw_statistics *wstats = &local->wstats;
1002         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1003         struct sta_info *sta = NULL;
1004
1005         rcu_read_lock();
1006
1007         if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
1008             sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
1009                 sta = sta_info_get(local, sdata->u.sta.bssid);
1010         if (!sta) {
1011                 wstats->discard.fragment = 0;
1012                 wstats->discard.misc = 0;
1013                 wstats->qual.qual = 0;
1014                 wstats->qual.level = 0;
1015                 wstats->qual.noise = 0;
1016                 wstats->qual.updated = IW_QUAL_ALL_INVALID;
1017         } else {
1018                 wstats->qual.level = sta->last_rssi;
1019                 wstats->qual.qual = sta->last_signal;
1020                 wstats->qual.noise = sta->last_noise;
1021                 wstats->qual.updated = local->wstats_flags;
1022         }
1023
1024         rcu_read_unlock();
1025
1026         return wstats;
1027 }
1028
1029 static int ieee80211_ioctl_giwauth(struct net_device *dev,
1030                                    struct iw_request_info *info,
1031                                    struct iw_param *data, char *extra)
1032 {
1033         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1034         int ret = 0;
1035
1036         switch (data->flags & IW_AUTH_INDEX) {
1037         case IW_AUTH_80211_AUTH_ALG:
1038                 if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
1039                     sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
1040                         data->value = sdata->u.sta.auth_algs;
1041                 else
1042                         ret = -EOPNOTSUPP;
1043                 break;
1044         default:
1045                 ret = -EOPNOTSUPP;
1046                 break;
1047         }
1048         return ret;
1049 }
1050
1051
1052 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1053                                         struct iw_request_info *info,
1054                                         struct iw_point *erq, char *extra)
1055 {
1056         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1057         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
1058         int uninitialized_var(alg), idx, i, remove = 0;
1059
1060         switch (ext->alg) {
1061         case IW_ENCODE_ALG_NONE:
1062                 remove = 1;
1063                 break;
1064         case IW_ENCODE_ALG_WEP:
1065                 alg = ALG_WEP;
1066                 break;
1067         case IW_ENCODE_ALG_TKIP:
1068                 alg = ALG_TKIP;
1069                 break;
1070         case IW_ENCODE_ALG_CCMP:
1071                 alg = ALG_CCMP;
1072                 break;
1073         default:
1074                 return -EOPNOTSUPP;
1075         }
1076
1077         if (erq->flags & IW_ENCODE_DISABLED)
1078                 remove = 1;
1079
1080         idx = erq->flags & IW_ENCODE_INDEX;
1081         if (idx < 1 || idx > 4) {
1082                 idx = -1;
1083                 if (!sdata->default_key)
1084                         idx = 0;
1085                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1086                         if (sdata->default_key == sdata->keys[i]) {
1087                                 idx = i;
1088                                 break;
1089                         }
1090                 }
1091                 if (idx < 0)
1092                         return -EINVAL;
1093         } else
1094                 idx--;
1095
1096         return ieee80211_set_encryption(dev, ext->addr.sa_data, idx, alg,
1097                                         remove,
1098                                         ext->ext_flags &
1099                                         IW_ENCODE_EXT_SET_TX_KEY,
1100                                         ext->key, ext->key_len);
1101 }
1102
1103
1104 /* Structures to export the Wireless Handlers */
1105
1106 static const iw_handler ieee80211_handler[] =
1107 {
1108         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
1109         (iw_handler) ieee80211_ioctl_giwname,           /* SIOCGIWNAME */
1110         (iw_handler) NULL,                              /* SIOCSIWNWID */
1111         (iw_handler) NULL,                              /* SIOCGIWNWID */
1112         (iw_handler) ieee80211_ioctl_siwfreq,           /* SIOCSIWFREQ */
1113         (iw_handler) ieee80211_ioctl_giwfreq,           /* SIOCGIWFREQ */
1114         (iw_handler) ieee80211_ioctl_siwmode,           /* SIOCSIWMODE */
1115         (iw_handler) ieee80211_ioctl_giwmode,           /* SIOCGIWMODE */
1116         (iw_handler) NULL,                              /* SIOCSIWSENS */
1117         (iw_handler) NULL,                              /* SIOCGIWSENS */
1118         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
1119         (iw_handler) ieee80211_ioctl_giwrange,          /* SIOCGIWRANGE */
1120         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
1121         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
1122         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
1123         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
1124         (iw_handler) NULL,                              /* SIOCSIWSPY */
1125         (iw_handler) NULL,                              /* SIOCGIWSPY */
1126         (iw_handler) NULL,                              /* SIOCSIWTHRSPY */
1127         (iw_handler) NULL,                              /* SIOCGIWTHRSPY */
1128         (iw_handler) ieee80211_ioctl_siwap,             /* SIOCSIWAP */
1129         (iw_handler) ieee80211_ioctl_giwap,             /* SIOCGIWAP */
1130         (iw_handler) ieee80211_ioctl_siwmlme,           /* SIOCSIWMLME */
1131         (iw_handler) NULL,                              /* SIOCGIWAPLIST */
1132         (iw_handler) ieee80211_ioctl_siwscan,           /* SIOCSIWSCAN */
1133         (iw_handler) ieee80211_ioctl_giwscan,           /* SIOCGIWSCAN */
1134         (iw_handler) ieee80211_ioctl_siwessid,          /* SIOCSIWESSID */
1135         (iw_handler) ieee80211_ioctl_giwessid,          /* SIOCGIWESSID */
1136         (iw_handler) NULL,                              /* SIOCSIWNICKN */
1137         (iw_handler) NULL,                              /* SIOCGIWNICKN */
1138         (iw_handler) NULL,                              /* -- hole -- */
1139         (iw_handler) NULL,                              /* -- hole -- */
1140         (iw_handler) ieee80211_ioctl_siwrate,           /* SIOCSIWRATE */
1141         (iw_handler) ieee80211_ioctl_giwrate,           /* SIOCGIWRATE */
1142         (iw_handler) ieee80211_ioctl_siwrts,            /* SIOCSIWRTS */
1143         (iw_handler) ieee80211_ioctl_giwrts,            /* SIOCGIWRTS */
1144         (iw_handler) ieee80211_ioctl_siwfrag,           /* SIOCSIWFRAG */
1145         (iw_handler) ieee80211_ioctl_giwfrag,           /* SIOCGIWFRAG */
1146         (iw_handler) ieee80211_ioctl_siwtxpower,        /* SIOCSIWTXPOW */
1147         (iw_handler) ieee80211_ioctl_giwtxpower,        /* SIOCGIWTXPOW */
1148         (iw_handler) ieee80211_ioctl_siwretry,          /* SIOCSIWRETRY */
1149         (iw_handler) ieee80211_ioctl_giwretry,          /* SIOCGIWRETRY */
1150         (iw_handler) ieee80211_ioctl_siwencode,         /* SIOCSIWENCODE */
1151         (iw_handler) ieee80211_ioctl_giwencode,         /* SIOCGIWENCODE */
1152         (iw_handler) NULL,                              /* SIOCSIWPOWER */
1153         (iw_handler) NULL,                              /* SIOCGIWPOWER */
1154         (iw_handler) NULL,                              /* -- hole -- */
1155         (iw_handler) NULL,                              /* -- hole -- */
1156         (iw_handler) ieee80211_ioctl_siwgenie,          /* SIOCSIWGENIE */
1157         (iw_handler) NULL,                              /* SIOCGIWGENIE */
1158         (iw_handler) ieee80211_ioctl_siwauth,           /* SIOCSIWAUTH */
1159         (iw_handler) ieee80211_ioctl_giwauth,           /* SIOCGIWAUTH */
1160         (iw_handler) ieee80211_ioctl_siwencodeext,      /* SIOCSIWENCODEEXT */
1161         (iw_handler) NULL,                              /* SIOCGIWENCODEEXT */
1162         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
1163         (iw_handler) NULL,                              /* -- hole -- */
1164 };
1165
1166 const struct iw_handler_def ieee80211_iw_handler_def =
1167 {
1168         .num_standard   = ARRAY_SIZE(ieee80211_handler),
1169         .standard       = (iw_handler *) ieee80211_handler,
1170         .get_wireless_stats = ieee80211_get_wireless_stats,
1171 };