Merge branch 'for-3.2' of git://linux-nfs.org/~bfields/linux
[pandora-kernel.git] / net / wireless / wext-compat.c
1 /*
2  * cfg80211 - wext compat code
3  *
4  * This is temporary code until all wireless functionality is migrated
5  * into cfg80211, when that happens all the exports here go away and
6  * we directly assign the wireless handlers of wireless interfaces.
7  *
8  * Copyright 2008-2009  Johannes Berg <johannes@sipsolutions.net>
9  */
10
11 #include <linux/wireless.h>
12 #include <linux/nl80211.h>
13 #include <linux/if_arp.h>
14 #include <linux/etherdevice.h>
15 #include <linux/slab.h>
16 #include <net/iw_handler.h>
17 #include <net/cfg80211.h>
18 #include <net/cfg80211-wext.h>
19 #include "wext-compat.h"
20 #include "core.h"
21
22 int cfg80211_wext_giwname(struct net_device *dev,
23                           struct iw_request_info *info,
24                           char *name, char *extra)
25 {
26         struct wireless_dev *wdev = dev->ieee80211_ptr;
27         struct ieee80211_supported_band *sband;
28         bool is_ht = false, is_a = false, is_b = false, is_g = false;
29
30         if (!wdev)
31                 return -EOPNOTSUPP;
32
33         sband = wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
34         if (sband) {
35                 is_a = true;
36                 is_ht |= sband->ht_cap.ht_supported;
37         }
38
39         sband = wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
40         if (sband) {
41                 int i;
42                 /* Check for mandatory rates */
43                 for (i = 0; i < sband->n_bitrates; i++) {
44                         if (sband->bitrates[i].bitrate == 10)
45                                 is_b = true;
46                         if (sband->bitrates[i].bitrate == 60)
47                                 is_g = true;
48                 }
49                 is_ht |= sband->ht_cap.ht_supported;
50         }
51
52         strcpy(name, "IEEE 802.11");
53         if (is_a)
54                 strcat(name, "a");
55         if (is_b)
56                 strcat(name, "b");
57         if (is_g)
58                 strcat(name, "g");
59         if (is_ht)
60                 strcat(name, "n");
61
62         return 0;
63 }
64 EXPORT_SYMBOL_GPL(cfg80211_wext_giwname);
65
66 int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
67                           u32 *mode, char *extra)
68 {
69         struct wireless_dev *wdev = dev->ieee80211_ptr;
70         struct cfg80211_registered_device *rdev;
71         struct vif_params vifparams;
72         enum nl80211_iftype type;
73         int ret;
74
75         rdev = wiphy_to_dev(wdev->wiphy);
76
77         switch (*mode) {
78         case IW_MODE_INFRA:
79                 type = NL80211_IFTYPE_STATION;
80                 break;
81         case IW_MODE_ADHOC:
82                 type = NL80211_IFTYPE_ADHOC;
83                 break;
84         case IW_MODE_REPEAT:
85                 type = NL80211_IFTYPE_WDS;
86                 break;
87         case IW_MODE_MONITOR:
88                 type = NL80211_IFTYPE_MONITOR;
89                 break;
90         default:
91                 return -EINVAL;
92         }
93
94         if (type == wdev->iftype)
95                 return 0;
96
97         memset(&vifparams, 0, sizeof(vifparams));
98
99         cfg80211_lock_rdev(rdev);
100         ret = cfg80211_change_iface(rdev, dev, type, NULL, &vifparams);
101         cfg80211_unlock_rdev(rdev);
102
103         return ret;
104 }
105 EXPORT_SYMBOL_GPL(cfg80211_wext_siwmode);
106
107 int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
108                           u32 *mode, char *extra)
109 {
110         struct wireless_dev *wdev = dev->ieee80211_ptr;
111
112         if (!wdev)
113                 return -EOPNOTSUPP;
114
115         switch (wdev->iftype) {
116         case NL80211_IFTYPE_AP:
117                 *mode = IW_MODE_MASTER;
118                 break;
119         case NL80211_IFTYPE_STATION:
120                 *mode = IW_MODE_INFRA;
121                 break;
122         case NL80211_IFTYPE_ADHOC:
123                 *mode = IW_MODE_ADHOC;
124                 break;
125         case NL80211_IFTYPE_MONITOR:
126                 *mode = IW_MODE_MONITOR;
127                 break;
128         case NL80211_IFTYPE_WDS:
129                 *mode = IW_MODE_REPEAT;
130                 break;
131         case NL80211_IFTYPE_AP_VLAN:
132                 *mode = IW_MODE_SECOND;         /* FIXME */
133                 break;
134         default:
135                 *mode = IW_MODE_AUTO;
136                 break;
137         }
138         return 0;
139 }
140 EXPORT_SYMBOL_GPL(cfg80211_wext_giwmode);
141
142
143 int cfg80211_wext_giwrange(struct net_device *dev,
144                            struct iw_request_info *info,
145                            struct iw_point *data, char *extra)
146 {
147         struct wireless_dev *wdev = dev->ieee80211_ptr;
148         struct iw_range *range = (struct iw_range *) extra;
149         enum ieee80211_band band;
150         int i, c = 0;
151
152         if (!wdev)
153                 return -EOPNOTSUPP;
154
155         data->length = sizeof(struct iw_range);
156         memset(range, 0, sizeof(struct iw_range));
157
158         range->we_version_compiled = WIRELESS_EXT;
159         range->we_version_source = 21;
160         range->retry_capa = IW_RETRY_LIMIT;
161         range->retry_flags = IW_RETRY_LIMIT;
162         range->min_retry = 0;
163         range->max_retry = 255;
164         range->min_rts = 0;
165         range->max_rts = 2347;
166         range->min_frag = 256;
167         range->max_frag = 2346;
168
169         range->max_encoding_tokens = 4;
170
171         range->max_qual.updated = IW_QUAL_NOISE_INVALID;
172
173         switch (wdev->wiphy->signal_type) {
174         case CFG80211_SIGNAL_TYPE_NONE:
175                 break;
176         case CFG80211_SIGNAL_TYPE_MBM:
177                 range->max_qual.level = -110;
178                 range->max_qual.qual = 70;
179                 range->avg_qual.qual = 35;
180                 range->max_qual.updated |= IW_QUAL_DBM;
181                 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
182                 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
183                 break;
184         case CFG80211_SIGNAL_TYPE_UNSPEC:
185                 range->max_qual.level = 100;
186                 range->max_qual.qual = 100;
187                 range->avg_qual.qual = 50;
188                 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
189                 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
190                 break;
191         }
192
193         range->avg_qual.level = range->max_qual.level / 2;
194         range->avg_qual.noise = range->max_qual.noise / 2;
195         range->avg_qual.updated = range->max_qual.updated;
196
197         for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
198                 switch (wdev->wiphy->cipher_suites[i]) {
199                 case WLAN_CIPHER_SUITE_TKIP:
200                         range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
201                                             IW_ENC_CAPA_WPA);
202                         break;
203
204                 case WLAN_CIPHER_SUITE_CCMP:
205                         range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
206                                             IW_ENC_CAPA_WPA2);
207                         break;
208
209                 case WLAN_CIPHER_SUITE_WEP40:
210                         range->encoding_size[range->num_encoding_sizes++] =
211                                 WLAN_KEY_LEN_WEP40;
212                         break;
213
214                 case WLAN_CIPHER_SUITE_WEP104:
215                         range->encoding_size[range->num_encoding_sizes++] =
216                                 WLAN_KEY_LEN_WEP104;
217                         break;
218                 }
219         }
220
221         for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
222                 struct ieee80211_supported_band *sband;
223
224                 sband = wdev->wiphy->bands[band];
225
226                 if (!sband)
227                         continue;
228
229                 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
230                         struct ieee80211_channel *chan = &sband->channels[i];
231
232                         if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
233                                 range->freq[c].i =
234                                         ieee80211_frequency_to_channel(
235                                                 chan->center_freq);
236                                 range->freq[c].m = chan->center_freq;
237                                 range->freq[c].e = 6;
238                                 c++;
239                         }
240                 }
241         }
242         range->num_channels = c;
243         range->num_frequency = c;
244
245         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
246         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
247         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
248
249         if (wdev->wiphy->max_scan_ssids > 0)
250                 range->scan_capa |= IW_SCAN_CAPA_ESSID;
251
252         return 0;
253 }
254 EXPORT_SYMBOL_GPL(cfg80211_wext_giwrange);
255
256
257 /**
258  * cfg80211_wext_freq - get wext frequency for non-"auto"
259  * @wiphy: the wiphy
260  * @freq: the wext freq encoding
261  *
262  * Returns a frequency, or a negative error code, or 0 for auto.
263  */
264 int cfg80211_wext_freq(struct wiphy *wiphy, struct iw_freq *freq)
265 {
266         /*
267          * Parse frequency - return 0 for auto and
268          * -EINVAL for impossible things.
269          */
270         if (freq->e == 0) {
271                 enum ieee80211_band band = IEEE80211_BAND_2GHZ;
272                 if (freq->m < 0)
273                         return 0;
274                 if (freq->m > 14)
275                         band = IEEE80211_BAND_5GHZ;
276                 return ieee80211_channel_to_frequency(freq->m, band);
277         } else {
278                 int i, div = 1000000;
279                 for (i = 0; i < freq->e; i++)
280                         div /= 10;
281                 if (div <= 0)
282                         return -EINVAL;
283                 return freq->m / div;
284         }
285 }
286
287 int cfg80211_wext_siwrts(struct net_device *dev,
288                          struct iw_request_info *info,
289                          struct iw_param *rts, char *extra)
290 {
291         struct wireless_dev *wdev = dev->ieee80211_ptr;
292         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
293         u32 orts = wdev->wiphy->rts_threshold;
294         int err;
295
296         if (rts->disabled || !rts->fixed)
297                 wdev->wiphy->rts_threshold = (u32) -1;
298         else if (rts->value < 0)
299                 return -EINVAL;
300         else
301                 wdev->wiphy->rts_threshold = rts->value;
302
303         err = rdev->ops->set_wiphy_params(wdev->wiphy,
304                                           WIPHY_PARAM_RTS_THRESHOLD);
305         if (err)
306                 wdev->wiphy->rts_threshold = orts;
307
308         return err;
309 }
310 EXPORT_SYMBOL_GPL(cfg80211_wext_siwrts);
311
312 int cfg80211_wext_giwrts(struct net_device *dev,
313                          struct iw_request_info *info,
314                          struct iw_param *rts, char *extra)
315 {
316         struct wireless_dev *wdev = dev->ieee80211_ptr;
317
318         rts->value = wdev->wiphy->rts_threshold;
319         rts->disabled = rts->value == (u32) -1;
320         rts->fixed = 1;
321
322         return 0;
323 }
324 EXPORT_SYMBOL_GPL(cfg80211_wext_giwrts);
325
326 int cfg80211_wext_siwfrag(struct net_device *dev,
327                           struct iw_request_info *info,
328                           struct iw_param *frag, char *extra)
329 {
330         struct wireless_dev *wdev = dev->ieee80211_ptr;
331         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
332         u32 ofrag = wdev->wiphy->frag_threshold;
333         int err;
334
335         if (frag->disabled || !frag->fixed)
336                 wdev->wiphy->frag_threshold = (u32) -1;
337         else if (frag->value < 256)
338                 return -EINVAL;
339         else {
340                 /* Fragment length must be even, so strip LSB. */
341                 wdev->wiphy->frag_threshold = frag->value & ~0x1;
342         }
343
344         err = rdev->ops->set_wiphy_params(wdev->wiphy,
345                                           WIPHY_PARAM_FRAG_THRESHOLD);
346         if (err)
347                 wdev->wiphy->frag_threshold = ofrag;
348
349         return err;
350 }
351 EXPORT_SYMBOL_GPL(cfg80211_wext_siwfrag);
352
353 int cfg80211_wext_giwfrag(struct net_device *dev,
354                           struct iw_request_info *info,
355                           struct iw_param *frag, char *extra)
356 {
357         struct wireless_dev *wdev = dev->ieee80211_ptr;
358
359         frag->value = wdev->wiphy->frag_threshold;
360         frag->disabled = frag->value == (u32) -1;
361         frag->fixed = 1;
362
363         return 0;
364 }
365 EXPORT_SYMBOL_GPL(cfg80211_wext_giwfrag);
366
367 static int cfg80211_wext_siwretry(struct net_device *dev,
368                                   struct iw_request_info *info,
369                                   struct iw_param *retry, char *extra)
370 {
371         struct wireless_dev *wdev = dev->ieee80211_ptr;
372         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
373         u32 changed = 0;
374         u8 olong = wdev->wiphy->retry_long;
375         u8 oshort = wdev->wiphy->retry_short;
376         int err;
377
378         if (retry->disabled ||
379             (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
380                 return -EINVAL;
381
382         if (retry->flags & IW_RETRY_LONG) {
383                 wdev->wiphy->retry_long = retry->value;
384                 changed |= WIPHY_PARAM_RETRY_LONG;
385         } else if (retry->flags & IW_RETRY_SHORT) {
386                 wdev->wiphy->retry_short = retry->value;
387                 changed |= WIPHY_PARAM_RETRY_SHORT;
388         } else {
389                 wdev->wiphy->retry_short = retry->value;
390                 wdev->wiphy->retry_long = retry->value;
391                 changed |= WIPHY_PARAM_RETRY_LONG;
392                 changed |= WIPHY_PARAM_RETRY_SHORT;
393         }
394
395         if (!changed)
396                 return 0;
397
398         err = rdev->ops->set_wiphy_params(wdev->wiphy, changed);
399         if (err) {
400                 wdev->wiphy->retry_short = oshort;
401                 wdev->wiphy->retry_long = olong;
402         }
403
404         return err;
405 }
406
407 int cfg80211_wext_giwretry(struct net_device *dev,
408                            struct iw_request_info *info,
409                            struct iw_param *retry, char *extra)
410 {
411         struct wireless_dev *wdev = dev->ieee80211_ptr;
412
413         retry->disabled = 0;
414
415         if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
416                 /*
417                  * First return short value, iwconfig will ask long value
418                  * later if needed
419                  */
420                 retry->flags |= IW_RETRY_LIMIT;
421                 retry->value = wdev->wiphy->retry_short;
422                 if (wdev->wiphy->retry_long != wdev->wiphy->retry_short)
423                         retry->flags |= IW_RETRY_LONG;
424
425                 return 0;
426         }
427
428         if (retry->flags & IW_RETRY_LONG) {
429                 retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
430                 retry->value = wdev->wiphy->retry_long;
431         }
432
433         return 0;
434 }
435 EXPORT_SYMBOL_GPL(cfg80211_wext_giwretry);
436
437 static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
438                                      struct net_device *dev, bool pairwise,
439                                      const u8 *addr, bool remove, bool tx_key,
440                                      int idx, struct key_params *params)
441 {
442         struct wireless_dev *wdev = dev->ieee80211_ptr;
443         int err, i;
444         bool rejoin = false;
445
446         if (pairwise && !addr)
447                 return -EINVAL;
448
449         if (!wdev->wext.keys) {
450                 wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
451                                               GFP_KERNEL);
452                 if (!wdev->wext.keys)
453                         return -ENOMEM;
454                 for (i = 0; i < 6; i++)
455                         wdev->wext.keys->params[i].key =
456                                 wdev->wext.keys->data[i];
457         }
458
459         if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
460             wdev->iftype != NL80211_IFTYPE_STATION)
461                 return -EOPNOTSUPP;
462
463         if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
464                 if (!wdev->current_bss)
465                         return -ENOLINK;
466
467                 if (!rdev->ops->set_default_mgmt_key)
468                         return -EOPNOTSUPP;
469
470                 if (idx < 4 || idx > 5)
471                         return -EINVAL;
472         } else if (idx < 0 || idx > 3)
473                 return -EINVAL;
474
475         if (remove) {
476                 err = 0;
477                 if (wdev->current_bss) {
478                         /*
479                          * If removing the current TX key, we will need to
480                          * join a new IBSS without the privacy bit clear.
481                          */
482                         if (idx == wdev->wext.default_key &&
483                             wdev->iftype == NL80211_IFTYPE_ADHOC) {
484                                 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
485                                 rejoin = true;
486                         }
487
488                         if (!pairwise && addr &&
489                             !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
490                                 err = -ENOENT;
491                         else
492                                 err = rdev->ops->del_key(&rdev->wiphy, dev, idx,
493                                                          pairwise, addr);
494                 }
495                 wdev->wext.connect.privacy = false;
496                 /*
497                  * Applications using wireless extensions expect to be
498                  * able to delete keys that don't exist, so allow that.
499                  */
500                 if (err == -ENOENT)
501                         err = 0;
502                 if (!err) {
503                         if (!addr) {
504                                 wdev->wext.keys->params[idx].key_len = 0;
505                                 wdev->wext.keys->params[idx].cipher = 0;
506                         }
507                         if (idx == wdev->wext.default_key)
508                                 wdev->wext.default_key = -1;
509                         else if (idx == wdev->wext.default_mgmt_key)
510                                 wdev->wext.default_mgmt_key = -1;
511                 }
512
513                 if (!err && rejoin)
514                         err = cfg80211_ibss_wext_join(rdev, wdev);
515
516                 return err;
517         }
518
519         if (addr)
520                 tx_key = false;
521
522         if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
523                 return -EINVAL;
524
525         err = 0;
526         if (wdev->current_bss)
527                 err = rdev->ops->add_key(&rdev->wiphy, dev, idx,
528                                          pairwise, addr, params);
529         if (err)
530                 return err;
531
532         if (!addr) {
533                 wdev->wext.keys->params[idx] = *params;
534                 memcpy(wdev->wext.keys->data[idx],
535                         params->key, params->key_len);
536                 wdev->wext.keys->params[idx].key =
537                         wdev->wext.keys->data[idx];
538         }
539
540         if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
541              params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
542             (tx_key || (!addr && wdev->wext.default_key == -1))) {
543                 if (wdev->current_bss) {
544                         /*
545                          * If we are getting a new TX key from not having
546                          * had one before we need to join a new IBSS with
547                          * the privacy bit set.
548                          */
549                         if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
550                             wdev->wext.default_key == -1) {
551                                 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
552                                 rejoin = true;
553                         }
554                         err = rdev->ops->set_default_key(&rdev->wiphy, dev,
555                                                          idx, true, true);
556                 }
557                 if (!err) {
558                         wdev->wext.default_key = idx;
559                         if (rejoin)
560                                 err = cfg80211_ibss_wext_join(rdev, wdev);
561                 }
562                 return err;
563         }
564
565         if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
566             (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
567                 if (wdev->current_bss)
568                         err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
569                                                               dev, idx);
570                 if (!err)
571                         wdev->wext.default_mgmt_key = idx;
572                 return err;
573         }
574
575         return 0;
576 }
577
578 static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
579                                    struct net_device *dev, bool pairwise,
580                                    const u8 *addr, bool remove, bool tx_key,
581                                    int idx, struct key_params *params)
582 {
583         int err;
584
585         /* devlist mutex needed for possible IBSS re-join */
586         mutex_lock(&rdev->devlist_mtx);
587         wdev_lock(dev->ieee80211_ptr);
588         err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
589                                         remove, tx_key, idx, params);
590         wdev_unlock(dev->ieee80211_ptr);
591         mutex_unlock(&rdev->devlist_mtx);
592
593         return err;
594 }
595
596 static int cfg80211_wext_siwencode(struct net_device *dev,
597                                    struct iw_request_info *info,
598                                    struct iw_point *erq, char *keybuf)
599 {
600         struct wireless_dev *wdev = dev->ieee80211_ptr;
601         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
602         int idx, err;
603         bool remove = false;
604         struct key_params params;
605
606         if (wdev->iftype != NL80211_IFTYPE_STATION &&
607             wdev->iftype != NL80211_IFTYPE_ADHOC)
608                 return -EOPNOTSUPP;
609
610         /* no use -- only MFP (set_default_mgmt_key) is optional */
611         if (!rdev->ops->del_key ||
612             !rdev->ops->add_key ||
613             !rdev->ops->set_default_key)
614                 return -EOPNOTSUPP;
615
616         idx = erq->flags & IW_ENCODE_INDEX;
617         if (idx == 0) {
618                 idx = wdev->wext.default_key;
619                 if (idx < 0)
620                         idx = 0;
621         } else if (idx < 1 || idx > 4)
622                 return -EINVAL;
623         else
624                 idx--;
625
626         if (erq->flags & IW_ENCODE_DISABLED)
627                 remove = true;
628         else if (erq->length == 0) {
629                 /* No key data - just set the default TX key index */
630                 err = 0;
631                 wdev_lock(wdev);
632                 if (wdev->current_bss)
633                         err = rdev->ops->set_default_key(&rdev->wiphy, dev,
634                                                          idx, true, true);
635                 if (!err)
636                         wdev->wext.default_key = idx;
637                 wdev_unlock(wdev);
638                 return err;
639         }
640
641         memset(&params, 0, sizeof(params));
642         params.key = keybuf;
643         params.key_len = erq->length;
644         if (erq->length == 5)
645                 params.cipher = WLAN_CIPHER_SUITE_WEP40;
646         else if (erq->length == 13)
647                 params.cipher = WLAN_CIPHER_SUITE_WEP104;
648         else if (!remove)
649                 return -EINVAL;
650
651         return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
652                                        wdev->wext.default_key == -1,
653                                        idx, &params);
654 }
655
656 static int cfg80211_wext_siwencodeext(struct net_device *dev,
657                                       struct iw_request_info *info,
658                                       struct iw_point *erq, char *extra)
659 {
660         struct wireless_dev *wdev = dev->ieee80211_ptr;
661         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
662         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
663         const u8 *addr;
664         int idx;
665         bool remove = false;
666         struct key_params params;
667         u32 cipher;
668
669         if (wdev->iftype != NL80211_IFTYPE_STATION &&
670             wdev->iftype != NL80211_IFTYPE_ADHOC)
671                 return -EOPNOTSUPP;
672
673         /* no use -- only MFP (set_default_mgmt_key) is optional */
674         if (!rdev->ops->del_key ||
675             !rdev->ops->add_key ||
676             !rdev->ops->set_default_key)
677                 return -EOPNOTSUPP;
678
679         switch (ext->alg) {
680         case IW_ENCODE_ALG_NONE:
681                 remove = true;
682                 cipher = 0;
683                 break;
684         case IW_ENCODE_ALG_WEP:
685                 if (ext->key_len == 5)
686                         cipher = WLAN_CIPHER_SUITE_WEP40;
687                 else if (ext->key_len == 13)
688                         cipher = WLAN_CIPHER_SUITE_WEP104;
689                 else
690                         return -EINVAL;
691                 break;
692         case IW_ENCODE_ALG_TKIP:
693                 cipher = WLAN_CIPHER_SUITE_TKIP;
694                 break;
695         case IW_ENCODE_ALG_CCMP:
696                 cipher = WLAN_CIPHER_SUITE_CCMP;
697                 break;
698         case IW_ENCODE_ALG_AES_CMAC:
699                 cipher = WLAN_CIPHER_SUITE_AES_CMAC;
700                 break;
701         default:
702                 return -EOPNOTSUPP;
703         }
704
705         if (erq->flags & IW_ENCODE_DISABLED)
706                 remove = true;
707
708         idx = erq->flags & IW_ENCODE_INDEX;
709         if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
710                 if (idx < 4 || idx > 5) {
711                         idx = wdev->wext.default_mgmt_key;
712                         if (idx < 0)
713                                 return -EINVAL;
714                 } else
715                         idx--;
716         } else {
717                 if (idx < 1 || idx > 4) {
718                         idx = wdev->wext.default_key;
719                         if (idx < 0)
720                                 return -EINVAL;
721                 } else
722                         idx--;
723         }
724
725         addr = ext->addr.sa_data;
726         if (is_broadcast_ether_addr(addr))
727                 addr = NULL;
728
729         memset(&params, 0, sizeof(params));
730         params.key = ext->key;
731         params.key_len = ext->key_len;
732         params.cipher = cipher;
733
734         if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
735                 params.seq = ext->rx_seq;
736                 params.seq_len = 6;
737         }
738
739         return cfg80211_set_encryption(
740                         rdev, dev,
741                         !(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
742                         addr, remove,
743                         ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
744                         idx, &params);
745 }
746
747 static int cfg80211_wext_giwencode(struct net_device *dev,
748                                    struct iw_request_info *info,
749                                    struct iw_point *erq, char *keybuf)
750 {
751         struct wireless_dev *wdev = dev->ieee80211_ptr;
752         int idx;
753
754         if (wdev->iftype != NL80211_IFTYPE_STATION &&
755             wdev->iftype != NL80211_IFTYPE_ADHOC)
756                 return -EOPNOTSUPP;
757
758         idx = erq->flags & IW_ENCODE_INDEX;
759         if (idx == 0) {
760                 idx = wdev->wext.default_key;
761                 if (idx < 0)
762                         idx = 0;
763         } else if (idx < 1 || idx > 4)
764                 return -EINVAL;
765         else
766                 idx--;
767
768         erq->flags = idx + 1;
769
770         if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
771                 erq->flags |= IW_ENCODE_DISABLED;
772                 erq->length = 0;
773                 return 0;
774         }
775
776         erq->length = min_t(size_t, erq->length,
777                             wdev->wext.keys->params[idx].key_len);
778         memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
779         erq->flags |= IW_ENCODE_ENABLED;
780
781         return 0;
782 }
783
784 static int cfg80211_wext_siwfreq(struct net_device *dev,
785                                  struct iw_request_info *info,
786                                  struct iw_freq *wextfreq, char *extra)
787 {
788         struct wireless_dev *wdev = dev->ieee80211_ptr;
789         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
790         int freq, err;
791
792         switch (wdev->iftype) {
793         case NL80211_IFTYPE_STATION:
794                 return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
795         case NL80211_IFTYPE_ADHOC:
796                 return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
797         case NL80211_IFTYPE_MONITOR:
798         case NL80211_IFTYPE_WDS:
799         case NL80211_IFTYPE_MESH_POINT:
800                 freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
801                 if (freq < 0)
802                         return freq;
803                 if (freq == 0)
804                         return -EINVAL;
805                 mutex_lock(&rdev->devlist_mtx);
806                 wdev_lock(wdev);
807                 err = cfg80211_set_freq(rdev, wdev, freq, NL80211_CHAN_NO_HT);
808                 wdev_unlock(wdev);
809                 mutex_unlock(&rdev->devlist_mtx);
810                 return err;
811         default:
812                 return -EOPNOTSUPP;
813         }
814 }
815
816 static int cfg80211_wext_giwfreq(struct net_device *dev,
817                                  struct iw_request_info *info,
818                                  struct iw_freq *freq, char *extra)
819 {
820         struct wireless_dev *wdev = dev->ieee80211_ptr;
821
822         switch (wdev->iftype) {
823         case NL80211_IFTYPE_STATION:
824                 return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
825         case NL80211_IFTYPE_ADHOC:
826                 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
827         default:
828                 if (!wdev->channel)
829                         return -EINVAL;
830                 freq->m = wdev->channel->center_freq;
831                 freq->e = 6;
832                 return 0;
833         }
834 }
835
836 static int cfg80211_wext_siwtxpower(struct net_device *dev,
837                                     struct iw_request_info *info,
838                                     union iwreq_data *data, char *extra)
839 {
840         struct wireless_dev *wdev = dev->ieee80211_ptr;
841         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
842         enum nl80211_tx_power_setting type;
843         int dbm = 0;
844
845         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
846                 return -EINVAL;
847         if (data->txpower.flags & IW_TXPOW_RANGE)
848                 return -EINVAL;
849
850         if (!rdev->ops->set_tx_power)
851                 return -EOPNOTSUPP;
852
853         /* only change when not disabling */
854         if (!data->txpower.disabled) {
855                 rfkill_set_sw_state(rdev->rfkill, false);
856
857                 if (data->txpower.fixed) {
858                         /*
859                          * wext doesn't support negative values, see
860                          * below where it's for automatic
861                          */
862                         if (data->txpower.value < 0)
863                                 return -EINVAL;
864                         dbm = data->txpower.value;
865                         type = NL80211_TX_POWER_FIXED;
866                         /* TODO: do regulatory check! */
867                 } else {
868                         /*
869                          * Automatic power level setting, max being the value
870                          * passed in from userland.
871                          */
872                         if (data->txpower.value < 0) {
873                                 type = NL80211_TX_POWER_AUTOMATIC;
874                         } else {
875                                 dbm = data->txpower.value;
876                                 type = NL80211_TX_POWER_LIMITED;
877                         }
878                 }
879         } else {
880                 rfkill_set_sw_state(rdev->rfkill, true);
881                 schedule_work(&rdev->rfkill_sync);
882                 return 0;
883         }
884
885         return rdev->ops->set_tx_power(wdev->wiphy, type, DBM_TO_MBM(dbm));
886 }
887
888 static int cfg80211_wext_giwtxpower(struct net_device *dev,
889                                     struct iw_request_info *info,
890                                     union iwreq_data *data, char *extra)
891 {
892         struct wireless_dev *wdev = dev->ieee80211_ptr;
893         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
894         int err, val;
895
896         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
897                 return -EINVAL;
898         if (data->txpower.flags & IW_TXPOW_RANGE)
899                 return -EINVAL;
900
901         if (!rdev->ops->get_tx_power)
902                 return -EOPNOTSUPP;
903
904         err = rdev->ops->get_tx_power(wdev->wiphy, &val);
905         if (err)
906                 return err;
907
908         /* well... oh well */
909         data->txpower.fixed = 1;
910         data->txpower.disabled = rfkill_blocked(rdev->rfkill);
911         data->txpower.value = val;
912         data->txpower.flags = IW_TXPOW_DBM;
913
914         return 0;
915 }
916
917 static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
918                                  s32 auth_alg)
919 {
920         int nr_alg = 0;
921
922         if (!auth_alg)
923                 return -EINVAL;
924
925         if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
926                          IW_AUTH_ALG_SHARED_KEY |
927                          IW_AUTH_ALG_LEAP))
928                 return -EINVAL;
929
930         if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
931                 nr_alg++;
932                 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
933         }
934
935         if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
936                 nr_alg++;
937                 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
938         }
939
940         if (auth_alg & IW_AUTH_ALG_LEAP) {
941                 nr_alg++;
942                 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
943         }
944
945         if (nr_alg > 1)
946                 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
947
948         return 0;
949 }
950
951 static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
952 {
953         if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
954                              IW_AUTH_WPA_VERSION_WPA2|
955                              IW_AUTH_WPA_VERSION_DISABLED))
956                 return -EINVAL;
957
958         if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
959             (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
960                              IW_AUTH_WPA_VERSION_WPA2)))
961                 return -EINVAL;
962
963         if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
964                 wdev->wext.connect.crypto.wpa_versions &=
965                         ~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
966
967         if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
968                 wdev->wext.connect.crypto.wpa_versions |=
969                         NL80211_WPA_VERSION_1;
970
971         if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
972                 wdev->wext.connect.crypto.wpa_versions |=
973                         NL80211_WPA_VERSION_2;
974
975         return 0;
976 }
977
978 static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
979 {
980         if (cipher & IW_AUTH_CIPHER_WEP40)
981                 wdev->wext.connect.crypto.cipher_group =
982                         WLAN_CIPHER_SUITE_WEP40;
983         else if (cipher & IW_AUTH_CIPHER_WEP104)
984                 wdev->wext.connect.crypto.cipher_group =
985                         WLAN_CIPHER_SUITE_WEP104;
986         else if (cipher & IW_AUTH_CIPHER_TKIP)
987                 wdev->wext.connect.crypto.cipher_group =
988                         WLAN_CIPHER_SUITE_TKIP;
989         else if (cipher & IW_AUTH_CIPHER_CCMP)
990                 wdev->wext.connect.crypto.cipher_group =
991                         WLAN_CIPHER_SUITE_CCMP;
992         else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
993                 wdev->wext.connect.crypto.cipher_group =
994                         WLAN_CIPHER_SUITE_AES_CMAC;
995         else if (cipher & IW_AUTH_CIPHER_NONE)
996                 wdev->wext.connect.crypto.cipher_group = 0;
997         else
998                 return -EINVAL;
999
1000         return 0;
1001 }
1002
1003 static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
1004 {
1005         int nr_ciphers = 0;
1006         u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
1007
1008         if (cipher & IW_AUTH_CIPHER_WEP40) {
1009                 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
1010                 nr_ciphers++;
1011         }
1012
1013         if (cipher & IW_AUTH_CIPHER_WEP104) {
1014                 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1015                 nr_ciphers++;
1016         }
1017
1018         if (cipher & IW_AUTH_CIPHER_TKIP) {
1019                 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1020                 nr_ciphers++;
1021         }
1022
1023         if (cipher & IW_AUTH_CIPHER_CCMP) {
1024                 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1025                 nr_ciphers++;
1026         }
1027
1028         if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1029                 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1030                 nr_ciphers++;
1031         }
1032
1033         BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1034
1035         wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1036
1037         return 0;
1038 }
1039
1040
1041 static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
1042 {
1043         int nr_akm_suites = 0;
1044
1045         if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1046                         IW_AUTH_KEY_MGMT_PSK))
1047                 return -EINVAL;
1048
1049         if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1050                 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1051                         WLAN_AKM_SUITE_8021X;
1052                 nr_akm_suites++;
1053         }
1054
1055         if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1056                 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1057                         WLAN_AKM_SUITE_PSK;
1058                 nr_akm_suites++;
1059         }
1060
1061         wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1062
1063         return 0;
1064 }
1065
1066 static int cfg80211_wext_siwauth(struct net_device *dev,
1067                                  struct iw_request_info *info,
1068                                  struct iw_param *data, char *extra)
1069 {
1070         struct wireless_dev *wdev = dev->ieee80211_ptr;
1071
1072         if (wdev->iftype != NL80211_IFTYPE_STATION)
1073                 return -EOPNOTSUPP;
1074
1075         switch (data->flags & IW_AUTH_INDEX) {
1076         case IW_AUTH_PRIVACY_INVOKED:
1077                 wdev->wext.connect.privacy = data->value;
1078                 return 0;
1079         case IW_AUTH_WPA_VERSION:
1080                 return cfg80211_set_wpa_version(wdev, data->value);
1081         case IW_AUTH_CIPHER_GROUP:
1082                 return cfg80211_set_cipher_group(wdev, data->value);
1083         case IW_AUTH_KEY_MGMT:
1084                 return cfg80211_set_key_mgt(wdev, data->value);
1085         case IW_AUTH_CIPHER_PAIRWISE:
1086                 return cfg80211_set_cipher_pairwise(wdev, data->value);
1087         case IW_AUTH_80211_AUTH_ALG:
1088                 return cfg80211_set_auth_alg(wdev, data->value);
1089         case IW_AUTH_WPA_ENABLED:
1090         case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1091         case IW_AUTH_DROP_UNENCRYPTED:
1092         case IW_AUTH_MFP:
1093                 return 0;
1094         default:
1095                 return -EOPNOTSUPP;
1096         }
1097 }
1098
1099 static int cfg80211_wext_giwauth(struct net_device *dev,
1100                                  struct iw_request_info *info,
1101                                  struct iw_param *data, char *extra)
1102 {
1103         /* XXX: what do we need? */
1104
1105         return -EOPNOTSUPP;
1106 }
1107
1108 static int cfg80211_wext_siwpower(struct net_device *dev,
1109                                   struct iw_request_info *info,
1110                                   struct iw_param *wrq, char *extra)
1111 {
1112         struct wireless_dev *wdev = dev->ieee80211_ptr;
1113         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1114         bool ps = wdev->ps;
1115         int timeout = wdev->ps_timeout;
1116         int err;
1117
1118         if (wdev->iftype != NL80211_IFTYPE_STATION)
1119                 return -EINVAL;
1120
1121         if (!rdev->ops->set_power_mgmt)
1122                 return -EOPNOTSUPP;
1123
1124         if (wrq->disabled) {
1125                 ps = false;
1126         } else {
1127                 switch (wrq->flags & IW_POWER_MODE) {
1128                 case IW_POWER_ON:       /* If not specified */
1129                 case IW_POWER_MODE:     /* If set all mask */
1130                 case IW_POWER_ALL_R:    /* If explicitely state all */
1131                         ps = true;
1132                         break;
1133                 default:                /* Otherwise we ignore */
1134                         return -EINVAL;
1135                 }
1136
1137                 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1138                         return -EINVAL;
1139
1140                 if (wrq->flags & IW_POWER_TIMEOUT)
1141                         timeout = wrq->value / 1000;
1142         }
1143
1144         err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, ps, timeout);
1145         if (err)
1146                 return err;
1147
1148         wdev->ps = ps;
1149         wdev->ps_timeout = timeout;
1150
1151         return 0;
1152
1153 }
1154
1155 static int cfg80211_wext_giwpower(struct net_device *dev,
1156                                   struct iw_request_info *info,
1157                                   struct iw_param *wrq, char *extra)
1158 {
1159         struct wireless_dev *wdev = dev->ieee80211_ptr;
1160
1161         wrq->disabled = !wdev->ps;
1162
1163         return 0;
1164 }
1165
1166 static int cfg80211_wds_wext_siwap(struct net_device *dev,
1167                                    struct iw_request_info *info,
1168                                    struct sockaddr *addr, char *extra)
1169 {
1170         struct wireless_dev *wdev = dev->ieee80211_ptr;
1171         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1172         int err;
1173
1174         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1175                 return -EINVAL;
1176
1177         if (addr->sa_family != ARPHRD_ETHER)
1178                 return -EINVAL;
1179
1180         if (netif_running(dev))
1181                 return -EBUSY;
1182
1183         if (!rdev->ops->set_wds_peer)
1184                 return -EOPNOTSUPP;
1185
1186         err = rdev->ops->set_wds_peer(wdev->wiphy, dev, (u8 *) &addr->sa_data);
1187         if (err)
1188                 return err;
1189
1190         memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
1191
1192         return 0;
1193 }
1194
1195 static int cfg80211_wds_wext_giwap(struct net_device *dev,
1196                                    struct iw_request_info *info,
1197                                    struct sockaddr *addr, char *extra)
1198 {
1199         struct wireless_dev *wdev = dev->ieee80211_ptr;
1200
1201         if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1202                 return -EINVAL;
1203
1204         addr->sa_family = ARPHRD_ETHER;
1205         memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
1206
1207         return 0;
1208 }
1209
1210 static int cfg80211_wext_siwrate(struct net_device *dev,
1211                                  struct iw_request_info *info,
1212                                  struct iw_param *rate, char *extra)
1213 {
1214         struct wireless_dev *wdev = dev->ieee80211_ptr;
1215         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1216         struct cfg80211_bitrate_mask mask;
1217         u32 fixed, maxrate;
1218         struct ieee80211_supported_band *sband;
1219         int band, ridx;
1220         bool match = false;
1221
1222         if (!rdev->ops->set_bitrate_mask)
1223                 return -EOPNOTSUPP;
1224
1225         memset(&mask, 0, sizeof(mask));
1226         fixed = 0;
1227         maxrate = (u32)-1;
1228
1229         if (rate->value < 0) {
1230                 /* nothing */
1231         } else if (rate->fixed) {
1232                 fixed = rate->value / 100000;
1233         } else {
1234                 maxrate = rate->value / 100000;
1235         }
1236
1237         for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1238                 sband = wdev->wiphy->bands[band];
1239                 if (sband == NULL)
1240                         continue;
1241                 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1242                         struct ieee80211_rate *srate = &sband->bitrates[ridx];
1243                         if (fixed == srate->bitrate) {
1244                                 mask.control[band].legacy = 1 << ridx;
1245                                 match = true;
1246                                 break;
1247                         }
1248                         if (srate->bitrate <= maxrate) {
1249                                 mask.control[band].legacy |= 1 << ridx;
1250                                 match = true;
1251                         }
1252                 }
1253         }
1254
1255         if (!match)
1256                 return -EINVAL;
1257
1258         return rdev->ops->set_bitrate_mask(wdev->wiphy, dev, NULL, &mask);
1259 }
1260
1261 static int cfg80211_wext_giwrate(struct net_device *dev,
1262                                  struct iw_request_info *info,
1263                                  struct iw_param *rate, char *extra)
1264 {
1265         struct wireless_dev *wdev = dev->ieee80211_ptr;
1266         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1267         /* we are under RTNL - globally locked - so can use a static struct */
1268         static struct station_info sinfo;
1269         u8 addr[ETH_ALEN];
1270         int err;
1271
1272         if (wdev->iftype != NL80211_IFTYPE_STATION)
1273                 return -EOPNOTSUPP;
1274
1275         if (!rdev->ops->get_station)
1276                 return -EOPNOTSUPP;
1277
1278         err = 0;
1279         wdev_lock(wdev);
1280         if (wdev->current_bss)
1281                 memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
1282         else
1283                 err = -EOPNOTSUPP;
1284         wdev_unlock(wdev);
1285         if (err)
1286                 return err;
1287
1288         err = rdev->ops->get_station(&rdev->wiphy, dev, addr, &sinfo);
1289         if (err)
1290                 return err;
1291
1292         if (!(sinfo.filled & STATION_INFO_TX_BITRATE))
1293                 return -EOPNOTSUPP;
1294
1295         rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
1296
1297         return 0;
1298 }
1299
1300 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
1301 static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
1302 {
1303         struct wireless_dev *wdev = dev->ieee80211_ptr;
1304         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1305         /* we are under RTNL - globally locked - so can use static structs */
1306         static struct iw_statistics wstats;
1307         static struct station_info sinfo;
1308         u8 bssid[ETH_ALEN];
1309
1310         if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1311                 return NULL;
1312
1313         if (!rdev->ops->get_station)
1314                 return NULL;
1315
1316         /* Grab BSSID of current BSS, if any */
1317         wdev_lock(wdev);
1318         if (!wdev->current_bss) {
1319                 wdev_unlock(wdev);
1320                 return NULL;
1321         }
1322         memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
1323         wdev_unlock(wdev);
1324
1325         if (rdev->ops->get_station(&rdev->wiphy, dev, bssid, &sinfo))
1326                 return NULL;
1327
1328         memset(&wstats, 0, sizeof(wstats));
1329
1330         switch (rdev->wiphy.signal_type) {
1331         case CFG80211_SIGNAL_TYPE_MBM:
1332                 if (sinfo.filled & STATION_INFO_SIGNAL) {
1333                         int sig = sinfo.signal;
1334                         wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1335                         wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1336                         wstats.qual.updated |= IW_QUAL_DBM;
1337                         wstats.qual.level = sig;
1338                         if (sig < -110)
1339                                 sig = -110;
1340                         else if (sig > -40)
1341                                 sig = -40;
1342                         wstats.qual.qual = sig + 110;
1343                         break;
1344                 }
1345         case CFG80211_SIGNAL_TYPE_UNSPEC:
1346                 if (sinfo.filled & STATION_INFO_SIGNAL) {
1347                         wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1348                         wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1349                         wstats.qual.level = sinfo.signal;
1350                         wstats.qual.qual = sinfo.signal;
1351                         break;
1352                 }
1353         default:
1354                 wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1355                 wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1356         }
1357
1358         wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1359         if (sinfo.filled & STATION_INFO_RX_DROP_MISC)
1360                 wstats.discard.misc = sinfo.rx_dropped_misc;
1361         if (sinfo.filled & STATION_INFO_TX_FAILED)
1362                 wstats.discard.retries = sinfo.tx_failed;
1363
1364         return &wstats;
1365 }
1366
1367 static int cfg80211_wext_siwap(struct net_device *dev,
1368                                struct iw_request_info *info,
1369                                struct sockaddr *ap_addr, char *extra)
1370 {
1371         struct wireless_dev *wdev = dev->ieee80211_ptr;
1372
1373         switch (wdev->iftype) {
1374         case NL80211_IFTYPE_ADHOC:
1375                 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1376         case NL80211_IFTYPE_STATION:
1377                 return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1378         case NL80211_IFTYPE_WDS:
1379                 return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
1380         default:
1381                 return -EOPNOTSUPP;
1382         }
1383 }
1384
1385 static int cfg80211_wext_giwap(struct net_device *dev,
1386                                struct iw_request_info *info,
1387                                struct sockaddr *ap_addr, char *extra)
1388 {
1389         struct wireless_dev *wdev = dev->ieee80211_ptr;
1390
1391         switch (wdev->iftype) {
1392         case NL80211_IFTYPE_ADHOC:
1393                 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1394         case NL80211_IFTYPE_STATION:
1395                 return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1396         case NL80211_IFTYPE_WDS:
1397                 return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
1398         default:
1399                 return -EOPNOTSUPP;
1400         }
1401 }
1402
1403 static int cfg80211_wext_siwessid(struct net_device *dev,
1404                                   struct iw_request_info *info,
1405                                   struct iw_point *data, char *ssid)
1406 {
1407         struct wireless_dev *wdev = dev->ieee80211_ptr;
1408
1409         switch (wdev->iftype) {
1410         case NL80211_IFTYPE_ADHOC:
1411                 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1412         case NL80211_IFTYPE_STATION:
1413                 return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1414         default:
1415                 return -EOPNOTSUPP;
1416         }
1417 }
1418
1419 static int cfg80211_wext_giwessid(struct net_device *dev,
1420                                   struct iw_request_info *info,
1421                                   struct iw_point *data, char *ssid)
1422 {
1423         struct wireless_dev *wdev = dev->ieee80211_ptr;
1424
1425         data->flags = 0;
1426         data->length = 0;
1427
1428         switch (wdev->iftype) {
1429         case NL80211_IFTYPE_ADHOC:
1430                 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1431         case NL80211_IFTYPE_STATION:
1432                 return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1433         default:
1434                 return -EOPNOTSUPP;
1435         }
1436 }
1437
1438 static int cfg80211_wext_siwpmksa(struct net_device *dev,
1439                                   struct iw_request_info *info,
1440                                   struct iw_point *data, char *extra)
1441 {
1442         struct wireless_dev *wdev = dev->ieee80211_ptr;
1443         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
1444         struct cfg80211_pmksa cfg_pmksa;
1445         struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1446
1447         memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1448
1449         if (wdev->iftype != NL80211_IFTYPE_STATION)
1450                 return -EINVAL;
1451
1452         cfg_pmksa.bssid = pmksa->bssid.sa_data;
1453         cfg_pmksa.pmkid = pmksa->pmkid;
1454
1455         switch (pmksa->cmd) {
1456         case IW_PMKSA_ADD:
1457                 if (!rdev->ops->set_pmksa)
1458                         return -EOPNOTSUPP;
1459
1460                 return rdev->ops->set_pmksa(&rdev->wiphy, dev, &cfg_pmksa);
1461
1462         case IW_PMKSA_REMOVE:
1463                 if (!rdev->ops->del_pmksa)
1464                         return -EOPNOTSUPP;
1465
1466                 return rdev->ops->del_pmksa(&rdev->wiphy, dev, &cfg_pmksa);
1467
1468         case IW_PMKSA_FLUSH:
1469                 if (!rdev->ops->flush_pmksa)
1470                         return -EOPNOTSUPP;
1471
1472                 return rdev->ops->flush_pmksa(&rdev->wiphy, dev);
1473
1474         default:
1475                 return -EOPNOTSUPP;
1476         }
1477 }
1478
1479 static const iw_handler cfg80211_handlers[] = {
1480         [IW_IOCTL_IDX(SIOCGIWNAME)]     = (iw_handler) cfg80211_wext_giwname,
1481         [IW_IOCTL_IDX(SIOCSIWFREQ)]     = (iw_handler) cfg80211_wext_siwfreq,
1482         [IW_IOCTL_IDX(SIOCGIWFREQ)]     = (iw_handler) cfg80211_wext_giwfreq,
1483         [IW_IOCTL_IDX(SIOCSIWMODE)]     = (iw_handler) cfg80211_wext_siwmode,
1484         [IW_IOCTL_IDX(SIOCGIWMODE)]     = (iw_handler) cfg80211_wext_giwmode,
1485         [IW_IOCTL_IDX(SIOCGIWRANGE)]    = (iw_handler) cfg80211_wext_giwrange,
1486         [IW_IOCTL_IDX(SIOCSIWAP)]       = (iw_handler) cfg80211_wext_siwap,
1487         [IW_IOCTL_IDX(SIOCGIWAP)]       = (iw_handler) cfg80211_wext_giwap,
1488         [IW_IOCTL_IDX(SIOCSIWMLME)]     = (iw_handler) cfg80211_wext_siwmlme,
1489         [IW_IOCTL_IDX(SIOCSIWSCAN)]     = (iw_handler) cfg80211_wext_siwscan,
1490         [IW_IOCTL_IDX(SIOCGIWSCAN)]     = (iw_handler) cfg80211_wext_giwscan,
1491         [IW_IOCTL_IDX(SIOCSIWESSID)]    = (iw_handler) cfg80211_wext_siwessid,
1492         [IW_IOCTL_IDX(SIOCGIWESSID)]    = (iw_handler) cfg80211_wext_giwessid,
1493         [IW_IOCTL_IDX(SIOCSIWRATE)]     = (iw_handler) cfg80211_wext_siwrate,
1494         [IW_IOCTL_IDX(SIOCGIWRATE)]     = (iw_handler) cfg80211_wext_giwrate,
1495         [IW_IOCTL_IDX(SIOCSIWRTS)]      = (iw_handler) cfg80211_wext_siwrts,
1496         [IW_IOCTL_IDX(SIOCGIWRTS)]      = (iw_handler) cfg80211_wext_giwrts,
1497         [IW_IOCTL_IDX(SIOCSIWFRAG)]     = (iw_handler) cfg80211_wext_siwfrag,
1498         [IW_IOCTL_IDX(SIOCGIWFRAG)]     = (iw_handler) cfg80211_wext_giwfrag,
1499         [IW_IOCTL_IDX(SIOCSIWTXPOW)]    = (iw_handler) cfg80211_wext_siwtxpower,
1500         [IW_IOCTL_IDX(SIOCGIWTXPOW)]    = (iw_handler) cfg80211_wext_giwtxpower,
1501         [IW_IOCTL_IDX(SIOCSIWRETRY)]    = (iw_handler) cfg80211_wext_siwretry,
1502         [IW_IOCTL_IDX(SIOCGIWRETRY)]    = (iw_handler) cfg80211_wext_giwretry,
1503         [IW_IOCTL_IDX(SIOCSIWENCODE)]   = (iw_handler) cfg80211_wext_siwencode,
1504         [IW_IOCTL_IDX(SIOCGIWENCODE)]   = (iw_handler) cfg80211_wext_giwencode,
1505         [IW_IOCTL_IDX(SIOCSIWPOWER)]    = (iw_handler) cfg80211_wext_siwpower,
1506         [IW_IOCTL_IDX(SIOCGIWPOWER)]    = (iw_handler) cfg80211_wext_giwpower,
1507         [IW_IOCTL_IDX(SIOCSIWGENIE)]    = (iw_handler) cfg80211_wext_siwgenie,
1508         [IW_IOCTL_IDX(SIOCSIWAUTH)]     = (iw_handler) cfg80211_wext_siwauth,
1509         [IW_IOCTL_IDX(SIOCGIWAUTH)]     = (iw_handler) cfg80211_wext_giwauth,
1510         [IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
1511         [IW_IOCTL_IDX(SIOCSIWPMKSA)]    = (iw_handler) cfg80211_wext_siwpmksa,
1512 };
1513
1514 const struct iw_handler_def cfg80211_wext_handler = {
1515         .num_standard           = ARRAY_SIZE(cfg80211_handlers),
1516         .standard               = cfg80211_handlers,
1517         .get_wireless_stats = cfg80211_wireless_stats,
1518 };