nl80211: Send timeout event on failed direct probe
[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                 return cfg80211_ibss_wext_siwfreq(dev, info, freq, extra);
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_STATION)
160                                 sdata->u.mgd.flags |=
161                                         IEEE80211_STA_AUTO_CHANNEL_SEL;
162                         return 0;
163                 } else
164                         return ieee80211_set_freq(sdata,
165                                 ieee80211_channel_to_frequency(freq->m));
166         } else {
167                 int i, div = 1000000;
168                 for (i = 0; i < freq->e; i++)
169                         div /= 10;
170                 if (div > 0)
171                         return ieee80211_set_freq(sdata, freq->m / div);
172                 else
173                         return -EINVAL;
174         }
175 }
176
177
178 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
179                                    struct iw_request_info *info,
180                                    struct iw_freq *freq, char *extra)
181 {
182         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
183         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
184
185         if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
186                 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
187
188         freq->m = local->hw.conf.channel->center_freq;
189         freq->e = 6;
190
191         return 0;
192 }
193
194
195 static int ieee80211_ioctl_siwessid(struct net_device *dev,
196                                     struct iw_request_info *info,
197                                     struct iw_point *data, char *ssid)
198 {
199         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
200         size_t len = data->length;
201         int ret;
202
203         if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
204                 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
205
206         /* iwconfig uses nul termination in SSID.. */
207         if (len > 0 && ssid[len - 1] == '\0')
208                 len--;
209
210         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
211                 if (data->flags)
212                         sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
213                 else
214                         sdata->u.mgd.flags |= IEEE80211_STA_AUTO_SSID_SEL;
215
216                 ret = ieee80211_sta_set_ssid(sdata, ssid, len);
217                 if (ret)
218                         return ret;
219
220                 sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
221                 ieee80211_sta_req_auth(sdata);
222                 return 0;
223         }
224
225         return -EOPNOTSUPP;
226 }
227
228
229 static int ieee80211_ioctl_giwessid(struct net_device *dev,
230                                     struct iw_request_info *info,
231                                     struct iw_point *data, char *ssid)
232 {
233         size_t len;
234         struct ieee80211_sub_if_data *sdata;
235
236         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
237
238         if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
239                 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
240
241         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
242                 int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
243                 if (res == 0) {
244                         data->length = len;
245                         data->flags = 1;
246                 } else
247                         data->flags = 0;
248                 return res;
249         }
250
251         return -EOPNOTSUPP;
252 }
253
254
255 static int ieee80211_ioctl_siwap(struct net_device *dev,
256                                  struct iw_request_info *info,
257                                  struct sockaddr *ap_addr, char *extra)
258 {
259         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
260
261         if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
262                 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
263
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_WDS) {
281                 /*
282                  * If it is necessary to update the WDS peer address
283                  * while the interface is running, then we need to do
284                  * more work here, namely if it is running we need to
285                  * add a new and remove the old STA entry, this is
286                  * normally handled by _open() and _stop().
287                  */
288                 if (netif_running(dev))
289                         return -EBUSY;
290
291                 memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
292                        ETH_ALEN);
293
294                 return 0;
295         }
296
297         return -EOPNOTSUPP;
298 }
299
300
301 static int ieee80211_ioctl_giwap(struct net_device *dev,
302                                  struct iw_request_info *info,
303                                  struct sockaddr *ap_addr, char *extra)
304 {
305         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
306
307         if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
308                 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
309
310         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
311                 if (sdata->u.mgd.state == IEEE80211_STA_MLME_ASSOCIATED) {
312                         ap_addr->sa_family = ARPHRD_ETHER;
313                         memcpy(&ap_addr->sa_data, sdata->u.mgd.bssid, ETH_ALEN);
314                 } else
315                         memset(&ap_addr->sa_data, 0, ETH_ALEN);
316                 return 0;
317         } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
318                 ap_addr->sa_family = ARPHRD_ETHER;
319                 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
320                 return 0;
321         }
322
323         return -EOPNOTSUPP;
324 }
325
326
327 static int ieee80211_ioctl_siwrate(struct net_device *dev,
328                                   struct iw_request_info *info,
329                                   struct iw_param *rate, char *extra)
330 {
331         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
332         int i, err = -EINVAL;
333         u32 target_rate = rate->value / 100000;
334         struct ieee80211_sub_if_data *sdata;
335         struct ieee80211_supported_band *sband;
336
337         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
338
339         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
340
341         /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
342          * target_rate = X, rate->fixed = 1 means only rate X
343          * target_rate = X, rate->fixed = 0 means all rates <= X */
344         sdata->max_ratectrl_rateidx = -1;
345         sdata->force_unicast_rateidx = -1;
346         if (rate->value < 0)
347                 return 0;
348
349         for (i=0; i< sband->n_bitrates; i++) {
350                 struct ieee80211_rate *brate = &sband->bitrates[i];
351                 int this_rate = brate->bitrate;
352
353                 if (target_rate == this_rate) {
354                         sdata->max_ratectrl_rateidx = i;
355                         if (rate->fixed)
356                                 sdata->force_unicast_rateidx = i;
357                         err = 0;
358                         break;
359                 }
360         }
361         return err;
362 }
363
364 static int ieee80211_ioctl_giwrate(struct net_device *dev,
365                                   struct iw_request_info *info,
366                                   struct iw_param *rate, char *extra)
367 {
368         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
369         struct sta_info *sta;
370         struct ieee80211_sub_if_data *sdata;
371         struct ieee80211_supported_band *sband;
372
373         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
374
375         if (sdata->vif.type != NL80211_IFTYPE_STATION)
376                 return -EOPNOTSUPP;
377
378         sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
379
380         rcu_read_lock();
381
382         sta = sta_info_get(local, sdata->u.mgd.bssid);
383
384         if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
385                 rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
386         else
387                 rate->value = 0;
388
389         rcu_read_unlock();
390
391         if (!sta)
392                 return -ENODEV;
393
394         rate->value *= 100000;
395
396         return 0;
397 }
398
399 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
400                                       struct iw_request_info *info,
401                                       union iwreq_data *data, char *extra)
402 {
403         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
404         struct ieee80211_channel* chan = local->hw.conf.channel;
405         bool reconf = false;
406         u32 reconf_flags = 0;
407         int new_power_level;
408
409         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
410                 return -EINVAL;
411         if (data->txpower.flags & IW_TXPOW_RANGE)
412                 return -EINVAL;
413         if (!chan)
414                 return -EINVAL;
415
416         /* only change when not disabling */
417         if (!data->txpower.disabled) {
418                 if (data->txpower.fixed) {
419                         if (data->txpower.value < 0)
420                                 return -EINVAL;
421                         new_power_level = data->txpower.value;
422                         /*
423                          * Debatable, but we cannot do a fixed power
424                          * level above the regulatory constraint.
425                          * Use "iwconfig wlan0 txpower 15dBm" instead.
426                          */
427                         if (new_power_level > chan->max_power)
428                                 return -EINVAL;
429                 } else {
430                         /*
431                          * Automatic power level setting, max being the value
432                          * passed in from userland.
433                          */
434                         if (data->txpower.value < 0)
435                                 new_power_level = -1;
436                         else
437                                 new_power_level = data->txpower.value;
438                 }
439
440                 reconf = true;
441
442                 /*
443                  * ieee80211_hw_config() will limit to the channel's
444                  * max power and possibly power constraint from AP.
445                  */
446                 local->user_power_level = new_power_level;
447         }
448
449         if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
450                 local->hw.conf.radio_enabled = !(data->txpower.disabled);
451                 reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
452                 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
453         }
454
455         if (reconf || reconf_flags)
456                 ieee80211_hw_config(local, reconf_flags);
457
458         return 0;
459 }
460
461 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
462                                    struct iw_request_info *info,
463                                    union iwreq_data *data, char *extra)
464 {
465         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
466
467         data->txpower.fixed = 1;
468         data->txpower.disabled = !(local->hw.conf.radio_enabled);
469         data->txpower.value = local->hw.conf.power_level;
470         data->txpower.flags = IW_TXPOW_DBM;
471
472         return 0;
473 }
474
475 static int ieee80211_ioctl_siwencode(struct net_device *dev,
476                                      struct iw_request_info *info,
477                                      struct iw_point *erq, char *keybuf)
478 {
479         struct ieee80211_sub_if_data *sdata;
480         int idx, i, alg = ALG_WEP;
481         u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
482         int remove = 0, ret;
483
484         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
485
486         idx = erq->flags & IW_ENCODE_INDEX;
487         if (idx == 0) {
488                 if (sdata->default_key)
489                         for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
490                                 if (sdata->default_key == sdata->keys[i]) {
491                                         idx = i;
492                                         break;
493                                 }
494                         }
495         } else if (idx < 1 || idx > 4)
496                 return -EINVAL;
497         else
498                 idx--;
499
500         if (erq->flags & IW_ENCODE_DISABLED)
501                 remove = 1;
502         else if (erq->length == 0) {
503                 /* No key data - just set the default TX key index */
504                 ieee80211_set_default_key(sdata, idx);
505                 return 0;
506         }
507
508         ret = ieee80211_set_encryption(
509                 sdata, bcaddr,
510                 idx, alg, remove,
511                 !sdata->default_key,
512                 keybuf, erq->length);
513
514         if (!ret && sdata->vif.type == NL80211_IFTYPE_STATION) {
515                 if (remove)
516                         sdata->u.mgd.flags &= ~IEEE80211_STA_TKIP_WEP_USED;
517                 else
518                         sdata->u.mgd.flags |= IEEE80211_STA_TKIP_WEP_USED;
519         }
520
521         return ret;
522 }
523
524
525 static int ieee80211_ioctl_giwencode(struct net_device *dev,
526                                      struct iw_request_info *info,
527                                      struct iw_point *erq, char *key)
528 {
529         struct ieee80211_sub_if_data *sdata;
530         int idx, i;
531
532         sdata = IEEE80211_DEV_TO_SUB_IF(dev);
533
534         idx = erq->flags & IW_ENCODE_INDEX;
535         if (idx < 1 || idx > 4) {
536                 idx = -1;
537                 if (!sdata->default_key)
538                         idx = 0;
539                 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
540                         if (sdata->default_key == sdata->keys[i]) {
541                                 idx = i;
542                                 break;
543                         }
544                 }
545                 if (idx < 0)
546                         return -EINVAL;
547         } else
548                 idx--;
549
550         erq->flags = idx + 1;
551
552         if (!sdata->keys[idx]) {
553                 erq->length = 0;
554                 erq->flags |= IW_ENCODE_DISABLED;
555                 return 0;
556         }
557
558         memcpy(key, sdata->keys[idx]->conf.key,
559                min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
560         erq->length = sdata->keys[idx]->conf.keylen;
561         erq->flags |= IW_ENCODE_ENABLED;
562
563         if (sdata->vif.type == NL80211_IFTYPE_STATION) {
564                 switch (sdata->u.mgd.auth_alg) {
565                 case WLAN_AUTH_OPEN:
566                 case WLAN_AUTH_LEAP:
567                         erq->flags |= IW_ENCODE_OPEN;
568                         break;
569                 case WLAN_AUTH_SHARED_KEY:
570                         erq->flags |= IW_ENCODE_RESTRICTED;
571                         break;
572                 }
573         }
574
575         return 0;
576 }
577
578 static int ieee80211_ioctl_siwpower(struct net_device *dev,
579                                     struct iw_request_info *info,
580                                     struct iw_param *wrq,
581                                     char *extra)
582 {
583         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
584         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
585         struct ieee80211_conf *conf = &local->hw.conf;
586         int timeout = 0;
587         bool ps;
588
589         if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
590                 return -EOPNOTSUPP;
591
592         if (sdata->vif.type != NL80211_IFTYPE_STATION)
593                 return -EINVAL;
594
595         if (wrq->disabled) {
596                 ps = false;
597                 timeout = 0;
598                 goto set;
599         }
600
601         switch (wrq->flags & IW_POWER_MODE) {
602         case IW_POWER_ON:       /* If not specified */
603         case IW_POWER_MODE:     /* If set all mask */
604         case IW_POWER_ALL_R:    /* If explicitely state all */
605                 ps = true;
606                 break;
607         default:                /* Otherwise we ignore */
608                 return -EINVAL;
609         }
610
611         if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
612                 return -EINVAL;
613
614         if (wrq->flags & IW_POWER_TIMEOUT)
615                 timeout = wrq->value / 1000;
616
617  set:
618         if (ps == sdata->u.mgd.powersave && timeout == conf->dynamic_ps_timeout)
619                 return 0;
620
621         sdata->u.mgd.powersave = ps;
622         conf->dynamic_ps_timeout = timeout;
623
624         if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
625                 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
626
627         ieee80211_recalc_ps(local, -1);
628
629         return 0;
630 }
631
632 static int ieee80211_ioctl_giwpower(struct net_device *dev,
633                                     struct iw_request_info *info,
634                                     union iwreq_data *wrqu,
635                                     char *extra)
636 {
637         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
638
639         wrqu->power.disabled = !sdata->u.mgd.powersave;
640
641         return 0;
642 }
643
644 static int ieee80211_ioctl_siwauth(struct net_device *dev,
645                                    struct iw_request_info *info,
646                                    struct iw_param *data, char *extra)
647 {
648         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
649         int ret = 0;
650
651         switch (data->flags & IW_AUTH_INDEX) {
652         case IW_AUTH_WPA_VERSION:
653         case IW_AUTH_CIPHER_GROUP:
654         case IW_AUTH_WPA_ENABLED:
655         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
656         case IW_AUTH_KEY_MGMT:
657         case IW_AUTH_CIPHER_GROUP_MGMT:
658                 break;
659         case IW_AUTH_CIPHER_PAIRWISE:
660                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
661                         if (data->value & (IW_AUTH_CIPHER_WEP40 |
662                             IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_TKIP))
663                                 sdata->u.mgd.flags |=
664                                         IEEE80211_STA_TKIP_WEP_USED;
665                         else
666                                 sdata->u.mgd.flags &=
667                                         ~IEEE80211_STA_TKIP_WEP_USED;
668                 }
669                 break;
670         case IW_AUTH_DROP_UNENCRYPTED:
671                 sdata->drop_unencrypted = !!data->value;
672                 break;
673         case IW_AUTH_PRIVACY_INVOKED:
674                 if (sdata->vif.type != NL80211_IFTYPE_STATION)
675                         ret = -EINVAL;
676                 else {
677                         sdata->u.mgd.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
678                         /*
679                          * Privacy invoked by wpa_supplicant, store the
680                          * value and allow associating to a protected
681                          * network without having a key up front.
682                          */
683                         if (data->value)
684                                 sdata->u.mgd.flags |=
685                                         IEEE80211_STA_PRIVACY_INVOKED;
686                 }
687                 break;
688         case IW_AUTH_80211_AUTH_ALG:
689                 if (sdata->vif.type == NL80211_IFTYPE_STATION)
690                         sdata->u.mgd.auth_algs = data->value;
691                 else
692                         ret = -EOPNOTSUPP;
693                 break;
694         case IW_AUTH_MFP:
695                 if (!(sdata->local->hw.flags & IEEE80211_HW_MFP_CAPABLE)) {
696                         ret = -EOPNOTSUPP;
697                         break;
698                 }
699                 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
700                         switch (data->value) {
701                         case IW_AUTH_MFP_DISABLED:
702                                 sdata->u.mgd.mfp = IEEE80211_MFP_DISABLED;
703                                 break;
704                         case IW_AUTH_MFP_OPTIONAL:
705                                 sdata->u.mgd.mfp = IEEE80211_MFP_OPTIONAL;
706                                 break;
707                         case IW_AUTH_MFP_REQUIRED:
708                                 sdata->u.mgd.mfp = IEEE80211_MFP_REQUIRED;
709                                 break;
710                         default:
711                                 ret = -EINVAL;
712                         }
713                 } else
714                         ret = -EOPNOTSUPP;
715                 break;
716         default:
717                 ret = -EOPNOTSUPP;
718                 break;
719         }
720         return ret;
721 }
722
723 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
724 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
725 {
726         struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
727         struct iw_statistics *wstats = &local->wstats;
728         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
729         struct sta_info *sta = NULL;
730
731         rcu_read_lock();
732
733         if (sdata->vif.type == NL80211_IFTYPE_STATION)
734                 sta = sta_info_get(local, sdata->u.mgd.bssid);
735
736         if (!sta) {
737                 wstats->discard.fragment = 0;
738                 wstats->discard.misc = 0;
739                 wstats->qual.qual = 0;
740                 wstats->qual.level = 0;
741                 wstats->qual.noise = 0;
742                 wstats->qual.updated = IW_QUAL_ALL_INVALID;
743         } else {
744                 wstats->qual.updated = 0;
745                 /*
746                  * mirror what cfg80211 does for iwrange/scan results,
747                  * otherwise userspace gets confused.
748                  */
749                 if (local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
750                                        IEEE80211_HW_SIGNAL_DBM)) {
751                         wstats->qual.updated |= IW_QUAL_LEVEL_UPDATED;
752                         wstats->qual.updated |= IW_QUAL_QUAL_UPDATED;
753                 } else {
754                         wstats->qual.updated |= IW_QUAL_LEVEL_INVALID;
755                         wstats->qual.updated |= IW_QUAL_QUAL_INVALID;
756                 }
757
758                 if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
759                         wstats->qual.level = sta->last_signal;
760                         wstats->qual.qual = sta->last_signal;
761                 } else if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
762                         int sig = sta->last_signal;
763
764                         wstats->qual.updated |= IW_QUAL_DBM;
765                         wstats->qual.level = sig;
766                         if (sig < -110)
767                                 sig = -110;
768                         else if (sig > -40)
769                                 sig = -40;
770                         wstats->qual.qual = sig + 110;
771                 }
772
773                 if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
774                         /*
775                          * This assumes that if driver reports noise, it also
776                          * reports signal in dBm.
777                          */
778                         wstats->qual.noise = sta->last_noise;
779                         wstats->qual.updated |= IW_QUAL_NOISE_UPDATED;
780                 } else {
781                         wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
782                 }
783         }
784
785         rcu_read_unlock();
786
787         return wstats;
788 }
789
790 static int ieee80211_ioctl_giwauth(struct net_device *dev,
791                                    struct iw_request_info *info,
792                                    struct iw_param *data, char *extra)
793 {
794         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
795         int ret = 0;
796
797         switch (data->flags & IW_AUTH_INDEX) {
798         case IW_AUTH_80211_AUTH_ALG:
799                 if (sdata->vif.type == NL80211_IFTYPE_STATION)
800                         data->value = sdata->u.mgd.auth_algs;
801                 else
802                         ret = -EOPNOTSUPP;
803                 break;
804         default:
805                 ret = -EOPNOTSUPP;
806                 break;
807         }
808         return ret;
809 }
810
811
812 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
813                                         struct iw_request_info *info,
814                                         struct iw_point *erq, char *extra)
815 {
816         struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
817         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
818         int uninitialized_var(alg), idx, i, remove = 0;
819
820         switch (ext->alg) {
821         case IW_ENCODE_ALG_NONE:
822                 remove = 1;
823                 break;
824         case IW_ENCODE_ALG_WEP:
825                 alg = ALG_WEP;
826                 break;
827         case IW_ENCODE_ALG_TKIP:
828                 alg = ALG_TKIP;
829                 break;
830         case IW_ENCODE_ALG_CCMP:
831                 alg = ALG_CCMP;
832                 break;
833         case IW_ENCODE_ALG_AES_CMAC:
834                 alg = ALG_AES_CMAC;
835                 break;
836         default:
837                 return -EOPNOTSUPP;
838         }
839
840         if (erq->flags & IW_ENCODE_DISABLED)
841                 remove = 1;
842
843         idx = erq->flags & IW_ENCODE_INDEX;
844         if (alg == ALG_AES_CMAC) {
845                 if (idx < NUM_DEFAULT_KEYS + 1 ||
846                     idx > NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) {
847                         idx = -1;
848                         if (!sdata->default_mgmt_key)
849                                 idx = 0;
850                         else for (i = NUM_DEFAULT_KEYS;
851                                   i < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
852                                   i++) {
853                                 if (sdata->default_mgmt_key == sdata->keys[i])
854                                 {
855                                         idx = i;
856                                         break;
857                                 }
858                         }
859                         if (idx < 0)
860                                 return -EINVAL;
861                 } else
862                         idx--;
863         } else {
864                 if (idx < 1 || idx > 4) {
865                         idx = -1;
866                         if (!sdata->default_key)
867                                 idx = 0;
868                         else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
869                                 if (sdata->default_key == sdata->keys[i]) {
870                                         idx = i;
871                                         break;
872                                 }
873                         }
874                         if (idx < 0)
875                                 return -EINVAL;
876                 } else
877                         idx--;
878         }
879
880         return ieee80211_set_encryption(sdata, ext->addr.sa_data, idx, alg,
881                                         remove,
882                                         ext->ext_flags &
883                                         IW_ENCODE_EXT_SET_TX_KEY,
884                                         ext->key, ext->key_len);
885 }
886
887
888 /* Structures to export the Wireless Handlers */
889
890 static const iw_handler ieee80211_handler[] =
891 {
892         (iw_handler) NULL,                              /* SIOCSIWCOMMIT */
893         (iw_handler) cfg80211_wext_giwname,             /* SIOCGIWNAME */
894         (iw_handler) NULL,                              /* SIOCSIWNWID */
895         (iw_handler) NULL,                              /* SIOCGIWNWID */
896         (iw_handler) ieee80211_ioctl_siwfreq,           /* SIOCSIWFREQ */
897         (iw_handler) ieee80211_ioctl_giwfreq,           /* SIOCGIWFREQ */
898         (iw_handler) cfg80211_wext_siwmode,             /* SIOCSIWMODE */
899         (iw_handler) cfg80211_wext_giwmode,             /* SIOCGIWMODE */
900         (iw_handler) NULL,                              /* SIOCSIWSENS */
901         (iw_handler) NULL,                              /* SIOCGIWSENS */
902         (iw_handler) NULL /* not used */,               /* SIOCSIWRANGE */
903         (iw_handler) cfg80211_wext_giwrange,            /* SIOCGIWRANGE */
904         (iw_handler) NULL /* not used */,               /* SIOCSIWPRIV */
905         (iw_handler) NULL /* kernel code */,            /* SIOCGIWPRIV */
906         (iw_handler) NULL /* not used */,               /* SIOCSIWSTATS */
907         (iw_handler) NULL /* kernel code */,            /* SIOCGIWSTATS */
908         (iw_handler) NULL,                              /* SIOCSIWSPY */
909         (iw_handler) NULL,                              /* SIOCGIWSPY */
910         (iw_handler) NULL,                              /* SIOCSIWTHRSPY */
911         (iw_handler) NULL,                              /* SIOCGIWTHRSPY */
912         (iw_handler) ieee80211_ioctl_siwap,             /* SIOCSIWAP */
913         (iw_handler) ieee80211_ioctl_giwap,             /* SIOCGIWAP */
914         (iw_handler) cfg80211_wext_siwmlme,             /* SIOCSIWMLME */
915         (iw_handler) NULL,                              /* SIOCGIWAPLIST */
916         (iw_handler) cfg80211_wext_siwscan,             /* SIOCSIWSCAN */
917         (iw_handler) cfg80211_wext_giwscan,             /* SIOCGIWSCAN */
918         (iw_handler) ieee80211_ioctl_siwessid,          /* SIOCSIWESSID */
919         (iw_handler) ieee80211_ioctl_giwessid,          /* SIOCGIWESSID */
920         (iw_handler) NULL,                              /* SIOCSIWNICKN */
921         (iw_handler) NULL,                              /* SIOCGIWNICKN */
922         (iw_handler) NULL,                              /* -- hole -- */
923         (iw_handler) NULL,                              /* -- hole -- */
924         (iw_handler) ieee80211_ioctl_siwrate,           /* SIOCSIWRATE */
925         (iw_handler) ieee80211_ioctl_giwrate,           /* SIOCGIWRATE */
926         (iw_handler) cfg80211_wext_siwrts,              /* SIOCSIWRTS */
927         (iw_handler) cfg80211_wext_giwrts,              /* SIOCGIWRTS */
928         (iw_handler) cfg80211_wext_siwfrag,             /* SIOCSIWFRAG */
929         (iw_handler) cfg80211_wext_giwfrag,             /* SIOCGIWFRAG */
930         (iw_handler) ieee80211_ioctl_siwtxpower,        /* SIOCSIWTXPOW */
931         (iw_handler) ieee80211_ioctl_giwtxpower,        /* SIOCGIWTXPOW */
932         (iw_handler) cfg80211_wext_siwretry,            /* SIOCSIWRETRY */
933         (iw_handler) cfg80211_wext_giwretry,            /* SIOCGIWRETRY */
934         (iw_handler) ieee80211_ioctl_siwencode,         /* SIOCSIWENCODE */
935         (iw_handler) ieee80211_ioctl_giwencode,         /* SIOCGIWENCODE */
936         (iw_handler) ieee80211_ioctl_siwpower,          /* SIOCSIWPOWER */
937         (iw_handler) ieee80211_ioctl_giwpower,          /* SIOCGIWPOWER */
938         (iw_handler) NULL,                              /* -- hole -- */
939         (iw_handler) NULL,                              /* -- hole -- */
940         (iw_handler) ieee80211_ioctl_siwgenie,          /* SIOCSIWGENIE */
941         (iw_handler) NULL,                              /* SIOCGIWGENIE */
942         (iw_handler) ieee80211_ioctl_siwauth,           /* SIOCSIWAUTH */
943         (iw_handler) ieee80211_ioctl_giwauth,           /* SIOCGIWAUTH */
944         (iw_handler) ieee80211_ioctl_siwencodeext,      /* SIOCSIWENCODEEXT */
945         (iw_handler) NULL,                              /* SIOCGIWENCODEEXT */
946         (iw_handler) NULL,                              /* SIOCSIWPMKSA */
947         (iw_handler) NULL,                              /* -- hole -- */
948 };
949
950 const struct iw_handler_def ieee80211_iw_handler_def =
951 {
952         .num_standard   = ARRAY_SIZE(ieee80211_handler),
953         .standard       = (iw_handler *) ieee80211_handler,
954         .get_wireless_stats = ieee80211_get_wireless_stats,
955 };