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