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