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