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