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