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