cfg80211: set WE encoding size based on available ciphers
[linux-2.6.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 <net/iw_handler.h>
16 #include <net/cfg80211.h>
17 #include "core.h"
18
19 int cfg80211_wext_giwname(struct net_device *dev,
20                           struct iw_request_info *info,
21                           char *name, char *extra)
22 {
23         struct wireless_dev *wdev = dev->ieee80211_ptr;
24         struct ieee80211_supported_band *sband;
25         bool is_ht = false, is_a = false, is_b = false, is_g = false;
26
27         if (!wdev)
28                 return -EOPNOTSUPP;
29
30         sband = wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
31         if (sband) {
32                 is_a = true;
33                 is_ht |= sband->ht_cap.ht_supported;
34         }
35
36         sband = wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
37         if (sband) {
38                 int i;
39                 /* Check for mandatory rates */
40                 for (i = 0; i < sband->n_bitrates; i++) {
41                         if (sband->bitrates[i].bitrate == 10)
42                                 is_b = true;
43                         if (sband->bitrates[i].bitrate == 60)
44                                 is_g = true;
45                 }
46                 is_ht |= sband->ht_cap.ht_supported;
47         }
48
49         strcpy(name, "IEEE 802.11");
50         if (is_a)
51                 strcat(name, "a");
52         if (is_b)
53                 strcat(name, "b");
54         if (is_g)
55                 strcat(name, "g");
56         if (is_ht)
57                 strcat(name, "n");
58
59         return 0;
60 }
61 EXPORT_SYMBOL_GPL(cfg80211_wext_giwname);
62
63 int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
64                           u32 *mode, char *extra)
65 {
66         struct wireless_dev *wdev = dev->ieee80211_ptr;
67         struct cfg80211_registered_device *rdev;
68         struct vif_params vifparams;
69         enum nl80211_iftype type;
70         int ret;
71
72         if (!wdev)
73                 return -EOPNOTSUPP;
74
75         rdev = wiphy_to_dev(wdev->wiphy);
76
77         if (!rdev->ops->change_virtual_intf)
78                 return -EOPNOTSUPP;
79
80         /* don't support changing VLANs, you just re-create them */
81         if (wdev->iftype == NL80211_IFTYPE_AP_VLAN)
82                 return -EOPNOTSUPP;
83
84         switch (*mode) {
85         case IW_MODE_INFRA:
86                 type = NL80211_IFTYPE_STATION;
87                 break;
88         case IW_MODE_ADHOC:
89                 type = NL80211_IFTYPE_ADHOC;
90                 break;
91         case IW_MODE_REPEAT:
92                 type = NL80211_IFTYPE_WDS;
93                 break;
94         case IW_MODE_MONITOR:
95                 type = NL80211_IFTYPE_MONITOR;
96                 break;
97         default:
98                 return -EINVAL;
99         }
100
101         if (type == wdev->iftype)
102                 return 0;
103
104         memset(&vifparams, 0, sizeof(vifparams));
105
106         ret = rdev->ops->change_virtual_intf(wdev->wiphy, dev, type,
107                                              NULL, &vifparams);
108         WARN_ON(!ret && wdev->iftype != type);
109
110         return ret;
111 }
112 EXPORT_SYMBOL_GPL(cfg80211_wext_siwmode);
113
114 int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
115                           u32 *mode, char *extra)
116 {
117         struct wireless_dev *wdev = dev->ieee80211_ptr;
118
119         if (!wdev)
120                 return -EOPNOTSUPP;
121
122         switch (wdev->iftype) {
123         case NL80211_IFTYPE_AP:
124                 *mode = IW_MODE_MASTER;
125                 break;
126         case NL80211_IFTYPE_STATION:
127                 *mode = IW_MODE_INFRA;
128                 break;
129         case NL80211_IFTYPE_ADHOC:
130                 *mode = IW_MODE_ADHOC;
131                 break;
132         case NL80211_IFTYPE_MONITOR:
133                 *mode = IW_MODE_MONITOR;
134                 break;
135         case NL80211_IFTYPE_WDS:
136                 *mode = IW_MODE_REPEAT;
137                 break;
138         case NL80211_IFTYPE_AP_VLAN:
139                 *mode = IW_MODE_SECOND;         /* FIXME */
140                 break;
141         default:
142                 *mode = IW_MODE_AUTO;
143                 break;
144         }
145         return 0;
146 }
147 EXPORT_SYMBOL_GPL(cfg80211_wext_giwmode);
148
149
150 int cfg80211_wext_giwrange(struct net_device *dev,
151                            struct iw_request_info *info,
152                            struct iw_point *data, char *extra)
153 {
154         struct wireless_dev *wdev = dev->ieee80211_ptr;
155         struct iw_range *range = (struct iw_range *) extra;
156         enum ieee80211_band band;
157         int c = 0;
158
159         if (!wdev)
160                 return -EOPNOTSUPP;
161
162         data->length = sizeof(struct iw_range);
163         memset(range, 0, sizeof(struct iw_range));
164
165         range->we_version_compiled = WIRELESS_EXT;
166         range->we_version_source = 21;
167         range->retry_capa = IW_RETRY_LIMIT;
168         range->retry_flags = IW_RETRY_LIMIT;
169         range->min_retry = 0;
170         range->max_retry = 255;
171         range->min_rts = 0;
172         range->max_rts = 2347;
173         range->min_frag = 256;
174         range->max_frag = 2346;
175
176         range->max_encoding_tokens = 4;
177
178         range->max_qual.updated = IW_QUAL_NOISE_INVALID;
179
180         switch (wdev->wiphy->signal_type) {
181         case CFG80211_SIGNAL_TYPE_NONE:
182                 break;
183         case CFG80211_SIGNAL_TYPE_MBM:
184                 range->max_qual.level = -110;
185                 range->max_qual.qual = 70;
186                 range->avg_qual.qual = 35;
187                 range->max_qual.updated |= IW_QUAL_DBM;
188                 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
189                 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
190                 break;
191         case CFG80211_SIGNAL_TYPE_UNSPEC:
192                 range->max_qual.level = 100;
193                 range->max_qual.qual = 100;
194                 range->avg_qual.qual = 50;
195                 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
196                 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
197                 break;
198         }
199
200         range->avg_qual.level = range->max_qual.level / 2;
201         range->avg_qual.noise = range->max_qual.noise / 2;
202         range->avg_qual.updated = range->max_qual.updated;
203
204         range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2;
205
206         for (c = 0; c < wdev->wiphy->n_cipher_suites; c++) {
207                 switch (wdev->wiphy->cipher_suites[c]) {
208                 case WLAN_CIPHER_SUITE_TKIP:
209                         range->enc_capa |= IW_ENC_CAPA_CIPHER_TKIP;
210                         break;
211
212                 case WLAN_CIPHER_SUITE_CCMP:
213                         range->enc_capa |= IW_ENC_CAPA_CIPHER_CCMP;
214                         break;
215
216                 case WLAN_CIPHER_SUITE_WEP40:
217                         range->encoding_size[range->num_encoding_sizes++] =
218                                 WLAN_KEY_LEN_WEP40;
219                         break;
220
221                 case WLAN_CIPHER_SUITE_WEP104:
222                         range->encoding_size[range->num_encoding_sizes++] =
223                                 WLAN_KEY_LEN_WEP104;
224                         break;
225                 }
226         }
227
228         for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
229                 int i;
230                 struct ieee80211_supported_band *sband;
231
232                 sband = wdev->wiphy->bands[band];
233
234                 if (!sband)
235                         continue;
236
237                 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
238                         struct ieee80211_channel *chan = &sband->channels[i];
239
240                         if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
241                                 range->freq[c].i =
242                                         ieee80211_frequency_to_channel(
243                                                 chan->center_freq);
244                                 range->freq[c].m = chan->center_freq;
245                                 range->freq[c].e = 6;
246                                 c++;
247                         }
248                 }
249         }
250         range->num_channels = c;
251         range->num_frequency = c;
252
253         IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
254         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
255         IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
256
257         if (wdev->wiphy->max_scan_ssids > 0)
258                 range->scan_capa |= IW_SCAN_CAPA_ESSID;
259
260         return 0;
261 }
262 EXPORT_SYMBOL_GPL(cfg80211_wext_giwrange);
263
264 int cfg80211_wext_siwmlme(struct net_device *dev,
265                           struct iw_request_info *info,
266                           struct iw_point *data, char *extra)
267 {
268         struct wireless_dev *wdev = dev->ieee80211_ptr;
269         struct iw_mlme *mlme = (struct iw_mlme *)extra;
270         struct cfg80211_registered_device *rdev;
271         union {
272                 struct cfg80211_disassoc_request disassoc;
273                 struct cfg80211_deauth_request deauth;
274         } cmd;
275
276         if (!wdev)
277                 return -EOPNOTSUPP;
278
279         rdev = wiphy_to_dev(wdev->wiphy);
280
281         if (wdev->iftype != NL80211_IFTYPE_STATION)
282                 return -EINVAL;
283
284         if (mlme->addr.sa_family != ARPHRD_ETHER)
285                 return -EINVAL;
286
287         memset(&cmd, 0, sizeof(cmd));
288
289         switch (mlme->cmd) {
290         case IW_MLME_DEAUTH:
291                 if (!rdev->ops->deauth)
292                         return -EOPNOTSUPP;
293                 cmd.deauth.peer_addr = mlme->addr.sa_data;
294                 cmd.deauth.reason_code = mlme->reason_code;
295                 return rdev->ops->deauth(wdev->wiphy, dev, &cmd.deauth);
296         case IW_MLME_DISASSOC:
297                 if (!rdev->ops->disassoc)
298                         return -EOPNOTSUPP;
299                 cmd.disassoc.peer_addr = mlme->addr.sa_data;
300                 cmd.disassoc.reason_code = mlme->reason_code;
301                 return rdev->ops->disassoc(wdev->wiphy, dev, &cmd.disassoc);
302         default:
303                 return -EOPNOTSUPP;
304         }
305 }
306 EXPORT_SYMBOL_GPL(cfg80211_wext_siwmlme);
307
308
309 /**
310  * cfg80211_wext_freq - get wext frequency for non-"auto"
311  * @wiphy: the wiphy
312  * @freq: the wext freq encoding
313  *
314  * Returns a channel, %NULL for auto, or an ERR_PTR for errors!
315  */
316 struct ieee80211_channel *cfg80211_wext_freq(struct wiphy *wiphy,
317                                              struct iw_freq *freq)
318 {
319         struct ieee80211_channel *chan;
320         int f;
321
322         /*
323          * Parse frequency - return NULL for auto and
324          * -EINVAL for impossible things.
325          */
326         if (freq->e == 0) {
327                 if (freq->m < 0)
328                         return NULL;
329                 f = ieee80211_channel_to_frequency(freq->m);
330         } else {
331                 int i, div = 1000000;
332                 for (i = 0; i < freq->e; i++)
333                         div /= 10;
334                 if (div <= 0)
335                         return ERR_PTR(-EINVAL);
336                 f = freq->m / div;
337         }
338
339         /*
340          * Look up channel struct and return -EINVAL when
341          * it cannot be found.
342          */
343         chan = ieee80211_get_channel(wiphy, f);
344         if (!chan)
345                 return ERR_PTR(-EINVAL);
346         return chan;
347 }
348 EXPORT_SYMBOL_GPL(cfg80211_wext_freq);
349
350 int cfg80211_wext_siwrts(struct net_device *dev,
351                          struct iw_request_info *info,
352                          struct iw_param *rts, char *extra)
353 {
354         struct wireless_dev *wdev = dev->ieee80211_ptr;
355         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
356         u32 orts = wdev->wiphy->rts_threshold;
357         int err;
358
359         if (rts->disabled || !rts->fixed)
360                 wdev->wiphy->rts_threshold = (u32) -1;
361         else if (rts->value < 0)
362                 return -EINVAL;
363         else
364                 wdev->wiphy->rts_threshold = rts->value;
365
366         err = rdev->ops->set_wiphy_params(wdev->wiphy,
367                                           WIPHY_PARAM_RTS_THRESHOLD);
368         if (err)
369                 wdev->wiphy->rts_threshold = orts;
370
371         return err;
372 }
373 EXPORT_SYMBOL_GPL(cfg80211_wext_siwrts);
374
375 int cfg80211_wext_giwrts(struct net_device *dev,
376                          struct iw_request_info *info,
377                          struct iw_param *rts, char *extra)
378 {
379         struct wireless_dev *wdev = dev->ieee80211_ptr;
380
381         rts->value = wdev->wiphy->rts_threshold;
382         rts->disabled = rts->value == (u32) -1;
383         rts->fixed = 1;
384
385         return 0;
386 }
387 EXPORT_SYMBOL_GPL(cfg80211_wext_giwrts);
388
389 int cfg80211_wext_siwfrag(struct net_device *dev,
390                           struct iw_request_info *info,
391                           struct iw_param *frag, char *extra)
392 {
393         struct wireless_dev *wdev = dev->ieee80211_ptr;
394         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
395         u32 ofrag = wdev->wiphy->frag_threshold;
396         int err;
397
398         if (frag->disabled || !frag->fixed)
399                 wdev->wiphy->frag_threshold = (u32) -1;
400         else if (frag->value < 256)
401                 return -EINVAL;
402         else {
403                 /* Fragment length must be even, so strip LSB. */
404                 wdev->wiphy->frag_threshold = frag->value & ~0x1;
405         }
406
407         err = rdev->ops->set_wiphy_params(wdev->wiphy,
408                                           WIPHY_PARAM_FRAG_THRESHOLD);
409         if (err)
410                 wdev->wiphy->frag_threshold = ofrag;
411
412         return err;
413 }
414 EXPORT_SYMBOL_GPL(cfg80211_wext_siwfrag);
415
416 int cfg80211_wext_giwfrag(struct net_device *dev,
417                           struct iw_request_info *info,
418                           struct iw_param *frag, char *extra)
419 {
420         struct wireless_dev *wdev = dev->ieee80211_ptr;
421
422         frag->value = wdev->wiphy->frag_threshold;
423         frag->disabled = frag->value == (u32) -1;
424         frag->fixed = 1;
425
426         return 0;
427 }
428 EXPORT_SYMBOL_GPL(cfg80211_wext_giwfrag);
429
430 int cfg80211_wext_siwretry(struct net_device *dev,
431                            struct iw_request_info *info,
432                            struct iw_param *retry, char *extra)
433 {
434         struct wireless_dev *wdev = dev->ieee80211_ptr;
435         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
436         u32 changed = 0;
437         u8 olong = wdev->wiphy->retry_long;
438         u8 oshort = wdev->wiphy->retry_short;
439         int err;
440
441         if (retry->disabled ||
442             (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
443                 return -EINVAL;
444
445         if (retry->flags & IW_RETRY_LONG) {
446                 wdev->wiphy->retry_long = retry->value;
447                 changed |= WIPHY_PARAM_RETRY_LONG;
448         } else if (retry->flags & IW_RETRY_SHORT) {
449                 wdev->wiphy->retry_short = retry->value;
450                 changed |= WIPHY_PARAM_RETRY_SHORT;
451         } else {
452                 wdev->wiphy->retry_short = retry->value;
453                 wdev->wiphy->retry_long = retry->value;
454                 changed |= WIPHY_PARAM_RETRY_LONG;
455                 changed |= WIPHY_PARAM_RETRY_SHORT;
456         }
457
458         if (!changed)
459                 return 0;
460
461         err = rdev->ops->set_wiphy_params(wdev->wiphy, changed);
462         if (err) {
463                 wdev->wiphy->retry_short = oshort;
464                 wdev->wiphy->retry_long = olong;
465         }
466
467         return err;
468 }
469 EXPORT_SYMBOL_GPL(cfg80211_wext_siwretry);
470
471 int cfg80211_wext_giwretry(struct net_device *dev,
472                            struct iw_request_info *info,
473                            struct iw_param *retry, char *extra)
474 {
475         struct wireless_dev *wdev = dev->ieee80211_ptr;
476
477         retry->disabled = 0;
478
479         if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
480                 /*
481                  * First return short value, iwconfig will ask long value
482                  * later if needed
483                  */
484                 retry->flags |= IW_RETRY_LIMIT;
485                 retry->value = wdev->wiphy->retry_short;
486                 if (wdev->wiphy->retry_long != wdev->wiphy->retry_short)
487                         retry->flags |= IW_RETRY_LONG;
488
489                 return 0;
490         }
491
492         if (retry->flags & IW_RETRY_LONG) {
493                 retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
494                 retry->value = wdev->wiphy->retry_long;
495         }
496
497         return 0;
498 }
499 EXPORT_SYMBOL_GPL(cfg80211_wext_giwretry);
500
501 static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
502                                    struct net_device *dev, const u8 *addr,
503                                    bool remove, bool tx_key, int idx,
504                                    struct key_params *params)
505 {
506         struct wireless_dev *wdev = dev->ieee80211_ptr;
507         int err;
508
509         if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
510                 if (!rdev->ops->set_default_mgmt_key)
511                         return -EOPNOTSUPP;
512
513                 if (idx < 4 || idx > 5)
514                         return -EINVAL;
515         } else if (idx < 0 || idx > 3)
516                 return -EINVAL;
517
518         if (remove) {
519                 err = rdev->ops->del_key(&rdev->wiphy, dev, idx, addr);
520                 if (!err) {
521                         if (idx == wdev->wext.default_key)
522                                 wdev->wext.default_key = -1;
523                         else if (idx == wdev->wext.default_mgmt_key)
524                                 wdev->wext.default_mgmt_key = -1;
525                 }
526                 /*
527                  * Applications using wireless extensions expect to be
528                  * able to delete keys that don't exist, so allow that.
529                  */
530                 if (err == -ENOENT)
531                         return 0;
532
533                 return err;
534         } else {
535                 if (addr)
536                         tx_key = false;
537
538                 if (cfg80211_validate_key_settings(params, idx, addr))
539                         return -EINVAL;
540
541                 err = rdev->ops->add_key(&rdev->wiphy, dev, idx, addr, params);
542                 if (err)
543                         return err;
544
545                 if (tx_key || (!addr && wdev->wext.default_key == -1)) {
546                         err = rdev->ops->set_default_key(&rdev->wiphy,
547                                                          dev, idx);
548                         if (!err)
549                                 wdev->wext.default_key = idx;
550                         return err;
551                 }
552
553                 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
554                     (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
555                         err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
556                                                               dev, idx);
557                         if (!err)
558                                 wdev->wext.default_mgmt_key = idx;
559                         return err;
560                 }
561
562                 return 0;
563         }
564 }
565
566 int cfg80211_wext_siwencode(struct net_device *dev,
567                             struct iw_request_info *info,
568                             struct iw_point *erq, char *keybuf)
569 {
570         struct wireless_dev *wdev = dev->ieee80211_ptr;
571         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
572         int idx, err;
573         bool remove = false;
574         struct key_params params;
575
576         /* no use -- only MFP (set_default_mgmt_key) is optional */
577         if (!rdev->ops->del_key ||
578             !rdev->ops->add_key ||
579             !rdev->ops->set_default_key)
580                 return -EOPNOTSUPP;
581
582         idx = erq->flags & IW_ENCODE_INDEX;
583         if (idx == 0) {
584                 idx = wdev->wext.default_key;
585                 if (idx < 0)
586                         idx = 0;
587         } else if (idx < 1 || idx > 4)
588                 return -EINVAL;
589         else
590                 idx--;
591
592         if (erq->flags & IW_ENCODE_DISABLED)
593                 remove = true;
594         else if (erq->length == 0) {
595                 /* No key data - just set the default TX key index */
596                 err = rdev->ops->set_default_key(&rdev->wiphy, dev, idx);
597                 if (!err)
598                         wdev->wext.default_key = idx;
599                 return err;
600         }
601
602         memset(&params, 0, sizeof(params));
603         params.key = keybuf;
604         params.key_len = erq->length;
605         if (erq->length == 5)
606                 params.cipher = WLAN_CIPHER_SUITE_WEP40;
607         else if (erq->length == 13)
608                 params.cipher = WLAN_CIPHER_SUITE_WEP104;
609         else if (!remove)
610                 return -EINVAL;
611
612         return cfg80211_set_encryption(rdev, dev, NULL, remove,
613                                        wdev->wext.default_key == -1,
614                                        idx, &params);
615 }
616 EXPORT_SYMBOL_GPL(cfg80211_wext_siwencode);
617
618 int cfg80211_wext_siwencodeext(struct net_device *dev,
619                                struct iw_request_info *info,
620                                struct iw_point *erq, char *extra)
621 {
622         struct wireless_dev *wdev = dev->ieee80211_ptr;
623         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
624         struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
625         const u8 *addr;
626         int idx;
627         bool remove = false;
628         struct key_params params;
629         u32 cipher;
630
631         /* no use -- only MFP (set_default_mgmt_key) is optional */
632         if (!rdev->ops->del_key ||
633             !rdev->ops->add_key ||
634             !rdev->ops->set_default_key)
635                 return -EOPNOTSUPP;
636
637         switch (ext->alg) {
638         case IW_ENCODE_ALG_NONE:
639                 remove = true;
640                 cipher = 0;
641                 break;
642         case IW_ENCODE_ALG_WEP:
643                 if (ext->key_len == 5)
644                         cipher = WLAN_CIPHER_SUITE_WEP40;
645                 else if (ext->key_len == 13)
646                         cipher = WLAN_CIPHER_SUITE_WEP104;
647                 else
648                         return -EINVAL;
649                 break;
650         case IW_ENCODE_ALG_TKIP:
651                 cipher = WLAN_CIPHER_SUITE_TKIP;
652                 break;
653         case IW_ENCODE_ALG_CCMP:
654                 cipher = WLAN_CIPHER_SUITE_CCMP;
655                 break;
656         case IW_ENCODE_ALG_AES_CMAC:
657                 cipher = WLAN_CIPHER_SUITE_AES_CMAC;
658                 break;
659         default:
660                 return -EOPNOTSUPP;
661         }
662
663         if (erq->flags & IW_ENCODE_DISABLED)
664                 remove = true;
665
666         idx = erq->flags & IW_ENCODE_INDEX;
667         if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
668                 if (idx < 4 || idx > 5) {
669                         idx = wdev->wext.default_mgmt_key;
670                         if (idx < 0)
671                                 return -EINVAL;
672                 } else
673                         idx--;
674         } else {
675                 if (idx < 1 || idx > 4) {
676                         idx = wdev->wext.default_key;
677                         if (idx < 0)
678                                 return -EINVAL;
679                 } else
680                         idx--;
681         }
682
683         addr = ext->addr.sa_data;
684         if (is_broadcast_ether_addr(addr))
685                 addr = NULL;
686
687         memset(&params, 0, sizeof(params));
688         params.key = ext->key;
689         params.key_len = ext->key_len;
690         params.cipher = cipher;
691
692         if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
693                 params.seq = ext->rx_seq;
694                 params.seq_len = 6;
695         }
696
697         return cfg80211_set_encryption(
698                         rdev, dev, addr, remove,
699                         ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
700                         idx, &params);
701 }
702 EXPORT_SYMBOL_GPL(cfg80211_wext_siwencodeext);
703
704 struct giwencode_cookie {
705         size_t buflen;
706         char *keybuf;
707 };
708
709 static void giwencode_get_key_cb(void *cookie, struct key_params *params)
710 {
711         struct giwencode_cookie *data = cookie;
712
713         if (!params->key) {
714                 data->buflen = 0;
715                 return;
716         }
717
718         data->buflen = min_t(size_t, data->buflen, params->key_len);
719         memcpy(data->keybuf, params->key, data->buflen);
720 }
721
722 int cfg80211_wext_giwencode(struct net_device *dev,
723                             struct iw_request_info *info,
724                             struct iw_point *erq, char *keybuf)
725 {
726         struct wireless_dev *wdev = dev->ieee80211_ptr;
727         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
728         int idx, err;
729         struct giwencode_cookie data = {
730                 .keybuf = keybuf,
731                 .buflen = erq->length,
732         };
733
734         if (!rdev->ops->get_key)
735                 return -EOPNOTSUPP;
736
737         idx = erq->flags & IW_ENCODE_INDEX;
738         if (idx == 0) {
739                 idx = wdev->wext.default_key;
740                 if (idx < 0)
741                         idx = 0;
742         } else if (idx < 1 || idx > 4)
743                 return -EINVAL;
744         else
745                 idx--;
746
747         erq->flags = idx + 1;
748
749         err = rdev->ops->get_key(&rdev->wiphy, dev, idx, NULL, &data,
750                                  giwencode_get_key_cb);
751         if (!err) {
752                 erq->length = data.buflen;
753                 erq->flags |= IW_ENCODE_ENABLED;
754                 return 0;
755         }
756
757         if (err == -ENOENT) {
758                 erq->flags |= IW_ENCODE_DISABLED;
759                 erq->length = 0;
760                 return 0;
761         }
762
763         return err;
764 }
765 EXPORT_SYMBOL_GPL(cfg80211_wext_giwencode);
766
767 int cfg80211_wext_siwtxpower(struct net_device *dev,
768                              struct iw_request_info *info,
769                              union iwreq_data *data, char *extra)
770 {
771         struct wireless_dev *wdev = dev->ieee80211_ptr;
772         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
773         enum tx_power_setting type;
774         int dbm = 0;
775
776         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
777                 return -EINVAL;
778         if (data->txpower.flags & IW_TXPOW_RANGE)
779                 return -EINVAL;
780
781         if (!rdev->ops->set_tx_power)
782                 return -EOPNOTSUPP;
783
784         /* only change when not disabling */
785         if (!data->txpower.disabled) {
786                 rfkill_set_sw_state(rdev->rfkill, false);
787
788                 if (data->txpower.fixed) {
789                         /*
790                          * wext doesn't support negative values, see
791                          * below where it's for automatic
792                          */
793                         if (data->txpower.value < 0)
794                                 return -EINVAL;
795                         dbm = data->txpower.value;
796                         type = TX_POWER_FIXED;
797                         /* TODO: do regulatory check! */
798                 } else {
799                         /*
800                          * Automatic power level setting, max being the value
801                          * passed in from userland.
802                          */
803                         if (data->txpower.value < 0) {
804                                 type = TX_POWER_AUTOMATIC;
805                         } else {
806                                 dbm = data->txpower.value;
807                                 type = TX_POWER_LIMITED;
808                         }
809                 }
810         } else {
811                 rfkill_set_sw_state(rdev->rfkill, true);
812                 schedule_work(&rdev->rfkill_sync);
813                 return 0;
814         }
815
816         return rdev->ops->set_tx_power(wdev->wiphy, type, dbm);;
817 }
818 EXPORT_SYMBOL_GPL(cfg80211_wext_siwtxpower);
819
820 int cfg80211_wext_giwtxpower(struct net_device *dev,
821                              struct iw_request_info *info,
822                              union iwreq_data *data, char *extra)
823 {
824         struct wireless_dev *wdev = dev->ieee80211_ptr;
825         struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
826         int err, val;
827
828         if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
829                 return -EINVAL;
830         if (data->txpower.flags & IW_TXPOW_RANGE)
831                 return -EINVAL;
832
833         if (!rdev->ops->get_tx_power)
834                 return -EOPNOTSUPP;
835
836         err = rdev->ops->get_tx_power(wdev->wiphy, &val);
837         if (err)
838                 return err;
839
840         /* well... oh well */
841         data->txpower.fixed = 1;
842         data->txpower.disabled = rfkill_blocked(rdev->rfkill);
843         data->txpower.value = val;
844         data->txpower.flags = IW_TXPOW_DBM;
845
846         return 0;
847 }
848 EXPORT_SYMBOL_GPL(cfg80211_wext_giwtxpower);