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