Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[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 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 u8 ieee80211_get_wstats_flags(struct ieee80211_local *local)
149 {
150         u8 wstats_flags = 0;
151
152         wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
153                                            IEEE80211_HW_SIGNAL_DBM) ?
154                                 IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
155         wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
156                                 IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
157         if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
158                 wstats_flags |= IW_QUAL_DBM;
159
160         return wstats_flags;
161 }
162
163 static int ieee80211_ioctl_giwrange(struct net_device *dev,
164                                  struct iw_request_info *info,
165                                  struct iw_point *data, char *extra)
166 {
167         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
168         struct iw_range *range = (struct iw_range *) extra;
169         enum ieee80211_band band;
170         int c = 0;
171
172         data->length = sizeof(struct iw_range);
173         memset(range, 0, sizeof(struct iw_range));
174
175         range->we_version_compiled = WIRELESS_EXT;
176         range->we_version_source = 21;
177         range->retry_capa = IW_RETRY_LIMIT;
178         range->retry_flags = IW_RETRY_LIMIT;
179         range->min_retry = 0;
180         range->max_retry = 255;
181         range->min_rts = 0;
182         range->max_rts = 2347;
183         range->min_frag = 256;
184         range->max_frag = 2346;
185
186         range->encoding_size[0] = 5;
187         range->encoding_size[1] = 13;
188         range->num_encoding_sizes = 2;
189         range->max_encoding_tokens = NUM_DEFAULT_KEYS;
190
191         /* cfg80211 requires this, and enforces 0..100 */
192         if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
193                 range->max_qual.level = 100;
194         else if  (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
195                 range->max_qual.level = -110;
196         else
197                 range->max_qual.level = 0;
198
199         if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
200                 range->max_qual.noise = -110;
201         else
202                 range->max_qual.noise = 0;
203
204         range->max_qual.qual = 100;
205         range->max_qual.updated = ieee80211_get_wstats_flags(local);
206
207         range->avg_qual.qual = 50;
208         /* not always true but better than nothing */
209         range->avg_qual.level = range->max_qual.level / 2;
210         range->avg_qual.noise = range->max_qual.noise / 2;
211         range->avg_qual.updated = ieee80211_get_wstats_flags(local);
212
213         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
214                           IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
215
216
217         for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
218                 int i;
219                 struct ieee80211_supported_band *sband;
220
221                 sband = local->hw.wiphy->bands[band];
222
223                 if (!sband)
224                         continue;
225
226                 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
227                         struct ieee80211_channel *chan = &sband->channels[i];
228
229                         if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
230                                 range->freq[c].i =
231                                         ieee80211_frequency_to_channel(
232                                                 chan->center_freq);
233                                 range->freq[c].m = chan->center_freq;
234                                 range->freq[c].e = 6;
235                                 c++;
236                         }
237                 }
238         }
239         range->num_channels = c;
240         range->num_frequency = c;
241
242         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
243         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
244         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
245
246         range->scan_capa |= IW_SCAN_CAPA_ESSID;
247
248         return 0;
249 }
250
251
252 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
253                                    struct iw_request_info *info,
254                                    struct iw_freq *freq, char *extra)
255 {
256         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
257
258         if (sdata->vif.type == NL80211_IFTYPE_ADHOC ||
259             sdata->vif.type == NL80211_IFTYPE_STATION)
260                 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
261
262         /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
263         if (freq->e == 0) {
264                 if (freq->m < 0) {
265                         if (sdata->vif.type == NL80211_IFTYPE_ADHOC ||
266                             sdata->vif.type == NL80211_IFTYPE_STATION)
267                                 sdata->u.sta.flags |=
268                                         IEEE80211_STA_AUTO_CHANNEL_SEL;
269                         return 0;
270                 } else
271                         return ieee80211_set_freq(sdata,
272                                 ieee80211_channel_to_frequency(freq->m));
273         } else {
274                 int i, div = 1000000;
275                 for (i = 0; i < freq->e; i++)
276                         div /= 10;
277                 if (div > 0)
278                         return ieee80211_set_freq(sdata, freq->m / div);
279                 else
280                         return -EINVAL;
281         }
282 }
283
284
285 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
286                                    struct iw_request_info *info,
287                                    struct iw_freq *freq, char *extra)
288 {
289         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
290
291         freq->m = local->hw.conf.channel->center_freq;
292         freq->e = 6;
293
294         return 0;
295 }
296
297
298 static int ieee80211_ioctl_siwessid(struct net_device *dev,
299                                     struct iw_request_info *info,
300                                     struct iw_point *data, char *ssid)
301 {
302         struct ieee80211_sub_if_data *sdata;
303         size_t len = data->length;
304
305         /* iwconfig uses nul termination in SSID.. */
306         if (len > 0 && ssid[len - 1] == '\0')
307                 len--;
308
309         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
310         if (sdata->vif.type == NL80211_IFTYPE_STATION ||
311             sdata->vif.type == NL80211_IFTYPE_ADHOC) {
312                 int ret;
313                 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
314                         if (len > IEEE80211_MAX_SSID_LEN)
315                                 return -EINVAL;
316                         memcpy(sdata->u.sta.ssid, ssid, len);
317                         sdata->u.sta.ssid_len = len;
318                         return 0;
319                 }
320                 if (data->flags)
321                         sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
322                 else
323                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_SSID_SEL;
324                 ret = ieee80211_sta_set_ssid(sdata, ssid, len);
325                 if (ret)
326                         return ret;
327                 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
328                 return 0;
329         }
330
331         return -EOPNOTSUPP;
332 }
333
334
335 static int ieee80211_ioctl_giwessid(struct net_device *dev,
336                                     struct iw_request_info *info,
337                                     struct iw_point *data, char *ssid)
338 {
339         size_t len;
340
341         struct ieee80211_sub_if_data *sdata;
342         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
343         if (sdata->vif.type == NL80211_IFTYPE_STATION ||
344             sdata->vif.type == NL80211_IFTYPE_ADHOC) {
345                 int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
346                 if (res == 0) {
347                         data->length = len;
348                         data->flags = 1;
349                 } else
350                         data->flags = 0;
351                 return res;
352         }
353
354         return -EOPNOTSUPP;
355 }
356
357
358 static int ieee80211_ioctl_siwap(struct net_device *dev,
359                                  struct iw_request_info *info,
360                                  struct sockaddr *ap_addr, char *extra)
361 {
362         struct ieee80211_sub_if_data *sdata;
363
364         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
365         if (sdata->vif.type == NL80211_IFTYPE_STATION ||
366             sdata->vif.type == NL80211_IFTYPE_ADHOC) {
367                 int ret;
368                 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
369                         memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
370                                ETH_ALEN);
371                         return 0;
372                 }
373                 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
374                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
375                                 IEEE80211_STA_AUTO_CHANNEL_SEL;
376                 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
377                         sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
378                 else
379                         sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
380                 ret = ieee80211_sta_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
381                 if (ret)
382                         return ret;
383                 ieee80211_sta_req_auth(sdata, &sdata->u.sta);
384                 return 0;
385         } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
386                 /*
387                  * If it is necessary to update the WDS peer address
388                  * while the interface is running, then we need to do
389                  * more work here, namely if it is running we need to
390                  * add a new and remove the old STA entry, this is
391                  * normally handled by _open() and _stop().
392                  */
393                 if (netif_running(dev))
394                         return -EBUSY;
395
396                 memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
397                        ETH_ALEN);
398
399                 return 0;
400         }
401
402         return -EOPNOTSUPP;
403 }
404
405
406 static int ieee80211_ioctl_giwap(struct net_device *dev,
407                                  struct iw_request_info *info,
408                                  struct sockaddr *ap_addr, char *extra)
409 {
410         struct ieee80211_sub_if_data *sdata;
411
412         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
413         if (sdata->vif.type == NL80211_IFTYPE_STATION ||
414             sdata->vif.type == NL80211_IFTYPE_ADHOC) {
415                 if (sdata->u.sta.state == IEEE80211_STA_MLME_ASSOCIATED ||
416                     sdata->u.sta.state == IEEE80211_STA_MLME_IBSS_JOINED) {
417                         ap_addr->sa_family = ARPHRD_ETHER;
418                         memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
419                         return 0;
420                 } else {
421                         memset(&ap_addr->sa_data, 0, ETH_ALEN);
422                         return 0;
423                 }
424         } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
425                 ap_addr->sa_family = ARPHRD_ETHER;
426                 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
427                 return 0;
428         }
429
430         return -EOPNOTSUPP;
431 }
432
433
434 static int ieee80211_ioctl_siwrate(struct net_device *dev,
435                                   struct iw_request_info *info,
436                                   struct iw_param *rate, char *extra)
437 {
438         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
439         int i, err = -EINVAL;
440         u32 target_rate = rate->value / 100000;
441         struct ieee80211_sub_if_data *sdata;
442         struct ieee80211_supported_band *sband;
443
444         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
445
446         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
447
448         /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
449          * target_rate = X, rate->fixed = 1 means only rate X
450          * target_rate = X, rate->fixed = 0 means all rates <= X */
451         sdata->max_ratectrl_rateidx = -1;
452         sdata->force_unicast_rateidx = -1;
453         if (rate->value < 0)
454                 return 0;
455
456         for (i=0; i< sband->n_bitrates; i++) {
457                 struct ieee80211_rate *brate = &sband->bitrates[i];
458                 int this_rate = brate->bitrate;
459
460                 if (target_rate == this_rate) {
461                         sdata->max_ratectrl_rateidx = i;
462                         if (rate->fixed)
463                                 sdata->force_unicast_rateidx = i;
464                         err = 0;
465                         break;
466                 }
467         }
468         return err;
469 }
470
471 static int ieee80211_ioctl_giwrate(struct net_device *dev,
472                                   struct iw_request_info *info,
473                                   struct iw_param *rate, char *extra)
474 {
475         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
476         struct sta_info *sta;
477         struct ieee80211_sub_if_data *sdata;
478         struct ieee80211_supported_band *sband;
479
480         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
481
482         if (sdata->vif.type != NL80211_IFTYPE_STATION)
483                 return -EOPNOTSUPP;
484
485         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
486
487         rcu_read_lock();
488
489         sta = sta_info_get(local, sdata->u.sta.bssid);
490
491         if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
492                 rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
493         else
494                 rate->value = 0;
495
496         rcu_read_unlock();
497
498         if (!sta)
499                 return -ENODEV;
500
501         rate->value *= 100000;
502
503         return 0;
504 }
505
506 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
507                                       struct iw_request_info *info,
508                                       union iwreq_data *data, char *extra)
509 {
510         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
511         struct ieee80211_channel* chan = local->hw.conf.channel;
512         u32 reconf_flags = 0;
513         int new_power_level;
514
515         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
516                 return -EINVAL;
517         if (data->txpower.flags & IW_TXPOW_RANGE)
518                 return -EINVAL;
519         if (!chan)
520                 return -EINVAL;
521
522         if (data->txpower.fixed)
523                 new_power_level = min(data->txpower.value, chan->max_power);
524         else /* Automatic power level setting */
525                 new_power_level = chan->max_power;
526
527         local->user_power_level = new_power_level;
528         if (local->hw.conf.power_level != new_power_level)
529                 reconf_flags |= IEEE80211_CONF_CHANGE_POWER;
530
531         if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
532                 local->hw.conf.radio_enabled = !(data->txpower.disabled);
533                 reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
534                 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
535         }
536
537         if (reconf_flags)
538                 ieee80211_hw_config(local, reconf_flags);
539
540         return 0;
541 }
542
543 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
544                                    struct iw_request_info *info,
545                                    union iwreq_data *data, char *extra)
546 {
547         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
548
549         data->txpower.fixed = 1;
550         data->txpower.disabled = !(local->hw.conf.radio_enabled);
551         data->txpower.value = local->hw.conf.power_level;
552         data->txpower.flags = IW_TXPOW_DBM;
553
554         return 0;
555 }
556
557 static int ieee80211_ioctl_siwrts(struct net_device *dev,
558                                   struct iw_request_info *info,
559                                   struct iw_param *rts, char *extra)
560 {
561         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
562
563         if (rts->disabled)
564                 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
565         else if (!rts->fixed)
566                 /* if the rts value is not fixed, then take default */
567                 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
568         else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
569                 return -EINVAL;
570         else
571                 local->rts_threshold = rts->value;
572
573         /* If the wlan card performs RTS/CTS in hardware/firmware,
574          * configure it here */
575
576         if (local->ops->set_rts_threshold)
577                 local->ops->set_rts_threshold(local_to_hw(local),
578                                              local->rts_threshold);
579
580         return 0;
581 }
582
583 static int ieee80211_ioctl_giwrts(struct net_device *dev,
584                                   struct iw_request_info *info,
585                                   struct iw_param *rts, char *extra)
586 {
587         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
588
589         rts->value = local->rts_threshold;
590         rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
591         rts->fixed = 1;
592
593         return 0;
594 }
595
596
597 static int ieee80211_ioctl_siwfrag(struct net_device *dev,
598                                    struct iw_request_info *info,
599                                    struct iw_param *frag, char *extra)
600 {
601         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
602
603         if (frag->disabled)
604                 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
605         else if (!frag->fixed)
606                 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
607         else if (frag->value < 256 ||
608                  frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
609                 return -EINVAL;
610         else {
611                 /* Fragment length must be even, so strip LSB. */
612                 local->fragmentation_threshold = frag->value & ~0x1;
613         }
614
615         return 0;
616 }
617
618 static int ieee80211_ioctl_giwfrag(struct net_device *dev,
619                                    struct iw_request_info *info,
620                                    struct iw_param *frag, char *extra)
621 {
622         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
623
624         frag->value = local->fragmentation_threshold;
625         frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
626         frag->fixed = 1;
627
628         return 0;
629 }
630
631
632 static int ieee80211_ioctl_siwretry(struct net_device *dev,
633                                     struct iw_request_info *info,
634                                     struct iw_param *retry, char *extra)
635 {
636         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
637
638         if (retry->disabled ||
639             (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
640                 return -EINVAL;
641
642         if (retry->flags & IW_RETRY_MAX) {
643                 local->hw.conf.long_frame_max_tx_count = retry->value;
644         } else if (retry->flags & IW_RETRY_MIN) {
645                 local->hw.conf.short_frame_max_tx_count = retry->value;
646         } else {
647                 local->hw.conf.long_frame_max_tx_count = retry->value;
648                 local->hw.conf.short_frame_max_tx_count = retry->value;
649         }
650
651         ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
652
653         return 0;
654 }
655
656
657 static int ieee80211_ioctl_giwretry(struct net_device *dev,
658                                     struct iw_request_info *info,
659                                     struct iw_param *retry, char *extra)
660 {
661         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
662
663         retry->disabled = 0;
664         if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
665                 /* first return min value, iwconfig will ask max value
666                  * later if needed */
667                 retry->flags |= IW_RETRY_LIMIT;
668                 retry->value = local->hw.conf.short_frame_max_tx_count;
669                 if (local->hw.conf.long_frame_max_tx_count !=
670                     local->hw.conf.short_frame_max_tx_count)
671                         retry->flags |= IW_RETRY_MIN;
672                 return 0;
673         }
674         if (retry->flags & IW_RETRY_MAX) {
675                 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
676                 retry->value = local->hw.conf.long_frame_max_tx_count;
677         }
678
679         return 0;
680 }
681
682 static int ieee80211_ioctl_siwmlme(struct net_device *dev,
683                                    struct iw_request_info *info,
684                                    struct iw_point *data, char *extra)
685 {
686         struct ieee80211_sub_if_data *sdata;
687         struct iw_mlme *mlme = (struct iw_mlme *) extra;
688
689         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
690         if (sdata->vif.type != NL80211_IFTYPE_STATION &&
691             sdata->vif.type != NL80211_IFTYPE_ADHOC)
692                 return -EINVAL;
693
694         switch (mlme->cmd) {
695         case IW_MLME_DEAUTH:
696                 /* TODO: mlme->addr.sa_data */
697                 return ieee80211_sta_deauthenticate(sdata, mlme->reason_code);
698         case IW_MLME_DISASSOC:
699                 /* TODO: mlme->addr.sa_data */
700                 return ieee80211_sta_disassociate(sdata, mlme->reason_code);
701         default:
702                 return -EOPNOTSUPP;
703         }
704 }
705
706
707 static int ieee80211_ioctl_siwencode(struct net_device *dev,
708                                      struct iw_request_info *info,
709                                      struct iw_point *erq, char *keybuf)
710 {
711         struct ieee80211_sub_if_data *sdata;
712         int idx, i, alg = ALG_WEP;
713         u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
714         int remove = 0;
715
716         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
717
718         idx = erq->flags & IW_ENCODE_INDEX;
719         if (idx == 0) {
720                 if (sdata->default_key)
721                         for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
722                                 if (sdata->default_key == sdata->keys[i]) {
723                                         idx = i;
724                                         break;
725                                 }
726                         }
727         } else if (idx < 1 || idx > 4)
728                 return -EINVAL;
729         else
730                 idx--;
731
732         if (erq->flags & IW_ENCODE_DISABLED)
733                 remove = 1;
734         else if (erq->length == 0) {
735                 /* No key data - just set the default TX key index */
736                 ieee80211_set_default_key(sdata, idx);
737                 return 0;
738         }
739
740         return ieee80211_set_encryption(
741                 sdata, bcaddr,
742                 idx, alg, remove,
743                 !sdata->default_key,
744                 keybuf, erq->length);
745 }
746
747
748 static int ieee80211_ioctl_giwencode(struct net_device *dev,
749                                      struct iw_request_info *info,
750                                      struct iw_point *erq, char *key)
751 {
752         struct ieee80211_sub_if_data *sdata;
753         int idx, i;
754
755         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
756
757         idx = erq->flags & IW_ENCODE_INDEX;
758         if (idx < 1 || idx > 4) {
759                 idx = -1;
760                 if (!sdata->default_key)
761                         idx = 0;
762                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
763                         if (sdata->default_key == sdata->keys[i]) {
764                                 idx = i;
765                                 break;
766                         }
767                 }
768                 if (idx < 0)
769                         return -EINVAL;
770         } else
771                 idx--;
772
773         erq->flags = idx + 1;
774
775         if (!sdata->keys[idx]) {
776                 erq->length = 0;
777                 erq->flags |= IW_ENCODE_DISABLED;
778                 return 0;
779         }
780
781         memcpy(key, sdata->keys[idx]->conf.key,
782                min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
783         erq->length = sdata->keys[idx]->conf.keylen;
784         erq->flags |= IW_ENCODE_ENABLED;
785
786         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
787                 struct ieee80211_if_sta *ifsta = &sdata->u.sta;
788                 switch (ifsta->auth_alg) {
789                 case WLAN_AUTH_OPEN:
790                 case WLAN_AUTH_LEAP:
791                         erq->flags |= IW_ENCODE_OPEN;
792                         break;
793                 case WLAN_AUTH_SHARED_KEY:
794                         erq->flags |= IW_ENCODE_RESTRICTED;
795                         break;
796                 }
797         }
798
799         return 0;
800 }
801
802 static int ieee80211_ioctl_siwpower(struct net_device *dev,
803                                     struct iw_request_info *info,
804                                     struct iw_param *wrq,
805                                     char *extra)
806 {
807         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
808         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
809         struct ieee80211_conf *conf = &local->hw.conf;
810         int ret = 0, timeout = 0;
811         bool ps;
812
813         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
814                 return -EOPNOTSUPP;
815
816         if (sdata->vif.type != NL80211_IFTYPE_STATION)
817                 return -EINVAL;
818
819         if (wrq->disabled) {
820                 ps = false;
821                 timeout = 0;
822                 goto set;
823         }
824
825         switch (wrq->flags & IW_POWER_MODE) {
826         case IW_POWER_ON:       /* If not specified */
827         case IW_POWER_MODE:     /* If set all mask */
828         case IW_POWER_ALL_R:    /* If explicitely state all */
829                 ps = true;
830                 break;
831         default:                /* Otherwise we ignore */
832                 return -EINVAL;
833         }
834
835         if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
836                 return -EINVAL;
837
838         if (wrq->flags & IW_POWER_TIMEOUT)
839                 timeout = wrq->value / 1000;
840
841  set:
842         if (ps == local->powersave && timeout == conf->dynamic_ps_timeout)
843                 return ret;
844
845         local->powersave = ps;
846         conf->dynamic_ps_timeout = timeout;
847
848         if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
849                 ret = ieee80211_hw_config(local,
850                                           IEEE80211_CONF_CHANGE_DYNPS_TIMEOUT);
851
852         if (!(sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
853                 return ret;
854
855         if (conf->dynamic_ps_timeout > 0 &&
856             !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
857                 mod_timer(&local->dynamic_ps_timer, jiffies +
858                           msecs_to_jiffies(conf->dynamic_ps_timeout));
859         } else {
860                 if (local->powersave) {
861                         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
862                                 ieee80211_send_nullfunc(local, sdata, 1);
863                         conf->flags |= IEEE80211_CONF_PS;
864                         ret = ieee80211_hw_config(local,
865                                         IEEE80211_CONF_CHANGE_PS);
866                 } else {
867                         conf->flags &= ~IEEE80211_CONF_PS;
868                         ret = ieee80211_hw_config(local,
869                                         IEEE80211_CONF_CHANGE_PS);
870                         if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
871                                 ieee80211_send_nullfunc(local, sdata, 0);
872                         del_timer_sync(&local->dynamic_ps_timer);
873                         cancel_work_sync(&local->dynamic_ps_enable_work);
874                 }
875         }
876
877         return ret;
878 }
879
880 static int ieee80211_ioctl_giwpower(struct net_device *dev,
881                                     struct iw_request_info *info,
882                                     union iwreq_data *wrqu,
883                                     char *extra)
884 {
885         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
886
887         wrqu->power.disabled = !local->powersave;
888
889         return 0;
890 }
891
892 static int ieee80211_ioctl_siwauth(struct net_device *dev,
893                                    struct iw_request_info *info,
894                                    struct iw_param *data, char *extra)
895 {
896         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
897         int ret = 0;
898
899         switch (data->flags & IW_AUTH_INDEX) {
900         case IW_AUTH_WPA_VERSION:
901         case IW_AUTH_CIPHER_GROUP:
902         case IW_AUTH_WPA_ENABLED:
903         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
904         case IW_AUTH_KEY_MGMT:
905         case IW_AUTH_CIPHER_GROUP_MGMT:
906                 break;
907         case IW_AUTH_CIPHER_PAIRWISE:
908                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
909                         if (data->value & (IW_AUTH_CIPHER_WEP40 |
910                             IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_TKIP))
911                                 sdata->u.sta.flags |=
912                                         IEEE80211_STA_TKIP_WEP_USED;
913                         else
914                                 sdata->u.sta.flags &=
915                                         ~IEEE80211_STA_TKIP_WEP_USED;
916                 }
917                 break;
918         case IW_AUTH_DROP_UNENCRYPTED:
919                 sdata->drop_unencrypted = !!data->value;
920                 break;
921         case IW_AUTH_PRIVACY_INVOKED:
922                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
923                         ret = -EINVAL;
924                 else {
925                         sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
926                         /*
927                          * Privacy invoked by wpa_supplicant, store the
928                          * value and allow associating to a protected
929                          * network without having a key up front.
930                          */
931                         if (data->value)
932                                 sdata->u.sta.flags |=
933                                         IEEE80211_STA_PRIVACY_INVOKED;
934                 }
935                 break;
936         case IW_AUTH_80211_AUTH_ALG:
937                 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
938                     sdata->vif.type == NL80211_IFTYPE_ADHOC)
939                         sdata->u.sta.auth_algs = data->value;
940                 else
941                         ret = -EOPNOTSUPP;
942                 break;
943         case IW_AUTH_MFP:
944                 if (!(sdata->local->hw.flags & IEEE80211_HW_MFP_CAPABLE)) {
945                         ret = -EOPNOTSUPP;
946                         break;
947                 }
948                 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
949                     sdata->vif.type == NL80211_IFTYPE_ADHOC) {
950                         switch (data->value) {
951                         case IW_AUTH_MFP_DISABLED:
952                                 sdata->u.sta.mfp = IEEE80211_MFP_DISABLED;
953                                 break;
954                         case IW_AUTH_MFP_OPTIONAL:
955                                 sdata->u.sta.mfp = IEEE80211_MFP_OPTIONAL;
956                                 break;
957                         case IW_AUTH_MFP_REQUIRED:
958                                 sdata->u.sta.mfp = IEEE80211_MFP_REQUIRED;
959                                 break;
960                         default:
961                                 ret = -EINVAL;
962                         }
963                 } else
964                         ret = -EOPNOTSUPP;
965                 break;
966         default:
967                 ret = -EOPNOTSUPP;
968                 break;
969         }
970         return ret;
971 }
972
973 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
974 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
975 {
976         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
977         struct iw_statistics *wstats = &local->wstats;
978         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
979         struct sta_info *sta = NULL;
980
981         rcu_read_lock();
982
983         if (sdata->vif.type == NL80211_IFTYPE_STATION ||
984             sdata->vif.type == NL80211_IFTYPE_ADHOC)
985                 sta = sta_info_get(local, sdata->u.sta.bssid);
986         if (!sta) {
987                 wstats->discard.fragment = 0;
988                 wstats->discard.misc = 0;
989                 wstats->qual.qual = 0;
990                 wstats->qual.level = 0;
991                 wstats->qual.noise = 0;
992                 wstats->qual.updated = IW_QUAL_ALL_INVALID;
993         } else {
994                 wstats->qual.level = sta->last_signal;
995                 wstats->qual.qual = sta->last_qual;
996                 wstats->qual.noise = sta->last_noise;
997                 wstats->qual.updated = ieee80211_get_wstats_flags(local);
998         }
999
1000         rcu_read_unlock();
1001
1002         return wstats;
1003 }
1004
1005 static int ieee80211_ioctl_giwauth(struct net_device *dev,
1006                                    struct iw_request_info *info,
1007                                    struct iw_param *data, char *extra)
1008 {
1009         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1010         int ret = 0;
1011
1012         switch (data->flags & IW_AUTH_INDEX) {
1013         case IW_AUTH_80211_AUTH_ALG:
1014                 if (sdata->vif.type == NL80211_IFTYPE_STATION ||
1015                     sdata->vif.type == NL80211_IFTYPE_ADHOC)
1016                         data->value = sdata->u.sta.auth_algs;
1017                 else
1018                         ret = -EOPNOTSUPP;
1019                 break;
1020         default:
1021                 ret = -EOPNOTSUPP;
1022                 break;
1023         }
1024         return ret;
1025 }
1026
1027
1028 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1029                                         struct iw_request_info *info,
1030                                         struct iw_point *erq, char *extra)
1031 {
1032         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1033         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
1034         int uninitialized_var(alg), idx, i, remove = 0;
1035
1036         switch (ext->alg) {
1037         case IW_ENCODE_ALG_NONE:
1038                 remove = 1;
1039                 break;
1040         case IW_ENCODE_ALG_WEP:
1041                 alg = ALG_WEP;
1042                 break;
1043         case IW_ENCODE_ALG_TKIP:
1044                 alg = ALG_TKIP;
1045                 break;
1046         case IW_ENCODE_ALG_CCMP:
1047                 alg = ALG_CCMP;
1048                 break;
1049         case IW_ENCODE_ALG_AES_CMAC:
1050                 alg = ALG_AES_CMAC;
1051                 break;
1052         default:
1053                 return -EOPNOTSUPP;
1054         }
1055
1056         if (erq->flags & IW_ENCODE_DISABLED)
1057                 remove = 1;
1058
1059         idx = erq->flags & IW_ENCODE_INDEX;
1060         if (alg == ALG_AES_CMAC) {
1061                 if (idx < NUM_DEFAULT_KEYS + 1 ||
1062                     idx > NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) {
1063                         idx = -1;
1064                         if (!sdata->default_mgmt_key)
1065                                 idx = 0;
1066                         else for (i = NUM_DEFAULT_KEYS;
1067                                   i < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
1068                                   i++) {
1069                                 if (sdata->default_mgmt_key == sdata->keys[i])
1070                                 {
1071                                         idx = i;
1072                                         break;
1073                                 }
1074                         }
1075                         if (idx < 0)
1076                                 return -EINVAL;
1077                 } else
1078                         idx--;
1079         } else {
1080                 if (idx < 1 || idx > 4) {
1081                         idx = -1;
1082                         if (!sdata->default_key)
1083                                 idx = 0;
1084                         else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1085                                 if (sdata->default_key == sdata->keys[i]) {
1086                                         idx = i;
1087                                         break;
1088                                 }
1089                         }
1090                         if (idx < 0)
1091                                 return -EINVAL;
1092                 } else
1093                         idx--;
1094         }
1095
1096         return ieee80211_set_encryption(sdata, 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) cfg80211_wext_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) cfg80211_wext_siwmode,             /* SIOCSIWMODE */
1115         (iw_handler) cfg80211_wext_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) cfg80211_wext_siwscan,             /* SIOCSIWSCAN */
1133         (iw_handler) cfg80211_wext_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) ieee80211_ioctl_siwpower,          /* SIOCSIWPOWER */
1153         (iw_handler) ieee80211_ioctl_giwpower,          /* 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 };