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