xref: /linux/net/wireless/wext-compat.c (revision 6a20b34fe3b31b292078bc79ec18a2ab0d9f7719)
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 
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 
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 
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 
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  */
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 
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 
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 
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 
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 
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 
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 
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, wdev, params, idx,
493 					   pairwise, addr))
494 		return -EINVAL;
495 
496 	err = 0;
497 	if (wdev->connected ||
498 	    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
499 	     wdev->u.ibss.current_bss))
500 		err = rdev_add_key(rdev, wdev, -1, idx, pairwise, addr, params);
501 	else if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
502 		 params->cipher != WLAN_CIPHER_SUITE_WEP104)
503 		return -EINVAL;
504 	if (err)
505 		return err;
506 
507 	/*
508 	 * We only need to store WEP keys, since they're the only keys that
509 	 * can be set before a connection is established and persist after
510 	 * disconnecting.
511 	 */
512 	if (!addr && (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
513 		      params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
514 		wdev->wext.keys->params[idx] = *params;
515 		memcpy(wdev->wext.keys->data[idx],
516 			params->key, params->key_len);
517 		wdev->wext.keys->params[idx].key =
518 			wdev->wext.keys->data[idx];
519 	}
520 
521 	if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
522 	     params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
523 	    (tx_key || (!addr && wdev->wext.default_key == -1))) {
524 		if (wdev->connected ||
525 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
526 		     wdev->u.ibss.current_bss)) {
527 			/*
528 			 * If we are getting a new TX key from not having
529 			 * had one before we need to join a new IBSS with
530 			 * the privacy bit set.
531 			 */
532 			if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
533 			    wdev->wext.default_key == -1) {
534 				cfg80211_leave_ibss(rdev, wdev->netdev, true);
535 				rejoin = true;
536 			}
537 			err = rdev_set_default_key(rdev, dev, -1, idx, true,
538 						   true);
539 		}
540 		if (!err) {
541 			wdev->wext.default_key = idx;
542 			if (rejoin)
543 				err = cfg80211_ibss_wext_join(rdev, wdev);
544 		}
545 		return err;
546 	}
547 
548 	if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
549 	    (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
550 		if (wdev->connected ||
551 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
552 		     wdev->u.ibss.current_bss))
553 			err = rdev_set_default_mgmt_key(rdev, wdev, -1, idx);
554 		if (!err)
555 			wdev->wext.default_mgmt_key = idx;
556 		return err;
557 	}
558 
559 	return 0;
560 }
561 
562 static int cfg80211_wext_siwencode(struct net_device *dev,
563 				   struct iw_request_info *info,
564 				   union iwreq_data *wrqu, char *keybuf)
565 {
566 	struct iw_point *erq = &wrqu->encoding;
567 	struct wireless_dev *wdev = dev->ieee80211_ptr;
568 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
569 	struct key_params params;
570 	bool remove = false;
571 	int idx;
572 
573 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
574 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
575 		return -EOPNOTSUPP;
576 
577 	/* no use -- only MFP (set_default_mgmt_key) is optional */
578 	if (!rdev->ops->del_key ||
579 	    !rdev->ops->add_key ||
580 	    !rdev->ops->set_default_key)
581 		return -EOPNOTSUPP;
582 
583 	guard(wiphy)(&rdev->wiphy);
584 	if (wdev->valid_links)
585 		return -EOPNOTSUPP;
586 
587 	idx = erq->flags & IW_ENCODE_INDEX;
588 	if (idx == 0) {
589 		idx = wdev->wext.default_key;
590 		if (idx < 0)
591 			idx = 0;
592 	} else if (idx < 1 || idx > 4) {
593 		return -EINVAL;
594 	} else {
595 		idx--;
596 	}
597 
598 	if (erq->flags & IW_ENCODE_DISABLED)
599 		remove = true;
600 	else if (erq->length == 0) {
601 		/* No key data - just set the default TX key index */
602 		int err = 0;
603 
604 		if (wdev->connected ||
605 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
606 		     wdev->u.ibss.current_bss))
607 			err = rdev_set_default_key(rdev, dev, -1, idx, true,
608 						   true);
609 		if (!err)
610 			wdev->wext.default_key = idx;
611 		return err;
612 	}
613 
614 	memset(&params, 0, sizeof(params));
615 	params.key = keybuf;
616 	params.key_len = erq->length;
617 	if (erq->length == 5)
618 		params.cipher = WLAN_CIPHER_SUITE_WEP40;
619 	else if (erq->length == 13)
620 		params.cipher = WLAN_CIPHER_SUITE_WEP104;
621 	else if (!remove)
622 		return -EINVAL;
623 
624 	return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
625 				       wdev->wext.default_key == -1,
626 				       idx, &params);
627 }
628 
629 static int cfg80211_wext_siwencodeext(struct net_device *dev,
630 				      struct iw_request_info *info,
631 				      union iwreq_data *wrqu, char *extra)
632 {
633 	struct iw_point *erq = &wrqu->encoding;
634 	struct wireless_dev *wdev = dev->ieee80211_ptr;
635 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
636 	struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
637 	const u8 *addr;
638 	int idx;
639 	bool remove = false;
640 	struct key_params params;
641 	u32 cipher;
642 
643 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
644 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
645 		return -EOPNOTSUPP;
646 
647 	/* no use -- only MFP (set_default_mgmt_key) is optional */
648 	if (!rdev->ops->del_key ||
649 	    !rdev->ops->add_key ||
650 	    !rdev->ops->set_default_key)
651 		return -EOPNOTSUPP;
652 
653 	if (wdev->valid_links)
654 		return -EOPNOTSUPP;
655 
656 	switch (ext->alg) {
657 	case IW_ENCODE_ALG_NONE:
658 		remove = true;
659 		cipher = 0;
660 		break;
661 	case IW_ENCODE_ALG_WEP:
662 		if (ext->key_len == 5)
663 			cipher = WLAN_CIPHER_SUITE_WEP40;
664 		else if (ext->key_len == 13)
665 			cipher = WLAN_CIPHER_SUITE_WEP104;
666 		else
667 			return -EINVAL;
668 		break;
669 	case IW_ENCODE_ALG_TKIP:
670 		cipher = WLAN_CIPHER_SUITE_TKIP;
671 		break;
672 	case IW_ENCODE_ALG_CCMP:
673 		cipher = WLAN_CIPHER_SUITE_CCMP;
674 		break;
675 	case IW_ENCODE_ALG_AES_CMAC:
676 		cipher = WLAN_CIPHER_SUITE_AES_CMAC;
677 		break;
678 	default:
679 		return -EOPNOTSUPP;
680 	}
681 
682 	if (erq->flags & IW_ENCODE_DISABLED)
683 		remove = true;
684 
685 	idx = erq->flags & IW_ENCODE_INDEX;
686 	if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
687 		if (idx < 5 || idx > 6) {
688 			idx = wdev->wext.default_mgmt_key;
689 			if (idx < 0)
690 				return -EINVAL;
691 		} else
692 			idx--;
693 	} else {
694 		if (idx < 1 || idx > 4) {
695 			idx = wdev->wext.default_key;
696 			if (idx < 0)
697 				return -EINVAL;
698 		} else
699 			idx--;
700 	}
701 
702 	addr = ext->addr.sa_data;
703 	if (is_broadcast_ether_addr(addr))
704 		addr = NULL;
705 
706 	memset(&params, 0, sizeof(params));
707 	params.key = ext->key;
708 	params.key_len = ext->key_len;
709 	params.cipher = cipher;
710 
711 	if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
712 		params.seq = ext->rx_seq;
713 		params.seq_len = 6;
714 	}
715 
716 	guard(wiphy)(wdev->wiphy);
717 
718 	return cfg80211_set_encryption(rdev, dev,
719 				       !(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
720 				       addr, remove,
721 				       ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
722 				       idx, &params);
723 }
724 
725 static int cfg80211_wext_giwencode(struct net_device *dev,
726 				   struct iw_request_info *info,
727 				   union iwreq_data *wrqu, char *keybuf)
728 {
729 	struct iw_point *erq = &wrqu->encoding;
730 	struct wireless_dev *wdev = dev->ieee80211_ptr;
731 	int idx;
732 
733 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
734 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
735 		return -EOPNOTSUPP;
736 
737 	idx = erq->flags & IW_ENCODE_INDEX;
738 	if (idx == 0) {
739 		idx = wdev->wext.default_key;
740 		if (idx < 0)
741 			idx = 0;
742 	} else if (idx < 1 || idx > 4)
743 		return -EINVAL;
744 	else
745 		idx--;
746 
747 	erq->flags = idx + 1;
748 
749 	if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
750 		erq->flags |= IW_ENCODE_DISABLED;
751 		erq->length = 0;
752 		return 0;
753 	}
754 
755 	erq->length = min_t(size_t, erq->length,
756 			    wdev->wext.keys->params[idx].key_len);
757 	memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
758 	erq->flags |= IW_ENCODE_ENABLED;
759 
760 	return 0;
761 }
762 
763 static int cfg80211_wext_siwfreq(struct net_device *dev,
764 				 struct iw_request_info *info,
765 				 union iwreq_data *wrqu, char *extra)
766 {
767 	struct iw_freq *wextfreq = &wrqu->freq;
768 	struct wireless_dev *wdev = dev->ieee80211_ptr;
769 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
770 	struct cfg80211_chan_def chandef = {
771 		.width = NL80211_CHAN_WIDTH_20_NOHT,
772 	};
773 	int freq;
774 
775 	guard(wiphy)(&rdev->wiphy);
776 
777 	switch (wdev->iftype) {
778 	case NL80211_IFTYPE_STATION:
779 		return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
780 	case NL80211_IFTYPE_ADHOC:
781 		return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
782 	case NL80211_IFTYPE_MONITOR:
783 		freq = cfg80211_wext_freq(wextfreq);
784 		if (freq < 0)
785 			return freq;
786 		if (freq == 0)
787 			return -EINVAL;
788 
789 		chandef.center_freq1 = freq;
790 		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
791 		if (!chandef.chan)
792 			return -EINVAL;
793 		if (!cfg80211_chandef_valid(&chandef))
794 			return -EINVAL;
795 		return cfg80211_set_monitor_channel(rdev, dev, &chandef);
796 	case NL80211_IFTYPE_MESH_POINT:
797 		freq = cfg80211_wext_freq(wextfreq);
798 		if (freq < 0)
799 			return freq;
800 		if (freq == 0)
801 			return -EINVAL;
802 		chandef.center_freq1 = freq;
803 		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
804 		if (!chandef.chan)
805 			return -EINVAL;
806 		return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
807 	default:
808 		return -EOPNOTSUPP;
809 	}
810 }
811 
812 static int cfg80211_wext_giwfreq(struct net_device *dev,
813 				 struct iw_request_info *info,
814 				 union iwreq_data *wrqu, char *extra)
815 {
816 	struct iw_freq *freq = &wrqu->freq;
817 	struct wireless_dev *wdev = dev->ieee80211_ptr;
818 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
819 	struct cfg80211_chan_def chandef = {};
820 	int ret;
821 
822 	guard(wiphy)(&rdev->wiphy);
823 
824 	switch (wdev->iftype) {
825 	case NL80211_IFTYPE_STATION:
826 		return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
827 	case NL80211_IFTYPE_ADHOC:
828 		return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
829 	case NL80211_IFTYPE_MONITOR:
830 		if (!rdev->ops->get_channel)
831 			return -EINVAL;
832 
833 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
834 		if (ret)
835 			return ret;
836 		freq->m = chandef.chan->center_freq;
837 		freq->e = 6;
838 		return ret;
839 	default:
840 		return -EINVAL;
841 	}
842 }
843 
844 static int cfg80211_wext_siwtxpower(struct net_device *dev,
845 				    struct iw_request_info *info,
846 				    union iwreq_data *data, char *extra)
847 {
848 	struct wireless_dev *wdev = dev->ieee80211_ptr;
849 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
850 	enum nl80211_tx_power_setting type;
851 	int dbm = 0;
852 
853 	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
854 		return -EINVAL;
855 	if (data->txpower.flags & IW_TXPOW_RANGE)
856 		return -EINVAL;
857 
858 	if (!rdev->ops->set_tx_power)
859 		return -EOPNOTSUPP;
860 
861 	/* only change when not disabling */
862 	if (!data->txpower.disabled) {
863 		rfkill_set_sw_state(rdev->wiphy.rfkill, false);
864 
865 		if (data->txpower.fixed) {
866 			/*
867 			 * wext doesn't support negative values, see
868 			 * below where it's for automatic
869 			 */
870 			if (data->txpower.value < 0)
871 				return -EINVAL;
872 			dbm = data->txpower.value;
873 			type = NL80211_TX_POWER_FIXED;
874 			/* TODO: do regulatory check! */
875 		} else {
876 			/*
877 			 * Automatic power level setting, max being the value
878 			 * passed in from userland.
879 			 */
880 			if (data->txpower.value < 0) {
881 				type = NL80211_TX_POWER_AUTOMATIC;
882 			} else {
883 				dbm = data->txpower.value;
884 				type = NL80211_TX_POWER_LIMITED;
885 			}
886 		}
887 	} else {
888 		if (rfkill_set_sw_state(rdev->wiphy.rfkill, true))
889 			schedule_work(&rdev->rfkill_block);
890 		return 0;
891 	}
892 
893 	guard(wiphy)(&rdev->wiphy);
894 
895 	return rdev_set_tx_power(rdev, wdev, -1, type, DBM_TO_MBM(dbm));
896 }
897 
898 static int cfg80211_wext_giwtxpower(struct net_device *dev,
899 				    struct iw_request_info *info,
900 				    union iwreq_data *data, char *extra)
901 {
902 	struct wireless_dev *wdev = dev->ieee80211_ptr;
903 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
904 	int err, val;
905 
906 	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
907 		return -EINVAL;
908 	if (data->txpower.flags & IW_TXPOW_RANGE)
909 		return -EINVAL;
910 
911 	if (!rdev->ops->get_tx_power)
912 		return -EOPNOTSUPP;
913 
914 	scoped_guard(wiphy, &rdev->wiphy) {
915 		err = rdev_get_tx_power(rdev, wdev, -1, 0, &val);
916 	}
917 	if (err)
918 		return err;
919 
920 	/* well... oh well */
921 	data->txpower.fixed = 1;
922 	data->txpower.disabled = rfkill_blocked(rdev->wiphy.rfkill);
923 	data->txpower.value = val;
924 	data->txpower.flags = IW_TXPOW_DBM;
925 
926 	return 0;
927 }
928 
929 static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
930 				 s32 auth_alg)
931 {
932 	int nr_alg = 0;
933 
934 	if (!auth_alg)
935 		return -EINVAL;
936 
937 	if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
938 			 IW_AUTH_ALG_SHARED_KEY |
939 			 IW_AUTH_ALG_LEAP))
940 		return -EINVAL;
941 
942 	if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
943 		nr_alg++;
944 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
945 	}
946 
947 	if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
948 		nr_alg++;
949 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
950 	}
951 
952 	if (auth_alg & IW_AUTH_ALG_LEAP) {
953 		nr_alg++;
954 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
955 	}
956 
957 	if (nr_alg > 1)
958 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
959 
960 	return 0;
961 }
962 
963 static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
964 {
965 	if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
966 			     IW_AUTH_WPA_VERSION_WPA2|
967 		             IW_AUTH_WPA_VERSION_DISABLED))
968 		return -EINVAL;
969 
970 	if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
971 	    (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
972 			     IW_AUTH_WPA_VERSION_WPA2)))
973 		return -EINVAL;
974 
975 	if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
976 		wdev->wext.connect.crypto.wpa_versions &=
977 			~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
978 
979 	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
980 		wdev->wext.connect.crypto.wpa_versions |=
981 			NL80211_WPA_VERSION_1;
982 
983 	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
984 		wdev->wext.connect.crypto.wpa_versions |=
985 			NL80211_WPA_VERSION_2;
986 
987 	return 0;
988 }
989 
990 static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
991 {
992 	if (cipher & IW_AUTH_CIPHER_WEP40)
993 		wdev->wext.connect.crypto.cipher_group =
994 			WLAN_CIPHER_SUITE_WEP40;
995 	else if (cipher & IW_AUTH_CIPHER_WEP104)
996 		wdev->wext.connect.crypto.cipher_group =
997 			WLAN_CIPHER_SUITE_WEP104;
998 	else if (cipher & IW_AUTH_CIPHER_TKIP)
999 		wdev->wext.connect.crypto.cipher_group =
1000 			WLAN_CIPHER_SUITE_TKIP;
1001 	else if (cipher & IW_AUTH_CIPHER_CCMP)
1002 		wdev->wext.connect.crypto.cipher_group =
1003 			WLAN_CIPHER_SUITE_CCMP;
1004 	else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
1005 		wdev->wext.connect.crypto.cipher_group =
1006 			WLAN_CIPHER_SUITE_AES_CMAC;
1007 	else if (cipher & IW_AUTH_CIPHER_NONE)
1008 		wdev->wext.connect.crypto.cipher_group = 0;
1009 	else
1010 		return -EINVAL;
1011 
1012 	return 0;
1013 }
1014 
1015 static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
1016 {
1017 	int nr_ciphers = 0;
1018 	u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
1019 
1020 	if (cipher & IW_AUTH_CIPHER_WEP40) {
1021 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
1022 		nr_ciphers++;
1023 	}
1024 
1025 	if (cipher & IW_AUTH_CIPHER_WEP104) {
1026 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1027 		nr_ciphers++;
1028 	}
1029 
1030 	if (cipher & IW_AUTH_CIPHER_TKIP) {
1031 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1032 		nr_ciphers++;
1033 	}
1034 
1035 	if (cipher & IW_AUTH_CIPHER_CCMP) {
1036 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1037 		nr_ciphers++;
1038 	}
1039 
1040 	if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1041 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1042 		nr_ciphers++;
1043 	}
1044 
1045 	BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1046 
1047 	wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1048 
1049 	return 0;
1050 }
1051 
1052 
1053 static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
1054 {
1055 	int nr_akm_suites = 0;
1056 
1057 	if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1058 			IW_AUTH_KEY_MGMT_PSK))
1059 		return -EINVAL;
1060 
1061 	if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1062 		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1063 			WLAN_AKM_SUITE_8021X;
1064 		nr_akm_suites++;
1065 	}
1066 
1067 	if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1068 		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1069 			WLAN_AKM_SUITE_PSK;
1070 		nr_akm_suites++;
1071 	}
1072 
1073 	wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1074 
1075 	return 0;
1076 }
1077 
1078 static int cfg80211_wext_siwauth(struct net_device *dev,
1079 				 struct iw_request_info *info,
1080 				 union iwreq_data *wrqu, char *extra)
1081 {
1082 	struct iw_param *data = &wrqu->param;
1083 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1084 
1085 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1086 		return -EOPNOTSUPP;
1087 
1088 	switch (data->flags & IW_AUTH_INDEX) {
1089 	case IW_AUTH_PRIVACY_INVOKED:
1090 		wdev->wext.connect.privacy = data->value;
1091 		return 0;
1092 	case IW_AUTH_WPA_VERSION:
1093 		return cfg80211_set_wpa_version(wdev, data->value);
1094 	case IW_AUTH_CIPHER_GROUP:
1095 		return cfg80211_set_cipher_group(wdev, data->value);
1096 	case IW_AUTH_KEY_MGMT:
1097 		return cfg80211_set_key_mgt(wdev, data->value);
1098 	case IW_AUTH_CIPHER_PAIRWISE:
1099 		return cfg80211_set_cipher_pairwise(wdev, data->value);
1100 	case IW_AUTH_80211_AUTH_ALG:
1101 		return cfg80211_set_auth_alg(wdev, data->value);
1102 	case IW_AUTH_WPA_ENABLED:
1103 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1104 	case IW_AUTH_DROP_UNENCRYPTED:
1105 	case IW_AUTH_MFP:
1106 		return 0;
1107 	default:
1108 		return -EOPNOTSUPP;
1109 	}
1110 }
1111 
1112 static int cfg80211_wext_giwauth(struct net_device *dev,
1113 				 struct iw_request_info *info,
1114 				 union iwreq_data *wrqu, char *extra)
1115 {
1116 	/* XXX: what do we need? */
1117 
1118 	return -EOPNOTSUPP;
1119 }
1120 
1121 static int cfg80211_wext_siwpower(struct net_device *dev,
1122 				  struct iw_request_info *info,
1123 				  union iwreq_data *wrqu, char *extra)
1124 {
1125 	struct iw_param *wrq = &wrqu->power;
1126 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1127 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1128 	bool ps;
1129 	int timeout = wdev->ps_timeout;
1130 	int err;
1131 
1132 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1133 		return -EINVAL;
1134 
1135 	if (!rdev->ops->set_power_mgmt)
1136 		return -EOPNOTSUPP;
1137 
1138 	if (wrq->disabled) {
1139 		ps = false;
1140 	} else {
1141 		switch (wrq->flags & IW_POWER_MODE) {
1142 		case IW_POWER_ON:       /* If not specified */
1143 		case IW_POWER_MODE:     /* If set all mask */
1144 		case IW_POWER_ALL_R:    /* If explicitly state all */
1145 			ps = true;
1146 			break;
1147 		default:                /* Otherwise we ignore */
1148 			return -EINVAL;
1149 		}
1150 
1151 		if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1152 			return -EINVAL;
1153 
1154 		if (wrq->flags & IW_POWER_TIMEOUT)
1155 			timeout = wrq->value / 1000;
1156 	}
1157 
1158 	guard(wiphy)(&rdev->wiphy);
1159 
1160 	err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
1161 	if (err)
1162 		return err;
1163 
1164 	wdev->ps = ps;
1165 	wdev->ps_timeout = timeout;
1166 
1167 	return 0;
1168 
1169 }
1170 
1171 static int cfg80211_wext_giwpower(struct net_device *dev,
1172 				  struct iw_request_info *info,
1173 				  union iwreq_data *wrqu, char *extra)
1174 {
1175 	struct iw_param *wrq = &wrqu->power;
1176 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1177 
1178 	wrq->disabled = !wdev->ps;
1179 
1180 	return 0;
1181 }
1182 
1183 static int cfg80211_wext_siwrate(struct net_device *dev,
1184 				 struct iw_request_info *info,
1185 				 union iwreq_data *wrqu, char *extra)
1186 {
1187 	struct iw_param *rate = &wrqu->bitrate;
1188 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1189 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1190 	struct cfg80211_bitrate_mask mask;
1191 	u32 fixed, maxrate;
1192 	struct ieee80211_supported_band *sband;
1193 	bool match = false;
1194 	int band, ridx;
1195 
1196 	if (!rdev->ops->set_bitrate_mask)
1197 		return -EOPNOTSUPP;
1198 
1199 	memset(&mask, 0, sizeof(mask));
1200 	fixed = 0;
1201 	maxrate = (u32)-1;
1202 
1203 	if (rate->value < 0) {
1204 		/* nothing */
1205 	} else if (rate->fixed) {
1206 		fixed = rate->value / 100000;
1207 	} else {
1208 		maxrate = rate->value / 100000;
1209 	}
1210 
1211 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1212 		sband = wdev->wiphy->bands[band];
1213 		if (sband == NULL)
1214 			continue;
1215 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1216 			struct ieee80211_rate *srate = &sband->bitrates[ridx];
1217 			if (fixed == srate->bitrate) {
1218 				mask.control[band].legacy = 1 << ridx;
1219 				match = true;
1220 				break;
1221 			}
1222 			if (srate->bitrate <= maxrate) {
1223 				mask.control[band].legacy |= 1 << ridx;
1224 				match = true;
1225 			}
1226 		}
1227 	}
1228 
1229 	if (!match)
1230 		return -EINVAL;
1231 
1232 	guard(wiphy)(&rdev->wiphy);
1233 
1234 	if (dev->ieee80211_ptr->valid_links)
1235 		return -EOPNOTSUPP;
1236 
1237 	return rdev_set_bitrate_mask(rdev, dev, 0, NULL, &mask);
1238 }
1239 
1240 static int cfg80211_wext_giwrate(struct net_device *dev,
1241 				 struct iw_request_info *info,
1242 				 union iwreq_data *wrqu, char *extra)
1243 {
1244 	struct iw_param *rate = &wrqu->bitrate;
1245 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1246 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1247 	struct station_info sinfo = {};
1248 	u8 addr[ETH_ALEN];
1249 	int err;
1250 
1251 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1252 		return -EOPNOTSUPP;
1253 
1254 	if (!rdev->ops->get_station)
1255 		return -EOPNOTSUPP;
1256 
1257 	err = 0;
1258 	if (!wdev->valid_links && wdev->links[0].client.current_bss)
1259 		memcpy(addr, wdev->links[0].client.current_bss->pub.bssid,
1260 		       ETH_ALEN);
1261 	else
1262 		err = -EOPNOTSUPP;
1263 	if (err)
1264 		return err;
1265 
1266 	scoped_guard(wiphy, &rdev->wiphy) {
1267 		err = rdev_get_station(rdev, wdev, addr, &sinfo);
1268 	}
1269 	if (err)
1270 		return err;
1271 
1272 	if (!(sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
1273 		err = -EOPNOTSUPP;
1274 		goto free;
1275 	}
1276 
1277 	rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
1278 
1279 free:
1280 	cfg80211_sinfo_release_content(&sinfo);
1281 	return err;
1282 }
1283 
1284 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
1285 static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
1286 {
1287 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1288 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1289 	/* we are under RTNL - globally locked - so can use static structs */
1290 	static struct iw_statistics wstats;
1291 	static struct station_info sinfo = {};
1292 	u8 bssid[ETH_ALEN];
1293 	int ret;
1294 
1295 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1296 		return NULL;
1297 
1298 	if (!rdev->ops->get_station)
1299 		return NULL;
1300 
1301 	/* Grab BSSID of current BSS, if any */
1302 	wiphy_lock(&rdev->wiphy);
1303 	if (wdev->valid_links || !wdev->links[0].client.current_bss) {
1304 		wiphy_unlock(&rdev->wiphy);
1305 		return NULL;
1306 	}
1307 	memcpy(bssid, wdev->links[0].client.current_bss->pub.bssid, ETH_ALEN);
1308 
1309 	memset(&sinfo, 0, sizeof(sinfo));
1310 
1311 	ret = rdev_get_station(rdev, wdev, bssid, &sinfo);
1312 	wiphy_unlock(&rdev->wiphy);
1313 
1314 	if (ret)
1315 		return NULL;
1316 
1317 	memset(&wstats, 0, sizeof(wstats));
1318 
1319 	switch (rdev->wiphy.signal_type) {
1320 	case CFG80211_SIGNAL_TYPE_MBM:
1321 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1322 			int sig = sinfo.signal;
1323 			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1324 			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1325 			wstats.qual.updated |= IW_QUAL_DBM;
1326 			wstats.qual.level = sig;
1327 			if (sig < -110)
1328 				sig = -110;
1329 			else if (sig > -40)
1330 				sig = -40;
1331 			wstats.qual.qual = sig + 110;
1332 			break;
1333 		}
1334 		fallthrough;
1335 	case CFG80211_SIGNAL_TYPE_UNSPEC:
1336 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1337 			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1338 			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1339 			wstats.qual.level = sinfo.signal;
1340 			wstats.qual.qual = sinfo.signal;
1341 			break;
1342 		}
1343 		fallthrough;
1344 	default:
1345 		wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1346 		wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1347 	}
1348 
1349 	wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1350 	if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC))
1351 		wstats.discard.misc = sinfo.rx_dropped_misc;
1352 	if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))
1353 		wstats.discard.retries = sinfo.tx_failed;
1354 
1355 	cfg80211_sinfo_release_content(&sinfo);
1356 
1357 	return &wstats;
1358 }
1359 
1360 static int cfg80211_wext_siwap(struct net_device *dev,
1361 			       struct iw_request_info *info,
1362 			       union iwreq_data *wrqu, char *extra)
1363 {
1364 	struct sockaddr *ap_addr = &wrqu->ap_addr;
1365 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1366 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1367 
1368 	guard(wiphy)(&rdev->wiphy);
1369 
1370 	switch (wdev->iftype) {
1371 	case NL80211_IFTYPE_ADHOC:
1372 		return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1373 	case NL80211_IFTYPE_STATION:
1374 		return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1375 	default:
1376 		return -EOPNOTSUPP;
1377 	}
1378 }
1379 
1380 static int cfg80211_wext_giwap(struct net_device *dev,
1381 			       struct iw_request_info *info,
1382 			       union iwreq_data *wrqu, char *extra)
1383 {
1384 	struct sockaddr *ap_addr = &wrqu->ap_addr;
1385 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1386 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1387 
1388 	guard(wiphy)(&rdev->wiphy);
1389 
1390 	switch (wdev->iftype) {
1391 	case NL80211_IFTYPE_ADHOC:
1392 		return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1393 	case NL80211_IFTYPE_STATION:
1394 		return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1395 	default:
1396 		return -EOPNOTSUPP;
1397 	}
1398 }
1399 
1400 static int cfg80211_wext_siwessid(struct net_device *dev,
1401 				  struct iw_request_info *info,
1402 				  union iwreq_data *wrqu, char *ssid)
1403 {
1404 	struct iw_point *data = &wrqu->data;
1405 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1406 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1407 
1408 	guard(wiphy)(&rdev->wiphy);
1409 
1410 	switch (wdev->iftype) {
1411 	case NL80211_IFTYPE_ADHOC:
1412 		return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1413 	case NL80211_IFTYPE_STATION:
1414 		return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1415 	default:
1416 		return -EOPNOTSUPP;
1417 	}
1418 }
1419 
1420 static int cfg80211_wext_giwessid(struct net_device *dev,
1421 				  struct iw_request_info *info,
1422 				  union iwreq_data *wrqu, char *ssid)
1423 {
1424 	struct iw_point *data = &wrqu->data;
1425 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1426 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1427 
1428 	data->flags = 0;
1429 	data->length = 0;
1430 
1431 	guard(wiphy)(&rdev->wiphy);
1432 
1433 	switch (wdev->iftype) {
1434 	case NL80211_IFTYPE_ADHOC:
1435 		return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1436 	case NL80211_IFTYPE_STATION:
1437 		return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1438 	default:
1439 		return -EOPNOTSUPP;
1440 	}
1441 }
1442 
1443 static int cfg80211_wext_siwpmksa(struct net_device *dev,
1444 				  struct iw_request_info *info,
1445 				  union iwreq_data *wrqu, char *extra)
1446 {
1447 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1448 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1449 	struct cfg80211_pmksa cfg_pmksa;
1450 	struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1451 
1452 	memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1453 
1454 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1455 		return -EINVAL;
1456 
1457 	cfg_pmksa.bssid = pmksa->bssid.sa_data;
1458 	cfg_pmksa.pmkid = pmksa->pmkid;
1459 
1460 	guard(wiphy)(&rdev->wiphy);
1461 
1462 	switch (pmksa->cmd) {
1463 	case IW_PMKSA_ADD:
1464 		if (!rdev->ops->set_pmksa)
1465 			return -EOPNOTSUPP;
1466 
1467 		return rdev_set_pmksa(rdev, dev, &cfg_pmksa);
1468 	case IW_PMKSA_REMOVE:
1469 		if (!rdev->ops->del_pmksa)
1470 			return -EOPNOTSUPP;
1471 
1472 		return rdev_del_pmksa(rdev, dev, &cfg_pmksa);
1473 	case IW_PMKSA_FLUSH:
1474 		if (!rdev->ops->flush_pmksa)
1475 			return -EOPNOTSUPP;
1476 
1477 		return rdev_flush_pmksa(rdev, dev);
1478 	default:
1479 		return -EOPNOTSUPP;
1480 	}
1481 }
1482 
1483 static const iw_handler cfg80211_handlers[] = {
1484 	IW_HANDLER(SIOCGIWNAME,		cfg80211_wext_giwname),
1485 	IW_HANDLER(SIOCSIWFREQ,		cfg80211_wext_siwfreq),
1486 	IW_HANDLER(SIOCGIWFREQ,		cfg80211_wext_giwfreq),
1487 	IW_HANDLER(SIOCSIWMODE,		cfg80211_wext_siwmode),
1488 	IW_HANDLER(SIOCGIWMODE,		cfg80211_wext_giwmode),
1489 	IW_HANDLER(SIOCGIWRANGE,	cfg80211_wext_giwrange),
1490 	IW_HANDLER(SIOCSIWAP,		cfg80211_wext_siwap),
1491 	IW_HANDLER(SIOCGIWAP,		cfg80211_wext_giwap),
1492 	IW_HANDLER(SIOCSIWMLME,		cfg80211_wext_siwmlme),
1493 	IW_HANDLER(SIOCSIWSCAN,		cfg80211_wext_siwscan),
1494 	IW_HANDLER(SIOCGIWSCAN,		cfg80211_wext_giwscan),
1495 	IW_HANDLER(SIOCSIWESSID,	cfg80211_wext_siwessid),
1496 	IW_HANDLER(SIOCGIWESSID,	cfg80211_wext_giwessid),
1497 	IW_HANDLER(SIOCSIWRATE,		cfg80211_wext_siwrate),
1498 	IW_HANDLER(SIOCGIWRATE,		cfg80211_wext_giwrate),
1499 	IW_HANDLER(SIOCSIWRTS,		cfg80211_wext_siwrts),
1500 	IW_HANDLER(SIOCGIWRTS,		cfg80211_wext_giwrts),
1501 	IW_HANDLER(SIOCSIWFRAG,		cfg80211_wext_siwfrag),
1502 	IW_HANDLER(SIOCGIWFRAG,		cfg80211_wext_giwfrag),
1503 	IW_HANDLER(SIOCSIWTXPOW,	cfg80211_wext_siwtxpower),
1504 	IW_HANDLER(SIOCGIWTXPOW,	cfg80211_wext_giwtxpower),
1505 	IW_HANDLER(SIOCSIWRETRY,	cfg80211_wext_siwretry),
1506 	IW_HANDLER(SIOCGIWRETRY,	cfg80211_wext_giwretry),
1507 	IW_HANDLER(SIOCSIWENCODE,	cfg80211_wext_siwencode),
1508 	IW_HANDLER(SIOCGIWENCODE,	cfg80211_wext_giwencode),
1509 	IW_HANDLER(SIOCSIWPOWER,	cfg80211_wext_siwpower),
1510 	IW_HANDLER(SIOCGIWPOWER,	cfg80211_wext_giwpower),
1511 	IW_HANDLER(SIOCSIWGENIE,	cfg80211_wext_siwgenie),
1512 	IW_HANDLER(SIOCSIWAUTH,		cfg80211_wext_siwauth),
1513 	IW_HANDLER(SIOCGIWAUTH,		cfg80211_wext_giwauth),
1514 	IW_HANDLER(SIOCSIWENCODEEXT,	cfg80211_wext_siwencodeext),
1515 	IW_HANDLER(SIOCSIWPMKSA,	cfg80211_wext_siwpmksa),
1516 };
1517 
1518 const struct iw_handler_def cfg80211_wext_handler = {
1519 	.num_standard		= ARRAY_SIZE(cfg80211_handlers),
1520 	.standard		= cfg80211_handlers,
1521 	.get_wireless_stats = cfg80211_wireless_stats,
1522 };
1523