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