xref: /linux/net/wireless/wext-compat.c (revision 3ba84ac69b53e6ee07c31d54554e00793d7b144f)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * cfg80211 - wext compat code
4  *
5  * This is temporary code until all wireless functionality is migrated
6  * into cfg80211, when that happens all the exports here go away and
7  * we directly assign the wireless handlers of wireless interfaces.
8  *
9  * Copyright 2008-2009	Johannes Berg <johannes@sipsolutions.net>
10  * Copyright (C) 2019-2023 Intel Corporation
11  */
12 
13 #include <linux/export.h>
14 #include <linux/wireless.h>
15 #include <linux/nl80211.h>
16 #include <linux/if_arp.h>
17 #include <linux/etherdevice.h>
18 #include <linux/slab.h>
19 #include <net/iw_handler.h>
20 #include <net/cfg80211.h>
21 #include <net/cfg80211-wext.h>
22 #include "wext-compat.h"
23 #include "core.h"
24 #include "rdev-ops.h"
25 
26 int cfg80211_wext_giwname(struct net_device *dev,
27 			  struct iw_request_info *info,
28 			  union iwreq_data *wrqu, char *extra)
29 {
30 	strcpy(wrqu->name, "IEEE 802.11");
31 	return 0;
32 }
33 
34 int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
35 			  union iwreq_data *wrqu, char *extra)
36 {
37 	__u32 *mode = &wrqu->mode;
38 	struct wireless_dev *wdev = dev->ieee80211_ptr;
39 	struct cfg80211_registered_device *rdev;
40 	struct vif_params vifparams;
41 	enum nl80211_iftype type;
42 	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 
72 int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
73 			  union iwreq_data *wrqu, char *extra)
74 {
75 	__u32 *mode = &wrqu->mode;
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 
107 
108 int cfg80211_wext_giwrange(struct net_device *dev,
109 			   struct iw_request_info *info,
110 			   union iwreq_data *wrqu, char *extra)
111 {
112 	struct iw_point *data = &wrqu->data;
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 
221 
222 /**
223  * cfg80211_wext_freq - get wext frequency for non-"auto"
224  * @freq: the wext freq encoding
225  *
226  * Returns: a frequency, or a negative error code, or 0 for auto.
227  */
228 int cfg80211_wext_freq(struct iw_freq *freq)
229 {
230 	/*
231 	 * Parse frequency - return 0 for auto and
232 	 * -EINVAL for impossible things.
233 	 */
234 	if (freq->e == 0) {
235 		enum nl80211_band band = NL80211_BAND_2GHZ;
236 		if (freq->m < 0)
237 			return 0;
238 		if (freq->m > 14)
239 			band = NL80211_BAND_5GHZ;
240 		return ieee80211_channel_to_frequency(freq->m, band);
241 	} else {
242 		int i, div = 1000000;
243 		for (i = 0; i < freq->e; i++)
244 			div /= 10;
245 		if (div <= 0)
246 			return -EINVAL;
247 		return freq->m / div;
248 	}
249 }
250 
251 int cfg80211_wext_siwrts(struct net_device *dev,
252 			 struct iw_request_info *info,
253 			 union iwreq_data *wrqu, char *extra)
254 {
255 	struct iw_param *rts = &wrqu->rts;
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 
281 int cfg80211_wext_giwrts(struct net_device *dev,
282 			 struct iw_request_info *info,
283 			 union iwreq_data *wrqu, char *extra)
284 {
285 	struct iw_param *rts = &wrqu->rts;
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 
295 int cfg80211_wext_siwfrag(struct net_device *dev,
296 			  struct iw_request_info *info,
297 			  union iwreq_data *wrqu, char *extra)
298 {
299 	struct iw_param *frag = &wrqu->frag;
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 
325 int cfg80211_wext_giwfrag(struct net_device *dev,
326 			  struct iw_request_info *info,
327 			  union iwreq_data *wrqu, char *extra)
328 {
329 	struct iw_param *frag = &wrqu->frag;
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 
339 static int cfg80211_wext_siwretry(struct net_device *dev,
340 				  struct iw_request_info *info,
341 				  union iwreq_data *wrqu, char *extra)
342 {
343 	struct iw_param *retry = &wrqu->retry;
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 			   union iwreq_data *wrqu, char *extra)
382 {
383 	struct iw_param *retry = &wrqu->retry;
384 	struct wireless_dev *wdev = dev->ieee80211_ptr;
385 
386 	retry->disabled = 0;
387 
388 	if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
389 		/*
390 		 * First return short value, iwconfig will ask long value
391 		 * later if needed
392 		 */
393 		retry->flags |= IW_RETRY_LIMIT | IW_RETRY_SHORT;
394 		retry->value = wdev->wiphy->retry_short;
395 		if (wdev->wiphy->retry_long == wdev->wiphy->retry_short)
396 			retry->flags |= IW_RETRY_LONG;
397 
398 		return 0;
399 	}
400 
401 	if (retry->flags & IW_RETRY_LONG) {
402 		retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
403 		retry->value = wdev->wiphy->retry_long;
404 	}
405 
406 	return 0;
407 }
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 < 4; 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_wext_siwencode(struct net_device *dev,
575 				   struct iw_request_info *info,
576 				   union iwreq_data *wrqu, char *keybuf)
577 {
578 	struct iw_point *erq = &wrqu->encoding;
579 	struct wireless_dev *wdev = dev->ieee80211_ptr;
580 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
581 	int idx, err;
582 	bool remove = false;
583 	struct key_params params;
584 
585 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
586 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
587 		return -EOPNOTSUPP;
588 
589 	/* no use -- only MFP (set_default_mgmt_key) is optional */
590 	if (!rdev->ops->del_key ||
591 	    !rdev->ops->add_key ||
592 	    !rdev->ops->set_default_key)
593 		return -EOPNOTSUPP;
594 
595 	wiphy_lock(&rdev->wiphy);
596 	if (wdev->valid_links) {
597 		err = -EOPNOTSUPP;
598 		goto out;
599 	}
600 
601 	idx = erq->flags & IW_ENCODE_INDEX;
602 	if (idx == 0) {
603 		idx = wdev->wext.default_key;
604 		if (idx < 0)
605 			idx = 0;
606 	} else if (idx < 1 || idx > 4) {
607 		err = -EINVAL;
608 		goto out;
609 	} else {
610 		idx--;
611 	}
612 
613 	if (erq->flags & IW_ENCODE_DISABLED)
614 		remove = true;
615 	else if (erq->length == 0) {
616 		/* No key data - just set the default TX key index */
617 		err = 0;
618 		if (wdev->connected ||
619 		    (wdev->iftype == NL80211_IFTYPE_ADHOC &&
620 		     wdev->u.ibss.current_bss))
621 			err = rdev_set_default_key(rdev, dev, -1, idx, true,
622 						   true);
623 		if (!err)
624 			wdev->wext.default_key = idx;
625 		goto out;
626 	}
627 
628 	memset(&params, 0, sizeof(params));
629 	params.key = keybuf;
630 	params.key_len = erq->length;
631 	if (erq->length == 5) {
632 		params.cipher = WLAN_CIPHER_SUITE_WEP40;
633 	} else if (erq->length == 13) {
634 		params.cipher = WLAN_CIPHER_SUITE_WEP104;
635 	} else if (!remove) {
636 		err = -EINVAL;
637 		goto out;
638 	}
639 
640 	err = cfg80211_set_encryption(rdev, dev, false, NULL, remove,
641 				      wdev->wext.default_key == -1,
642 				      idx, &params);
643 out:
644 	wiphy_unlock(&rdev->wiphy);
645 
646 	return err;
647 }
648 
649 static int cfg80211_wext_siwencodeext(struct net_device *dev,
650 				      struct iw_request_info *info,
651 				      union iwreq_data *wrqu, char *extra)
652 {
653 	struct iw_point *erq = &wrqu->encoding;
654 	struct wireless_dev *wdev = dev->ieee80211_ptr;
655 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
656 	struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
657 	const u8 *addr;
658 	int idx;
659 	bool remove = false;
660 	struct key_params params;
661 	u32 cipher;
662 	int ret;
663 
664 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
665 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
666 		return -EOPNOTSUPP;
667 
668 	/* no use -- only MFP (set_default_mgmt_key) is optional */
669 	if (!rdev->ops->del_key ||
670 	    !rdev->ops->add_key ||
671 	    !rdev->ops->set_default_key)
672 		return -EOPNOTSUPP;
673 
674 	if (wdev->valid_links)
675 		return -EOPNOTSUPP;
676 
677 	switch (ext->alg) {
678 	case IW_ENCODE_ALG_NONE:
679 		remove = true;
680 		cipher = 0;
681 		break;
682 	case IW_ENCODE_ALG_WEP:
683 		if (ext->key_len == 5)
684 			cipher = WLAN_CIPHER_SUITE_WEP40;
685 		else if (ext->key_len == 13)
686 			cipher = WLAN_CIPHER_SUITE_WEP104;
687 		else
688 			return -EINVAL;
689 		break;
690 	case IW_ENCODE_ALG_TKIP:
691 		cipher = WLAN_CIPHER_SUITE_TKIP;
692 		break;
693 	case IW_ENCODE_ALG_CCMP:
694 		cipher = WLAN_CIPHER_SUITE_CCMP;
695 		break;
696 	case IW_ENCODE_ALG_AES_CMAC:
697 		cipher = WLAN_CIPHER_SUITE_AES_CMAC;
698 		break;
699 	default:
700 		return -EOPNOTSUPP;
701 	}
702 
703 	if (erq->flags & IW_ENCODE_DISABLED)
704 		remove = true;
705 
706 	idx = erq->flags & IW_ENCODE_INDEX;
707 	if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
708 		if (idx < 4 || idx > 5) {
709 			idx = wdev->wext.default_mgmt_key;
710 			if (idx < 0)
711 				return -EINVAL;
712 		} else
713 			idx--;
714 	} else {
715 		if (idx < 1 || idx > 4) {
716 			idx = wdev->wext.default_key;
717 			if (idx < 0)
718 				return -EINVAL;
719 		} else
720 			idx--;
721 	}
722 
723 	addr = ext->addr.sa_data;
724 	if (is_broadcast_ether_addr(addr))
725 		addr = NULL;
726 
727 	memset(&params, 0, sizeof(params));
728 	params.key = ext->key;
729 	params.key_len = ext->key_len;
730 	params.cipher = cipher;
731 
732 	if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
733 		params.seq = ext->rx_seq;
734 		params.seq_len = 6;
735 	}
736 
737 	wiphy_lock(wdev->wiphy);
738 	ret = cfg80211_set_encryption(
739 			rdev, dev,
740 			!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
741 			addr, remove,
742 			ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
743 			idx, &params);
744 	wiphy_unlock(wdev->wiphy);
745 
746 	return ret;
747 }
748 
749 static int cfg80211_wext_giwencode(struct net_device *dev,
750 				   struct iw_request_info *info,
751 				   union iwreq_data *wrqu, char *keybuf)
752 {
753 	struct iw_point *erq = &wrqu->encoding;
754 	struct wireless_dev *wdev = dev->ieee80211_ptr;
755 	int idx;
756 
757 	if (wdev->iftype != NL80211_IFTYPE_STATION &&
758 	    wdev->iftype != NL80211_IFTYPE_ADHOC)
759 		return -EOPNOTSUPP;
760 
761 	idx = erq->flags & IW_ENCODE_INDEX;
762 	if (idx == 0) {
763 		idx = wdev->wext.default_key;
764 		if (idx < 0)
765 			idx = 0;
766 	} else if (idx < 1 || idx > 4)
767 		return -EINVAL;
768 	else
769 		idx--;
770 
771 	erq->flags = idx + 1;
772 
773 	if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
774 		erq->flags |= IW_ENCODE_DISABLED;
775 		erq->length = 0;
776 		return 0;
777 	}
778 
779 	erq->length = min_t(size_t, erq->length,
780 			    wdev->wext.keys->params[idx].key_len);
781 	memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
782 	erq->flags |= IW_ENCODE_ENABLED;
783 
784 	return 0;
785 }
786 
787 static int cfg80211_wext_siwfreq(struct net_device *dev,
788 				 struct iw_request_info *info,
789 				 union iwreq_data *wrqu, char *extra)
790 {
791 	struct iw_freq *wextfreq = &wrqu->freq;
792 	struct wireless_dev *wdev = dev->ieee80211_ptr;
793 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
794 	struct cfg80211_chan_def chandef = {
795 		.width = NL80211_CHAN_WIDTH_20_NOHT,
796 	};
797 	int freq, ret;
798 
799 	wiphy_lock(&rdev->wiphy);
800 
801 	switch (wdev->iftype) {
802 	case NL80211_IFTYPE_STATION:
803 		ret = cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
804 		break;
805 	case NL80211_IFTYPE_ADHOC:
806 		ret = cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
807 		break;
808 	case NL80211_IFTYPE_MONITOR:
809 		freq = cfg80211_wext_freq(wextfreq);
810 		if (freq < 0) {
811 			ret = freq;
812 			break;
813 		}
814 		if (freq == 0) {
815 			ret = -EINVAL;
816 			break;
817 		}
818 		chandef.center_freq1 = freq;
819 		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
820 		if (!chandef.chan) {
821 			ret = -EINVAL;
822 			break;
823 		}
824 		ret = cfg80211_set_monitor_channel(rdev, &chandef);
825 		break;
826 	case NL80211_IFTYPE_MESH_POINT:
827 		freq = cfg80211_wext_freq(wextfreq);
828 		if (freq < 0) {
829 			ret = freq;
830 			break;
831 		}
832 		if (freq == 0) {
833 			ret = -EINVAL;
834 			break;
835 		}
836 		chandef.center_freq1 = freq;
837 		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
838 		if (!chandef.chan) {
839 			ret = -EINVAL;
840 			break;
841 		}
842 		ret = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
843 		break;
844 	default:
845 		ret = -EOPNOTSUPP;
846 		break;
847 	}
848 
849 	wiphy_unlock(&rdev->wiphy);
850 
851 	return ret;
852 }
853 
854 static int cfg80211_wext_giwfreq(struct net_device *dev,
855 				 struct iw_request_info *info,
856 				 union iwreq_data *wrqu, char *extra)
857 {
858 	struct iw_freq *freq = &wrqu->freq;
859 	struct wireless_dev *wdev = dev->ieee80211_ptr;
860 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
861 	struct cfg80211_chan_def chandef = {};
862 	int ret;
863 
864 	wiphy_lock(&rdev->wiphy);
865 	switch (wdev->iftype) {
866 	case NL80211_IFTYPE_STATION:
867 		ret = cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
868 		break;
869 	case NL80211_IFTYPE_ADHOC:
870 		ret = cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
871 		break;
872 	case NL80211_IFTYPE_MONITOR:
873 		if (!rdev->ops->get_channel) {
874 			ret = -EINVAL;
875 			break;
876 		}
877 
878 		ret = rdev_get_channel(rdev, wdev, 0, &chandef);
879 		if (ret)
880 			break;
881 		freq->m = chandef.chan->center_freq;
882 		freq->e = 6;
883 		ret = 0;
884 		break;
885 	default:
886 		ret = -EINVAL;
887 		break;
888 	}
889 
890 	wiphy_unlock(&rdev->wiphy);
891 
892 	return ret;
893 }
894 
895 static int cfg80211_wext_siwtxpower(struct net_device *dev,
896 				    struct iw_request_info *info,
897 				    union iwreq_data *data, char *extra)
898 {
899 	struct wireless_dev *wdev = dev->ieee80211_ptr;
900 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
901 	enum nl80211_tx_power_setting type;
902 	int dbm = 0;
903 	int ret;
904 
905 	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
906 		return -EINVAL;
907 	if (data->txpower.flags & IW_TXPOW_RANGE)
908 		return -EINVAL;
909 
910 	if (!rdev->ops->set_tx_power)
911 		return -EOPNOTSUPP;
912 
913 	/* only change when not disabling */
914 	if (!data->txpower.disabled) {
915 		rfkill_set_sw_state(rdev->wiphy.rfkill, false);
916 
917 		if (data->txpower.fixed) {
918 			/*
919 			 * wext doesn't support negative values, see
920 			 * below where it's for automatic
921 			 */
922 			if (data->txpower.value < 0)
923 				return -EINVAL;
924 			dbm = data->txpower.value;
925 			type = NL80211_TX_POWER_FIXED;
926 			/* TODO: do regulatory check! */
927 		} else {
928 			/*
929 			 * Automatic power level setting, max being the value
930 			 * passed in from userland.
931 			 */
932 			if (data->txpower.value < 0) {
933 				type = NL80211_TX_POWER_AUTOMATIC;
934 			} else {
935 				dbm = data->txpower.value;
936 				type = NL80211_TX_POWER_LIMITED;
937 			}
938 		}
939 	} else {
940 		if (rfkill_set_sw_state(rdev->wiphy.rfkill, true))
941 			schedule_work(&rdev->rfkill_block);
942 		return 0;
943 	}
944 
945 	wiphy_lock(&rdev->wiphy);
946 	ret = rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
947 	wiphy_unlock(&rdev->wiphy);
948 
949 	return ret;
950 }
951 
952 static int cfg80211_wext_giwtxpower(struct net_device *dev,
953 				    struct iw_request_info *info,
954 				    union iwreq_data *data, char *extra)
955 {
956 	struct wireless_dev *wdev = dev->ieee80211_ptr;
957 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
958 	int err, val;
959 
960 	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
961 		return -EINVAL;
962 	if (data->txpower.flags & IW_TXPOW_RANGE)
963 		return -EINVAL;
964 
965 	if (!rdev->ops->get_tx_power)
966 		return -EOPNOTSUPP;
967 
968 	wiphy_lock(&rdev->wiphy);
969 	err = rdev_get_tx_power(rdev, wdev, &val);
970 	wiphy_unlock(&rdev->wiphy);
971 	if (err)
972 		return err;
973 
974 	/* well... oh well */
975 	data->txpower.fixed = 1;
976 	data->txpower.disabled = rfkill_blocked(rdev->wiphy.rfkill);
977 	data->txpower.value = val;
978 	data->txpower.flags = IW_TXPOW_DBM;
979 
980 	return 0;
981 }
982 
983 static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
984 				 s32 auth_alg)
985 {
986 	int nr_alg = 0;
987 
988 	if (!auth_alg)
989 		return -EINVAL;
990 
991 	if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
992 			 IW_AUTH_ALG_SHARED_KEY |
993 			 IW_AUTH_ALG_LEAP))
994 		return -EINVAL;
995 
996 	if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
997 		nr_alg++;
998 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
999 	}
1000 
1001 	if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
1002 		nr_alg++;
1003 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
1004 	}
1005 
1006 	if (auth_alg & IW_AUTH_ALG_LEAP) {
1007 		nr_alg++;
1008 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
1009 	}
1010 
1011 	if (nr_alg > 1)
1012 		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
1013 
1014 	return 0;
1015 }
1016 
1017 static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
1018 {
1019 	if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
1020 			     IW_AUTH_WPA_VERSION_WPA2|
1021 		             IW_AUTH_WPA_VERSION_DISABLED))
1022 		return -EINVAL;
1023 
1024 	if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
1025 	    (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
1026 			     IW_AUTH_WPA_VERSION_WPA2)))
1027 		return -EINVAL;
1028 
1029 	if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
1030 		wdev->wext.connect.crypto.wpa_versions &=
1031 			~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
1032 
1033 	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
1034 		wdev->wext.connect.crypto.wpa_versions |=
1035 			NL80211_WPA_VERSION_1;
1036 
1037 	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
1038 		wdev->wext.connect.crypto.wpa_versions |=
1039 			NL80211_WPA_VERSION_2;
1040 
1041 	return 0;
1042 }
1043 
1044 static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
1045 {
1046 	if (cipher & IW_AUTH_CIPHER_WEP40)
1047 		wdev->wext.connect.crypto.cipher_group =
1048 			WLAN_CIPHER_SUITE_WEP40;
1049 	else if (cipher & IW_AUTH_CIPHER_WEP104)
1050 		wdev->wext.connect.crypto.cipher_group =
1051 			WLAN_CIPHER_SUITE_WEP104;
1052 	else if (cipher & IW_AUTH_CIPHER_TKIP)
1053 		wdev->wext.connect.crypto.cipher_group =
1054 			WLAN_CIPHER_SUITE_TKIP;
1055 	else if (cipher & IW_AUTH_CIPHER_CCMP)
1056 		wdev->wext.connect.crypto.cipher_group =
1057 			WLAN_CIPHER_SUITE_CCMP;
1058 	else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
1059 		wdev->wext.connect.crypto.cipher_group =
1060 			WLAN_CIPHER_SUITE_AES_CMAC;
1061 	else if (cipher & IW_AUTH_CIPHER_NONE)
1062 		wdev->wext.connect.crypto.cipher_group = 0;
1063 	else
1064 		return -EINVAL;
1065 
1066 	return 0;
1067 }
1068 
1069 static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
1070 {
1071 	int nr_ciphers = 0;
1072 	u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
1073 
1074 	if (cipher & IW_AUTH_CIPHER_WEP40) {
1075 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
1076 		nr_ciphers++;
1077 	}
1078 
1079 	if (cipher & IW_AUTH_CIPHER_WEP104) {
1080 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1081 		nr_ciphers++;
1082 	}
1083 
1084 	if (cipher & IW_AUTH_CIPHER_TKIP) {
1085 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1086 		nr_ciphers++;
1087 	}
1088 
1089 	if (cipher & IW_AUTH_CIPHER_CCMP) {
1090 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1091 		nr_ciphers++;
1092 	}
1093 
1094 	if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1095 		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1096 		nr_ciphers++;
1097 	}
1098 
1099 	BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1100 
1101 	wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1102 
1103 	return 0;
1104 }
1105 
1106 
1107 static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
1108 {
1109 	int nr_akm_suites = 0;
1110 
1111 	if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1112 			IW_AUTH_KEY_MGMT_PSK))
1113 		return -EINVAL;
1114 
1115 	if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1116 		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1117 			WLAN_AKM_SUITE_8021X;
1118 		nr_akm_suites++;
1119 	}
1120 
1121 	if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1122 		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1123 			WLAN_AKM_SUITE_PSK;
1124 		nr_akm_suites++;
1125 	}
1126 
1127 	wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1128 
1129 	return 0;
1130 }
1131 
1132 static int cfg80211_wext_siwauth(struct net_device *dev,
1133 				 struct iw_request_info *info,
1134 				 union iwreq_data *wrqu, char *extra)
1135 {
1136 	struct iw_param *data = &wrqu->param;
1137 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1138 
1139 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1140 		return -EOPNOTSUPP;
1141 
1142 	switch (data->flags & IW_AUTH_INDEX) {
1143 	case IW_AUTH_PRIVACY_INVOKED:
1144 		wdev->wext.connect.privacy = data->value;
1145 		return 0;
1146 	case IW_AUTH_WPA_VERSION:
1147 		return cfg80211_set_wpa_version(wdev, data->value);
1148 	case IW_AUTH_CIPHER_GROUP:
1149 		return cfg80211_set_cipher_group(wdev, data->value);
1150 	case IW_AUTH_KEY_MGMT:
1151 		return cfg80211_set_key_mgt(wdev, data->value);
1152 	case IW_AUTH_CIPHER_PAIRWISE:
1153 		return cfg80211_set_cipher_pairwise(wdev, data->value);
1154 	case IW_AUTH_80211_AUTH_ALG:
1155 		return cfg80211_set_auth_alg(wdev, data->value);
1156 	case IW_AUTH_WPA_ENABLED:
1157 	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1158 	case IW_AUTH_DROP_UNENCRYPTED:
1159 	case IW_AUTH_MFP:
1160 		return 0;
1161 	default:
1162 		return -EOPNOTSUPP;
1163 	}
1164 }
1165 
1166 static int cfg80211_wext_giwauth(struct net_device *dev,
1167 				 struct iw_request_info *info,
1168 				 union iwreq_data *wrqu, char *extra)
1169 {
1170 	/* XXX: what do we need? */
1171 
1172 	return -EOPNOTSUPP;
1173 }
1174 
1175 static int cfg80211_wext_siwpower(struct net_device *dev,
1176 				  struct iw_request_info *info,
1177 				  union iwreq_data *wrqu, char *extra)
1178 {
1179 	struct iw_param *wrq = &wrqu->power;
1180 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1181 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1182 	bool ps;
1183 	int timeout = wdev->ps_timeout;
1184 	int err;
1185 
1186 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1187 		return -EINVAL;
1188 
1189 	if (!rdev->ops->set_power_mgmt)
1190 		return -EOPNOTSUPP;
1191 
1192 	if (wrq->disabled) {
1193 		ps = false;
1194 	} else {
1195 		switch (wrq->flags & IW_POWER_MODE) {
1196 		case IW_POWER_ON:       /* If not specified */
1197 		case IW_POWER_MODE:     /* If set all mask */
1198 		case IW_POWER_ALL_R:    /* If explicitly state all */
1199 			ps = true;
1200 			break;
1201 		default:                /* Otherwise we ignore */
1202 			return -EINVAL;
1203 		}
1204 
1205 		if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1206 			return -EINVAL;
1207 
1208 		if (wrq->flags & IW_POWER_TIMEOUT)
1209 			timeout = wrq->value / 1000;
1210 	}
1211 
1212 	wiphy_lock(&rdev->wiphy);
1213 	err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
1214 	wiphy_unlock(&rdev->wiphy);
1215 	if (err)
1216 		return err;
1217 
1218 	wdev->ps = ps;
1219 	wdev->ps_timeout = timeout;
1220 
1221 	return 0;
1222 
1223 }
1224 
1225 static int cfg80211_wext_giwpower(struct net_device *dev,
1226 				  struct iw_request_info *info,
1227 				  union iwreq_data *wrqu, char *extra)
1228 {
1229 	struct iw_param *wrq = &wrqu->power;
1230 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1231 
1232 	wrq->disabled = !wdev->ps;
1233 
1234 	return 0;
1235 }
1236 
1237 static int cfg80211_wext_siwrate(struct net_device *dev,
1238 				 struct iw_request_info *info,
1239 				 union iwreq_data *wrqu, char *extra)
1240 {
1241 	struct iw_param *rate = &wrqu->bitrate;
1242 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1243 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1244 	struct cfg80211_bitrate_mask mask;
1245 	u32 fixed, maxrate;
1246 	struct ieee80211_supported_band *sband;
1247 	int band, ridx, ret;
1248 	bool match = false;
1249 
1250 	if (!rdev->ops->set_bitrate_mask)
1251 		return -EOPNOTSUPP;
1252 
1253 	memset(&mask, 0, sizeof(mask));
1254 	fixed = 0;
1255 	maxrate = (u32)-1;
1256 
1257 	if (rate->value < 0) {
1258 		/* nothing */
1259 	} else if (rate->fixed) {
1260 		fixed = rate->value / 100000;
1261 	} else {
1262 		maxrate = rate->value / 100000;
1263 	}
1264 
1265 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
1266 		sband = wdev->wiphy->bands[band];
1267 		if (sband == NULL)
1268 			continue;
1269 		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1270 			struct ieee80211_rate *srate = &sband->bitrates[ridx];
1271 			if (fixed == srate->bitrate) {
1272 				mask.control[band].legacy = 1 << ridx;
1273 				match = true;
1274 				break;
1275 			}
1276 			if (srate->bitrate <= maxrate) {
1277 				mask.control[band].legacy |= 1 << ridx;
1278 				match = true;
1279 			}
1280 		}
1281 	}
1282 
1283 	if (!match)
1284 		return -EINVAL;
1285 
1286 	wiphy_lock(&rdev->wiphy);
1287 	if (dev->ieee80211_ptr->valid_links)
1288 		ret = -EOPNOTSUPP;
1289 	else
1290 		ret = rdev_set_bitrate_mask(rdev, dev, 0, NULL, &mask);
1291 	wiphy_unlock(&rdev->wiphy);
1292 
1293 	return ret;
1294 }
1295 
1296 static int cfg80211_wext_giwrate(struct net_device *dev,
1297 				 struct iw_request_info *info,
1298 				 union iwreq_data *wrqu, char *extra)
1299 {
1300 	struct iw_param *rate = &wrqu->bitrate;
1301 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1302 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1303 	struct station_info sinfo = {};
1304 	u8 addr[ETH_ALEN];
1305 	int err;
1306 
1307 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1308 		return -EOPNOTSUPP;
1309 
1310 	if (!rdev->ops->get_station)
1311 		return -EOPNOTSUPP;
1312 
1313 	err = 0;
1314 	if (!wdev->valid_links && wdev->links[0].client.current_bss)
1315 		memcpy(addr, wdev->links[0].client.current_bss->pub.bssid,
1316 		       ETH_ALEN);
1317 	else
1318 		err = -EOPNOTSUPP;
1319 	if (err)
1320 		return err;
1321 
1322 	wiphy_lock(&rdev->wiphy);
1323 	err = rdev_get_station(rdev, dev, addr, &sinfo);
1324 	wiphy_unlock(&rdev->wiphy);
1325 	if (err)
1326 		return err;
1327 
1328 	if (!(sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
1329 		err = -EOPNOTSUPP;
1330 		goto free;
1331 	}
1332 
1333 	rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
1334 
1335 free:
1336 	cfg80211_sinfo_release_content(&sinfo);
1337 	return err;
1338 }
1339 
1340 /* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
1341 static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
1342 {
1343 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1344 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1345 	/* we are under RTNL - globally locked - so can use static structs */
1346 	static struct iw_statistics wstats;
1347 	static struct station_info sinfo = {};
1348 	u8 bssid[ETH_ALEN];
1349 	int ret;
1350 
1351 	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1352 		return NULL;
1353 
1354 	if (!rdev->ops->get_station)
1355 		return NULL;
1356 
1357 	/* Grab BSSID of current BSS, if any */
1358 	wiphy_lock(&rdev->wiphy);
1359 	if (wdev->valid_links || !wdev->links[0].client.current_bss) {
1360 		wiphy_unlock(&rdev->wiphy);
1361 		return NULL;
1362 	}
1363 	memcpy(bssid, wdev->links[0].client.current_bss->pub.bssid, ETH_ALEN);
1364 
1365 	memset(&sinfo, 0, sizeof(sinfo));
1366 
1367 	ret = rdev_get_station(rdev, dev, bssid, &sinfo);
1368 	wiphy_unlock(&rdev->wiphy);
1369 
1370 	if (ret)
1371 		return NULL;
1372 
1373 	memset(&wstats, 0, sizeof(wstats));
1374 
1375 	switch (rdev->wiphy.signal_type) {
1376 	case CFG80211_SIGNAL_TYPE_MBM:
1377 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1378 			int sig = sinfo.signal;
1379 			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1380 			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1381 			wstats.qual.updated |= IW_QUAL_DBM;
1382 			wstats.qual.level = sig;
1383 			if (sig < -110)
1384 				sig = -110;
1385 			else if (sig > -40)
1386 				sig = -40;
1387 			wstats.qual.qual = sig + 110;
1388 			break;
1389 		}
1390 		fallthrough;
1391 	case CFG80211_SIGNAL_TYPE_UNSPEC:
1392 		if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1393 			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1394 			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1395 			wstats.qual.level = sinfo.signal;
1396 			wstats.qual.qual = sinfo.signal;
1397 			break;
1398 		}
1399 		fallthrough;
1400 	default:
1401 		wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1402 		wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1403 	}
1404 
1405 	wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1406 	if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC))
1407 		wstats.discard.misc = sinfo.rx_dropped_misc;
1408 	if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))
1409 		wstats.discard.retries = sinfo.tx_failed;
1410 
1411 	cfg80211_sinfo_release_content(&sinfo);
1412 
1413 	return &wstats;
1414 }
1415 
1416 static int cfg80211_wext_siwap(struct net_device *dev,
1417 			       struct iw_request_info *info,
1418 			       union iwreq_data *wrqu, char *extra)
1419 {
1420 	struct sockaddr *ap_addr = &wrqu->ap_addr;
1421 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1422 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1423 	int ret;
1424 
1425 	wiphy_lock(&rdev->wiphy);
1426 	switch (wdev->iftype) {
1427 	case NL80211_IFTYPE_ADHOC:
1428 		ret = cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1429 		break;
1430 	case NL80211_IFTYPE_STATION:
1431 		ret = cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1432 		break;
1433 	default:
1434 		ret = -EOPNOTSUPP;
1435 		break;
1436 	}
1437 	wiphy_unlock(&rdev->wiphy);
1438 
1439 	return ret;
1440 }
1441 
1442 static int cfg80211_wext_giwap(struct net_device *dev,
1443 			       struct iw_request_info *info,
1444 			       union iwreq_data *wrqu, char *extra)
1445 {
1446 	struct sockaddr *ap_addr = &wrqu->ap_addr;
1447 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1448 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1449 	int ret;
1450 
1451 	wiphy_lock(&rdev->wiphy);
1452 	switch (wdev->iftype) {
1453 	case NL80211_IFTYPE_ADHOC:
1454 		ret = cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1455 		break;
1456 	case NL80211_IFTYPE_STATION:
1457 		ret = cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1458 		break;
1459 	default:
1460 		ret = -EOPNOTSUPP;
1461 		break;
1462 	}
1463 	wiphy_unlock(&rdev->wiphy);
1464 
1465 	return ret;
1466 }
1467 
1468 static int cfg80211_wext_siwessid(struct net_device *dev,
1469 				  struct iw_request_info *info,
1470 				  union iwreq_data *wrqu, char *ssid)
1471 {
1472 	struct iw_point *data = &wrqu->data;
1473 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1474 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1475 	int ret;
1476 
1477 	wiphy_lock(&rdev->wiphy);
1478 	switch (wdev->iftype) {
1479 	case NL80211_IFTYPE_ADHOC:
1480 		ret = cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1481 		break;
1482 	case NL80211_IFTYPE_STATION:
1483 		ret = cfg80211_mgd_wext_siwessid(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_giwessid(struct net_device *dev,
1495 				  struct iw_request_info *info,
1496 				  union iwreq_data *wrqu, char *ssid)
1497 {
1498 	struct iw_point *data = &wrqu->data;
1499 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1500 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1501 	int ret;
1502 
1503 	data->flags = 0;
1504 	data->length = 0;
1505 
1506 	wiphy_lock(&rdev->wiphy);
1507 	switch (wdev->iftype) {
1508 	case NL80211_IFTYPE_ADHOC:
1509 		ret = cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1510 		break;
1511 	case NL80211_IFTYPE_STATION:
1512 		ret = cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1513 		break;
1514 	default:
1515 		ret = -EOPNOTSUPP;
1516 		break;
1517 	}
1518 	wiphy_unlock(&rdev->wiphy);
1519 
1520 	return ret;
1521 }
1522 
1523 static int cfg80211_wext_siwpmksa(struct net_device *dev,
1524 				  struct iw_request_info *info,
1525 				  union iwreq_data *wrqu, char *extra)
1526 {
1527 	struct wireless_dev *wdev = dev->ieee80211_ptr;
1528 	struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1529 	struct cfg80211_pmksa cfg_pmksa;
1530 	struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1531 	int ret;
1532 
1533 	memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1534 
1535 	if (wdev->iftype != NL80211_IFTYPE_STATION)
1536 		return -EINVAL;
1537 
1538 	cfg_pmksa.bssid = pmksa->bssid.sa_data;
1539 	cfg_pmksa.pmkid = pmksa->pmkid;
1540 
1541 	wiphy_lock(&rdev->wiphy);
1542 	switch (pmksa->cmd) {
1543 	case IW_PMKSA_ADD:
1544 		if (!rdev->ops->set_pmksa) {
1545 			ret = -EOPNOTSUPP;
1546 			break;
1547 		}
1548 
1549 		ret = rdev_set_pmksa(rdev, dev, &cfg_pmksa);
1550 		break;
1551 	case IW_PMKSA_REMOVE:
1552 		if (!rdev->ops->del_pmksa) {
1553 			ret = -EOPNOTSUPP;
1554 			break;
1555 		}
1556 
1557 		ret = rdev_del_pmksa(rdev, dev, &cfg_pmksa);
1558 		break;
1559 	case IW_PMKSA_FLUSH:
1560 		if (!rdev->ops->flush_pmksa) {
1561 			ret = -EOPNOTSUPP;
1562 			break;
1563 		}
1564 
1565 		ret = rdev_flush_pmksa(rdev, dev);
1566 		break;
1567 	default:
1568 		ret = -EOPNOTSUPP;
1569 		break;
1570 	}
1571 	wiphy_unlock(&rdev->wiphy);
1572 
1573 	return ret;
1574 }
1575 
1576 static const iw_handler cfg80211_handlers[] = {
1577 	IW_HANDLER(SIOCGIWNAME,		cfg80211_wext_giwname),
1578 	IW_HANDLER(SIOCSIWFREQ,		cfg80211_wext_siwfreq),
1579 	IW_HANDLER(SIOCGIWFREQ,		cfg80211_wext_giwfreq),
1580 	IW_HANDLER(SIOCSIWMODE,		cfg80211_wext_siwmode),
1581 	IW_HANDLER(SIOCGIWMODE,		cfg80211_wext_giwmode),
1582 	IW_HANDLER(SIOCGIWRANGE,	cfg80211_wext_giwrange),
1583 	IW_HANDLER(SIOCSIWAP,		cfg80211_wext_siwap),
1584 	IW_HANDLER(SIOCGIWAP,		cfg80211_wext_giwap),
1585 	IW_HANDLER(SIOCSIWMLME,		cfg80211_wext_siwmlme),
1586 	IW_HANDLER(SIOCSIWSCAN,		cfg80211_wext_siwscan),
1587 	IW_HANDLER(SIOCGIWSCAN,		cfg80211_wext_giwscan),
1588 	IW_HANDLER(SIOCSIWESSID,	cfg80211_wext_siwessid),
1589 	IW_HANDLER(SIOCGIWESSID,	cfg80211_wext_giwessid),
1590 	IW_HANDLER(SIOCSIWRATE,		cfg80211_wext_siwrate),
1591 	IW_HANDLER(SIOCGIWRATE,		cfg80211_wext_giwrate),
1592 	IW_HANDLER(SIOCSIWRTS,		cfg80211_wext_siwrts),
1593 	IW_HANDLER(SIOCGIWRTS,		cfg80211_wext_giwrts),
1594 	IW_HANDLER(SIOCSIWFRAG,		cfg80211_wext_siwfrag),
1595 	IW_HANDLER(SIOCGIWFRAG,		cfg80211_wext_giwfrag),
1596 	IW_HANDLER(SIOCSIWTXPOW,	cfg80211_wext_siwtxpower),
1597 	IW_HANDLER(SIOCGIWTXPOW,	cfg80211_wext_giwtxpower),
1598 	IW_HANDLER(SIOCSIWRETRY,	cfg80211_wext_siwretry),
1599 	IW_HANDLER(SIOCGIWRETRY,	cfg80211_wext_giwretry),
1600 	IW_HANDLER(SIOCSIWENCODE,	cfg80211_wext_siwencode),
1601 	IW_HANDLER(SIOCGIWENCODE,	cfg80211_wext_giwencode),
1602 	IW_HANDLER(SIOCSIWPOWER,	cfg80211_wext_siwpower),
1603 	IW_HANDLER(SIOCGIWPOWER,	cfg80211_wext_giwpower),
1604 	IW_HANDLER(SIOCSIWGENIE,	cfg80211_wext_siwgenie),
1605 	IW_HANDLER(SIOCSIWAUTH,		cfg80211_wext_siwauth),
1606 	IW_HANDLER(SIOCGIWAUTH,		cfg80211_wext_giwauth),
1607 	IW_HANDLER(SIOCSIWENCODEEXT,	cfg80211_wext_siwencodeext),
1608 	IW_HANDLER(SIOCSIWPMKSA,	cfg80211_wext_siwpmksa),
1609 };
1610 
1611 const struct iw_handler_def cfg80211_wext_handler = {
1612 	.num_standard		= ARRAY_SIZE(cfg80211_handlers),
1613 	.standard		= cfg80211_handlers,
1614 	.get_wireless_stats = cfg80211_wireless_stats,
1615 };
1616