1 // SPDX-License-Identifier: GPL-2.0
3 * cfg80211 - wext compat code
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.
9 * Copyright 2008-2009 Johannes Berg <johannes@sipsolutions.net>
12 #include <linux/export.h>
13 #include <linux/wireless.h>
14 #include <linux/nl80211.h>
15 #include <linux/if_arp.h>
16 #include <linux/etherdevice.h>
17 #include <linux/slab.h>
18 #include <net/iw_handler.h>
19 #include <net/cfg80211.h>
20 #include <net/cfg80211-wext.h>
21 #include "wext-compat.h"
25 int cfg80211_wext_giwname(struct net_device *dev,
26 struct iw_request_info *info,
27 char *name, char *extra)
29 strcpy(name, "IEEE 802.11");
32 EXPORT_WEXT_HANDLER(cfg80211_wext_giwname);
34 int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
35 u32 *mode, char *extra)
37 struct wireless_dev *wdev = dev->ieee80211_ptr;
38 struct cfg80211_registered_device *rdev;
39 struct vif_params vifparams;
40 enum nl80211_iftype type;
42 rdev = wiphy_to_rdev(wdev->wiphy);
46 type = NL80211_IFTYPE_STATION;
49 type = NL80211_IFTYPE_ADHOC;
52 type = NL80211_IFTYPE_WDS;
55 type = NL80211_IFTYPE_MONITOR;
61 if (type == wdev->iftype)
64 memset(&vifparams, 0, sizeof(vifparams));
66 return cfg80211_change_iface(rdev, dev, type, &vifparams);
68 EXPORT_WEXT_HANDLER(cfg80211_wext_siwmode);
70 int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
71 u32 *mode, char *extra)
73 struct wireless_dev *wdev = dev->ieee80211_ptr;
78 switch (wdev->iftype) {
79 case NL80211_IFTYPE_AP:
80 *mode = IW_MODE_MASTER;
82 case NL80211_IFTYPE_STATION:
83 *mode = IW_MODE_INFRA;
85 case NL80211_IFTYPE_ADHOC:
86 *mode = IW_MODE_ADHOC;
88 case NL80211_IFTYPE_MONITOR:
89 *mode = IW_MODE_MONITOR;
91 case NL80211_IFTYPE_WDS:
92 *mode = IW_MODE_REPEAT;
94 case NL80211_IFTYPE_AP_VLAN:
95 *mode = IW_MODE_SECOND; /* FIXME */
103 EXPORT_WEXT_HANDLER(cfg80211_wext_giwmode);
106 int cfg80211_wext_giwrange(struct net_device *dev,
107 struct iw_request_info *info,
108 struct iw_point *data, char *extra)
110 struct wireless_dev *wdev = dev->ieee80211_ptr;
111 struct iw_range *range = (struct iw_range *) extra;
112 enum nl80211_band band;
118 data->length = sizeof(struct iw_range);
119 memset(range, 0, sizeof(struct iw_range));
121 range->we_version_compiled = WIRELESS_EXT;
122 range->we_version_source = 21;
123 range->retry_capa = IW_RETRY_LIMIT;
124 range->retry_flags = IW_RETRY_LIMIT;
125 range->min_retry = 0;
126 range->max_retry = 255;
128 range->max_rts = 2347;
129 range->min_frag = 256;
130 range->max_frag = 2346;
132 range->max_encoding_tokens = 4;
134 range->max_qual.updated = IW_QUAL_NOISE_INVALID;
136 switch (wdev->wiphy->signal_type) {
137 case CFG80211_SIGNAL_TYPE_NONE:
139 case CFG80211_SIGNAL_TYPE_MBM:
140 range->max_qual.level = (u8)-110;
141 range->max_qual.qual = 70;
142 range->avg_qual.qual = 35;
143 range->max_qual.updated |= IW_QUAL_DBM;
144 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
145 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
147 case CFG80211_SIGNAL_TYPE_UNSPEC:
148 range->max_qual.level = 100;
149 range->max_qual.qual = 100;
150 range->avg_qual.qual = 50;
151 range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
152 range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
156 range->avg_qual.level = range->max_qual.level / 2;
157 range->avg_qual.noise = range->max_qual.noise / 2;
158 range->avg_qual.updated = range->max_qual.updated;
160 for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
161 switch (wdev->wiphy->cipher_suites[i]) {
162 case WLAN_CIPHER_SUITE_TKIP:
163 range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
167 case WLAN_CIPHER_SUITE_CCMP:
168 range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
172 case WLAN_CIPHER_SUITE_WEP40:
173 range->encoding_size[range->num_encoding_sizes++] =
177 case WLAN_CIPHER_SUITE_WEP104:
178 range->encoding_size[range->num_encoding_sizes++] =
184 for (band = 0; band < NUM_NL80211_BANDS; band ++) {
185 struct ieee80211_supported_band *sband;
187 sband = wdev->wiphy->bands[band];
192 for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
193 struct ieee80211_channel *chan = &sband->channels[i];
195 if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
197 ieee80211_frequency_to_channel(
199 range->freq[c].m = chan->center_freq;
200 range->freq[c].e = 6;
205 range->num_channels = c;
206 range->num_frequency = c;
208 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
209 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
210 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
212 if (wdev->wiphy->max_scan_ssids > 0)
213 range->scan_capa |= IW_SCAN_CAPA_ESSID;
217 EXPORT_WEXT_HANDLER(cfg80211_wext_giwrange);
221 * cfg80211_wext_freq - get wext frequency for non-"auto"
222 * @dev: the net device
223 * @freq: the wext freq encoding
225 * Returns a frequency, or a negative error code, or 0 for auto.
227 int cfg80211_wext_freq(struct iw_freq *freq)
230 * Parse frequency - return 0 for auto and
231 * -EINVAL for impossible things.
234 enum nl80211_band band = NL80211_BAND_2GHZ;
238 band = NL80211_BAND_5GHZ;
239 return ieee80211_channel_to_frequency(freq->m, band);
241 int i, div = 1000000;
242 for (i = 0; i < freq->e; i++)
246 return freq->m / div;
250 int cfg80211_wext_siwrts(struct net_device *dev,
251 struct iw_request_info *info,
252 struct iw_param *rts, char *extra)
254 struct wireless_dev *wdev = dev->ieee80211_ptr;
255 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
256 u32 orts = wdev->wiphy->rts_threshold;
259 if (rts->disabled || !rts->fixed)
260 wdev->wiphy->rts_threshold = (u32) -1;
261 else if (rts->value < 0)
264 wdev->wiphy->rts_threshold = rts->value;
266 err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_RTS_THRESHOLD);
268 wdev->wiphy->rts_threshold = orts;
272 EXPORT_WEXT_HANDLER(cfg80211_wext_siwrts);
274 int cfg80211_wext_giwrts(struct net_device *dev,
275 struct iw_request_info *info,
276 struct iw_param *rts, char *extra)
278 struct wireless_dev *wdev = dev->ieee80211_ptr;
280 rts->value = wdev->wiphy->rts_threshold;
281 rts->disabled = rts->value == (u32) -1;
286 EXPORT_WEXT_HANDLER(cfg80211_wext_giwrts);
288 int cfg80211_wext_siwfrag(struct net_device *dev,
289 struct iw_request_info *info,
290 struct iw_param *frag, char *extra)
292 struct wireless_dev *wdev = dev->ieee80211_ptr;
293 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
294 u32 ofrag = wdev->wiphy->frag_threshold;
297 if (frag->disabled || !frag->fixed)
298 wdev->wiphy->frag_threshold = (u32) -1;
299 else if (frag->value < 256)
302 /* Fragment length must be even, so strip LSB. */
303 wdev->wiphy->frag_threshold = frag->value & ~0x1;
306 err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_FRAG_THRESHOLD);
308 wdev->wiphy->frag_threshold = ofrag;
312 EXPORT_WEXT_HANDLER(cfg80211_wext_siwfrag);
314 int cfg80211_wext_giwfrag(struct net_device *dev,
315 struct iw_request_info *info,
316 struct iw_param *frag, char *extra)
318 struct wireless_dev *wdev = dev->ieee80211_ptr;
320 frag->value = wdev->wiphy->frag_threshold;
321 frag->disabled = frag->value == (u32) -1;
326 EXPORT_WEXT_HANDLER(cfg80211_wext_giwfrag);
328 static int cfg80211_wext_siwretry(struct net_device *dev,
329 struct iw_request_info *info,
330 struct iw_param *retry, char *extra)
332 struct wireless_dev *wdev = dev->ieee80211_ptr;
333 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
335 u8 olong = wdev->wiphy->retry_long;
336 u8 oshort = wdev->wiphy->retry_short;
339 if (retry->disabled || retry->value < 1 || retry->value > 255 ||
340 (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
343 if (retry->flags & IW_RETRY_LONG) {
344 wdev->wiphy->retry_long = retry->value;
345 changed |= WIPHY_PARAM_RETRY_LONG;
346 } else if (retry->flags & IW_RETRY_SHORT) {
347 wdev->wiphy->retry_short = retry->value;
348 changed |= WIPHY_PARAM_RETRY_SHORT;
350 wdev->wiphy->retry_short = retry->value;
351 wdev->wiphy->retry_long = retry->value;
352 changed |= WIPHY_PARAM_RETRY_LONG;
353 changed |= WIPHY_PARAM_RETRY_SHORT;
356 err = rdev_set_wiphy_params(rdev, changed);
358 wdev->wiphy->retry_short = oshort;
359 wdev->wiphy->retry_long = olong;
365 int cfg80211_wext_giwretry(struct net_device *dev,
366 struct iw_request_info *info,
367 struct iw_param *retry, char *extra)
369 struct wireless_dev *wdev = dev->ieee80211_ptr;
373 if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
375 * First return short value, iwconfig will ask long value
378 retry->flags |= IW_RETRY_LIMIT | IW_RETRY_SHORT;
379 retry->value = wdev->wiphy->retry_short;
380 if (wdev->wiphy->retry_long == wdev->wiphy->retry_short)
381 retry->flags |= IW_RETRY_LONG;
386 if (retry->flags & IW_RETRY_LONG) {
387 retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
388 retry->value = wdev->wiphy->retry_long;
393 EXPORT_WEXT_HANDLER(cfg80211_wext_giwretry);
395 static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
396 struct net_device *dev, bool pairwise,
397 const u8 *addr, bool remove, bool tx_key,
398 int idx, struct key_params *params)
400 struct wireless_dev *wdev = dev->ieee80211_ptr;
404 if (pairwise && !addr)
408 * In many cases we won't actually need this, but it's better
409 * to do it first in case the allocation fails. Don't use wext.
411 if (!wdev->wext.keys) {
412 wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
414 if (!wdev->wext.keys)
416 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
417 wdev->wext.keys->params[i].key =
418 wdev->wext.keys->data[i];
421 if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
422 wdev->iftype != NL80211_IFTYPE_STATION)
425 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
426 if (!wdev->current_bss)
429 if (!rdev->ops->set_default_mgmt_key)
432 if (idx < 4 || idx > 5)
434 } else if (idx < 0 || idx > 3)
439 if (wdev->current_bss) {
441 * If removing the current TX key, we will need to
442 * join a new IBSS without the privacy bit clear.
444 if (idx == wdev->wext.default_key &&
445 wdev->iftype == NL80211_IFTYPE_ADHOC) {
446 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
450 if (!pairwise && addr &&
451 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
454 err = rdev_del_key(rdev, dev, idx, pairwise,
457 wdev->wext.connect.privacy = false;
459 * Applications using wireless extensions expect to be
460 * able to delete keys that don't exist, so allow that.
465 if (!addr && idx < 4) {
466 memset(wdev->wext.keys->data[idx], 0,
467 sizeof(wdev->wext.keys->data[idx]));
468 wdev->wext.keys->params[idx].key_len = 0;
469 wdev->wext.keys->params[idx].cipher = 0;
471 if (idx == wdev->wext.default_key)
472 wdev->wext.default_key = -1;
473 else if (idx == wdev->wext.default_mgmt_key)
474 wdev->wext.default_mgmt_key = -1;
478 err = cfg80211_ibss_wext_join(rdev, wdev);
486 if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
490 if (wdev->current_bss)
491 err = rdev_add_key(rdev, dev, idx, pairwise, addr, params);
492 else if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
493 params->cipher != WLAN_CIPHER_SUITE_WEP104)
499 * We only need to store WEP keys, since they're the only keys that
500 * can be be set before a connection is established and persist after
503 if (!addr && (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
504 params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
505 wdev->wext.keys->params[idx] = *params;
506 memcpy(wdev->wext.keys->data[idx],
507 params->key, params->key_len);
508 wdev->wext.keys->params[idx].key =
509 wdev->wext.keys->data[idx];
512 if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
513 params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
514 (tx_key || (!addr && wdev->wext.default_key == -1))) {
515 if (wdev->current_bss) {
517 * If we are getting a new TX key from not having
518 * had one before we need to join a new IBSS with
519 * the privacy bit set.
521 if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
522 wdev->wext.default_key == -1) {
523 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
526 err = rdev_set_default_key(rdev, dev, idx, true, true);
529 wdev->wext.default_key = idx;
531 err = cfg80211_ibss_wext_join(rdev, wdev);
536 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
537 (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
538 if (wdev->current_bss)
539 err = rdev_set_default_mgmt_key(rdev, dev, idx);
541 wdev->wext.default_mgmt_key = idx;
548 static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
549 struct net_device *dev, bool pairwise,
550 const u8 *addr, bool remove, bool tx_key,
551 int idx, struct key_params *params)
555 wdev_lock(dev->ieee80211_ptr);
556 err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
557 remove, tx_key, idx, params);
558 wdev_unlock(dev->ieee80211_ptr);
563 static int cfg80211_wext_siwencode(struct net_device *dev,
564 struct iw_request_info *info,
565 struct iw_point *erq, char *keybuf)
567 struct wireless_dev *wdev = dev->ieee80211_ptr;
568 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
571 struct key_params params;
573 if (wdev->iftype != NL80211_IFTYPE_STATION &&
574 wdev->iftype != NL80211_IFTYPE_ADHOC)
577 /* no use -- only MFP (set_default_mgmt_key) is optional */
578 if (!rdev->ops->del_key ||
579 !rdev->ops->add_key ||
580 !rdev->ops->set_default_key)
583 idx = erq->flags & IW_ENCODE_INDEX;
585 idx = wdev->wext.default_key;
588 } else if (idx < 1 || idx > 4)
593 if (erq->flags & IW_ENCODE_DISABLED)
595 else if (erq->length == 0) {
596 /* No key data - just set the default TX key index */
599 if (wdev->current_bss)
600 err = rdev_set_default_key(rdev, dev, idx, true,
603 wdev->wext.default_key = idx;
608 memset(¶ms, 0, sizeof(params));
610 params.key_len = erq->length;
611 if (erq->length == 5)
612 params.cipher = WLAN_CIPHER_SUITE_WEP40;
613 else if (erq->length == 13)
614 params.cipher = WLAN_CIPHER_SUITE_WEP104;
618 return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
619 wdev->wext.default_key == -1,
623 static int cfg80211_wext_siwencodeext(struct net_device *dev,
624 struct iw_request_info *info,
625 struct iw_point *erq, char *extra)
627 struct wireless_dev *wdev = dev->ieee80211_ptr;
628 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
629 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
633 struct key_params params;
636 if (wdev->iftype != NL80211_IFTYPE_STATION &&
637 wdev->iftype != NL80211_IFTYPE_ADHOC)
640 /* no use -- only MFP (set_default_mgmt_key) is optional */
641 if (!rdev->ops->del_key ||
642 !rdev->ops->add_key ||
643 !rdev->ops->set_default_key)
647 case IW_ENCODE_ALG_NONE:
651 case IW_ENCODE_ALG_WEP:
652 if (ext->key_len == 5)
653 cipher = WLAN_CIPHER_SUITE_WEP40;
654 else if (ext->key_len == 13)
655 cipher = WLAN_CIPHER_SUITE_WEP104;
659 case IW_ENCODE_ALG_TKIP:
660 cipher = WLAN_CIPHER_SUITE_TKIP;
662 case IW_ENCODE_ALG_CCMP:
663 cipher = WLAN_CIPHER_SUITE_CCMP;
665 case IW_ENCODE_ALG_AES_CMAC:
666 cipher = WLAN_CIPHER_SUITE_AES_CMAC;
672 if (erq->flags & IW_ENCODE_DISABLED)
675 idx = erq->flags & IW_ENCODE_INDEX;
676 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
677 if (idx < 4 || idx > 5) {
678 idx = wdev->wext.default_mgmt_key;
684 if (idx < 1 || idx > 4) {
685 idx = wdev->wext.default_key;
692 addr = ext->addr.sa_data;
693 if (is_broadcast_ether_addr(addr))
696 memset(¶ms, 0, sizeof(params));
697 params.key = ext->key;
698 params.key_len = ext->key_len;
699 params.cipher = cipher;
701 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
702 params.seq = ext->rx_seq;
706 return cfg80211_set_encryption(
708 !(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
710 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
714 static int cfg80211_wext_giwencode(struct net_device *dev,
715 struct iw_request_info *info,
716 struct iw_point *erq, char *keybuf)
718 struct wireless_dev *wdev = dev->ieee80211_ptr;
721 if (wdev->iftype != NL80211_IFTYPE_STATION &&
722 wdev->iftype != NL80211_IFTYPE_ADHOC)
725 idx = erq->flags & IW_ENCODE_INDEX;
727 idx = wdev->wext.default_key;
730 } else if (idx < 1 || idx > 4)
735 erq->flags = idx + 1;
737 if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
738 erq->flags |= IW_ENCODE_DISABLED;
743 erq->length = min_t(size_t, erq->length,
744 wdev->wext.keys->params[idx].key_len);
745 memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
746 erq->flags |= IW_ENCODE_ENABLED;
751 static int cfg80211_wext_siwfreq(struct net_device *dev,
752 struct iw_request_info *info,
753 struct iw_freq *wextfreq, char *extra)
755 struct wireless_dev *wdev = dev->ieee80211_ptr;
756 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
757 struct cfg80211_chan_def chandef = {
758 .width = NL80211_CHAN_WIDTH_20_NOHT,
762 switch (wdev->iftype) {
763 case NL80211_IFTYPE_STATION:
764 return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
765 case NL80211_IFTYPE_ADHOC:
766 return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
767 case NL80211_IFTYPE_MONITOR:
768 freq = cfg80211_wext_freq(wextfreq);
773 chandef.center_freq1 = freq;
774 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
777 return cfg80211_set_monitor_channel(rdev, &chandef);
778 case NL80211_IFTYPE_MESH_POINT:
779 freq = cfg80211_wext_freq(wextfreq);
784 chandef.center_freq1 = freq;
785 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
788 return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
794 static int cfg80211_wext_giwfreq(struct net_device *dev,
795 struct iw_request_info *info,
796 struct iw_freq *freq, char *extra)
798 struct wireless_dev *wdev = dev->ieee80211_ptr;
799 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
800 struct cfg80211_chan_def chandef;
803 switch (wdev->iftype) {
804 case NL80211_IFTYPE_STATION:
805 return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
806 case NL80211_IFTYPE_ADHOC:
807 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
808 case NL80211_IFTYPE_MONITOR:
809 if (!rdev->ops->get_channel)
812 ret = rdev_get_channel(rdev, wdev, &chandef);
815 freq->m = chandef.chan->center_freq;
823 static int cfg80211_wext_siwtxpower(struct net_device *dev,
824 struct iw_request_info *info,
825 union iwreq_data *data, char *extra)
827 struct wireless_dev *wdev = dev->ieee80211_ptr;
828 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
829 enum nl80211_tx_power_setting type;
832 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
834 if (data->txpower.flags & IW_TXPOW_RANGE)
837 if (!rdev->ops->set_tx_power)
840 /* only change when not disabling */
841 if (!data->txpower.disabled) {
842 rfkill_set_sw_state(rdev->rfkill, false);
844 if (data->txpower.fixed) {
846 * wext doesn't support negative values, see
847 * below where it's for automatic
849 if (data->txpower.value < 0)
851 dbm = data->txpower.value;
852 type = NL80211_TX_POWER_FIXED;
853 /* TODO: do regulatory check! */
856 * Automatic power level setting, max being the value
857 * passed in from userland.
859 if (data->txpower.value < 0) {
860 type = NL80211_TX_POWER_AUTOMATIC;
862 dbm = data->txpower.value;
863 type = NL80211_TX_POWER_LIMITED;
867 rfkill_set_sw_state(rdev->rfkill, true);
868 schedule_work(&rdev->rfkill_sync);
872 return rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
875 static int cfg80211_wext_giwtxpower(struct net_device *dev,
876 struct iw_request_info *info,
877 union iwreq_data *data, char *extra)
879 struct wireless_dev *wdev = dev->ieee80211_ptr;
880 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
883 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
885 if (data->txpower.flags & IW_TXPOW_RANGE)
888 if (!rdev->ops->get_tx_power)
891 err = rdev_get_tx_power(rdev, wdev, &val);
895 /* well... oh well */
896 data->txpower.fixed = 1;
897 data->txpower.disabled = rfkill_blocked(rdev->rfkill);
898 data->txpower.value = val;
899 data->txpower.flags = IW_TXPOW_DBM;
904 static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
912 if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
913 IW_AUTH_ALG_SHARED_KEY |
917 if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
919 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
922 if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
924 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
927 if (auth_alg & IW_AUTH_ALG_LEAP) {
929 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
933 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
938 static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
940 if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
941 IW_AUTH_WPA_VERSION_WPA2|
942 IW_AUTH_WPA_VERSION_DISABLED))
945 if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
946 (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
947 IW_AUTH_WPA_VERSION_WPA2)))
950 if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
951 wdev->wext.connect.crypto.wpa_versions &=
952 ~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
954 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
955 wdev->wext.connect.crypto.wpa_versions |=
956 NL80211_WPA_VERSION_1;
958 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
959 wdev->wext.connect.crypto.wpa_versions |=
960 NL80211_WPA_VERSION_2;
965 static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
967 if (cipher & IW_AUTH_CIPHER_WEP40)
968 wdev->wext.connect.crypto.cipher_group =
969 WLAN_CIPHER_SUITE_WEP40;
970 else if (cipher & IW_AUTH_CIPHER_WEP104)
971 wdev->wext.connect.crypto.cipher_group =
972 WLAN_CIPHER_SUITE_WEP104;
973 else if (cipher & IW_AUTH_CIPHER_TKIP)
974 wdev->wext.connect.crypto.cipher_group =
975 WLAN_CIPHER_SUITE_TKIP;
976 else if (cipher & IW_AUTH_CIPHER_CCMP)
977 wdev->wext.connect.crypto.cipher_group =
978 WLAN_CIPHER_SUITE_CCMP;
979 else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
980 wdev->wext.connect.crypto.cipher_group =
981 WLAN_CIPHER_SUITE_AES_CMAC;
982 else if (cipher & IW_AUTH_CIPHER_NONE)
983 wdev->wext.connect.crypto.cipher_group = 0;
990 static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
993 u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
995 if (cipher & IW_AUTH_CIPHER_WEP40) {
996 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
1000 if (cipher & IW_AUTH_CIPHER_WEP104) {
1001 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1005 if (cipher & IW_AUTH_CIPHER_TKIP) {
1006 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1010 if (cipher & IW_AUTH_CIPHER_CCMP) {
1011 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1015 if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1016 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1020 BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1022 wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1028 static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
1030 int nr_akm_suites = 0;
1032 if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1033 IW_AUTH_KEY_MGMT_PSK))
1036 if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1037 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1038 WLAN_AKM_SUITE_8021X;
1042 if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1043 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1048 wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1053 static int cfg80211_wext_siwauth(struct net_device *dev,
1054 struct iw_request_info *info,
1055 struct iw_param *data, char *extra)
1057 struct wireless_dev *wdev = dev->ieee80211_ptr;
1059 if (wdev->iftype != NL80211_IFTYPE_STATION)
1062 switch (data->flags & IW_AUTH_INDEX) {
1063 case IW_AUTH_PRIVACY_INVOKED:
1064 wdev->wext.connect.privacy = data->value;
1066 case IW_AUTH_WPA_VERSION:
1067 return cfg80211_set_wpa_version(wdev, data->value);
1068 case IW_AUTH_CIPHER_GROUP:
1069 return cfg80211_set_cipher_group(wdev, data->value);
1070 case IW_AUTH_KEY_MGMT:
1071 return cfg80211_set_key_mgt(wdev, data->value);
1072 case IW_AUTH_CIPHER_PAIRWISE:
1073 return cfg80211_set_cipher_pairwise(wdev, data->value);
1074 case IW_AUTH_80211_AUTH_ALG:
1075 return cfg80211_set_auth_alg(wdev, data->value);
1076 case IW_AUTH_WPA_ENABLED:
1077 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1078 case IW_AUTH_DROP_UNENCRYPTED:
1086 static int cfg80211_wext_giwauth(struct net_device *dev,
1087 struct iw_request_info *info,
1088 struct iw_param *data, char *extra)
1090 /* XXX: what do we need? */
1095 static int cfg80211_wext_siwpower(struct net_device *dev,
1096 struct iw_request_info *info,
1097 struct iw_param *wrq, char *extra)
1099 struct wireless_dev *wdev = dev->ieee80211_ptr;
1100 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1102 int timeout = wdev->ps_timeout;
1105 if (wdev->iftype != NL80211_IFTYPE_STATION)
1108 if (!rdev->ops->set_power_mgmt)
1111 if (wrq->disabled) {
1114 switch (wrq->flags & IW_POWER_MODE) {
1115 case IW_POWER_ON: /* If not specified */
1116 case IW_POWER_MODE: /* If set all mask */
1117 case IW_POWER_ALL_R: /* If explicitely state all */
1120 default: /* Otherwise we ignore */
1124 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1127 if (wrq->flags & IW_POWER_TIMEOUT)
1128 timeout = wrq->value / 1000;
1131 err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
1136 wdev->ps_timeout = timeout;
1142 static int cfg80211_wext_giwpower(struct net_device *dev,
1143 struct iw_request_info *info,
1144 struct iw_param *wrq, char *extra)
1146 struct wireless_dev *wdev = dev->ieee80211_ptr;
1148 wrq->disabled = !wdev->ps;
1153 static int cfg80211_wds_wext_siwap(struct net_device *dev,
1154 struct iw_request_info *info,
1155 struct sockaddr *addr, char *extra)
1157 struct wireless_dev *wdev = dev->ieee80211_ptr;
1158 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1161 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1164 if (addr->sa_family != ARPHRD_ETHER)
1167 if (netif_running(dev))
1170 if (!rdev->ops->set_wds_peer)
1173 err = rdev_set_wds_peer(rdev, dev, (u8 *)&addr->sa_data);
1177 memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
1182 static int cfg80211_wds_wext_giwap(struct net_device *dev,
1183 struct iw_request_info *info,
1184 struct sockaddr *addr, char *extra)
1186 struct wireless_dev *wdev = dev->ieee80211_ptr;
1188 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1191 addr->sa_family = ARPHRD_ETHER;
1192 memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
1197 static int cfg80211_wext_siwrate(struct net_device *dev,
1198 struct iw_request_info *info,
1199 struct iw_param *rate, char *extra)
1201 struct wireless_dev *wdev = dev->ieee80211_ptr;
1202 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1203 struct cfg80211_bitrate_mask mask;
1205 struct ieee80211_supported_band *sband;
1209 if (!rdev->ops->set_bitrate_mask)
1212 memset(&mask, 0, sizeof(mask));
1216 if (rate->value < 0) {
1218 } else if (rate->fixed) {
1219 fixed = rate->value / 100000;
1221 maxrate = rate->value / 100000;
1224 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1225 sband = wdev->wiphy->bands[band];
1228 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1229 struct ieee80211_rate *srate = &sband->bitrates[ridx];
1230 if (fixed == srate->bitrate) {
1231 mask.control[band].legacy = 1 << ridx;
1235 if (srate->bitrate <= maxrate) {
1236 mask.control[band].legacy |= 1 << ridx;
1245 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
1248 static int cfg80211_wext_giwrate(struct net_device *dev,
1249 struct iw_request_info *info,
1250 struct iw_param *rate, char *extra)
1252 struct wireless_dev *wdev = dev->ieee80211_ptr;
1253 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1254 struct station_info sinfo = {};
1258 if (wdev->iftype != NL80211_IFTYPE_STATION)
1261 if (!rdev->ops->get_station)
1266 if (wdev->current_bss)
1267 memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
1274 err = rdev_get_station(rdev, dev, addr, &sinfo);
1278 if (!(sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_BITRATE))) {
1283 rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
1286 cfg80211_sinfo_release_content(&sinfo);
1290 /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
1291 static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
1293 struct wireless_dev *wdev = dev->ieee80211_ptr;
1294 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1295 /* we are under RTNL - globally locked - so can use static structs */
1296 static struct iw_statistics wstats;
1297 static struct station_info sinfo = {};
1300 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
1303 if (!rdev->ops->get_station)
1306 /* Grab BSSID of current BSS, if any */
1308 if (!wdev->current_bss) {
1312 memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
1315 memset(&sinfo, 0, sizeof(sinfo));
1317 if (rdev_get_station(rdev, dev, bssid, &sinfo))
1320 memset(&wstats, 0, sizeof(wstats));
1322 switch (rdev->wiphy.signal_type) {
1323 case CFG80211_SIGNAL_TYPE_MBM:
1324 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1325 int sig = sinfo.signal;
1326 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1327 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1328 wstats.qual.updated |= IW_QUAL_DBM;
1329 wstats.qual.level = sig;
1334 wstats.qual.qual = sig + 110;
1338 case CFG80211_SIGNAL_TYPE_UNSPEC:
1339 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_SIGNAL)) {
1340 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1341 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1342 wstats.qual.level = sinfo.signal;
1343 wstats.qual.qual = sinfo.signal;
1348 wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1349 wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1352 wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1353 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC))
1354 wstats.discard.misc = sinfo.rx_dropped_misc;
1355 if (sinfo.filled & BIT_ULL(NL80211_STA_INFO_TX_FAILED))
1356 wstats.discard.retries = sinfo.tx_failed;
1358 cfg80211_sinfo_release_content(&sinfo);
1363 static int cfg80211_wext_siwap(struct net_device *dev,
1364 struct iw_request_info *info,
1365 struct sockaddr *ap_addr, char *extra)
1367 struct wireless_dev *wdev = dev->ieee80211_ptr;
1369 switch (wdev->iftype) {
1370 case NL80211_IFTYPE_ADHOC:
1371 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1372 case NL80211_IFTYPE_STATION:
1373 return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1374 case NL80211_IFTYPE_WDS:
1375 return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
1381 static int cfg80211_wext_giwap(struct net_device *dev,
1382 struct iw_request_info *info,
1383 struct sockaddr *ap_addr, char *extra)
1385 struct wireless_dev *wdev = dev->ieee80211_ptr;
1387 switch (wdev->iftype) {
1388 case NL80211_IFTYPE_ADHOC:
1389 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1390 case NL80211_IFTYPE_STATION:
1391 return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1392 case NL80211_IFTYPE_WDS:
1393 return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
1399 static int cfg80211_wext_siwessid(struct net_device *dev,
1400 struct iw_request_info *info,
1401 struct iw_point *data, char *ssid)
1403 struct wireless_dev *wdev = dev->ieee80211_ptr;
1405 switch (wdev->iftype) {
1406 case NL80211_IFTYPE_ADHOC:
1407 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1408 case NL80211_IFTYPE_STATION:
1409 return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1415 static int cfg80211_wext_giwessid(struct net_device *dev,
1416 struct iw_request_info *info,
1417 struct iw_point *data, char *ssid)
1419 struct wireless_dev *wdev = dev->ieee80211_ptr;
1424 switch (wdev->iftype) {
1425 case NL80211_IFTYPE_ADHOC:
1426 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1427 case NL80211_IFTYPE_STATION:
1428 return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1434 static int cfg80211_wext_siwpmksa(struct net_device *dev,
1435 struct iw_request_info *info,
1436 struct iw_point *data, char *extra)
1438 struct wireless_dev *wdev = dev->ieee80211_ptr;
1439 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1440 struct cfg80211_pmksa cfg_pmksa;
1441 struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1443 memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1445 if (wdev->iftype != NL80211_IFTYPE_STATION)
1448 cfg_pmksa.bssid = pmksa->bssid.sa_data;
1449 cfg_pmksa.pmkid = pmksa->pmkid;
1451 switch (pmksa->cmd) {
1453 if (!rdev->ops->set_pmksa)
1456 return rdev_set_pmksa(rdev, dev, &cfg_pmksa);
1458 case IW_PMKSA_REMOVE:
1459 if (!rdev->ops->del_pmksa)
1462 return rdev_del_pmksa(rdev, dev, &cfg_pmksa);
1464 case IW_PMKSA_FLUSH:
1465 if (!rdev->ops->flush_pmksa)
1468 return rdev_flush_pmksa(rdev, dev);
1475 static const iw_handler cfg80211_handlers[] = {
1476 [IW_IOCTL_IDX(SIOCGIWNAME)] = (iw_handler) cfg80211_wext_giwname,
1477 [IW_IOCTL_IDX(SIOCSIWFREQ)] = (iw_handler) cfg80211_wext_siwfreq,
1478 [IW_IOCTL_IDX(SIOCGIWFREQ)] = (iw_handler) cfg80211_wext_giwfreq,
1479 [IW_IOCTL_IDX(SIOCSIWMODE)] = (iw_handler) cfg80211_wext_siwmode,
1480 [IW_IOCTL_IDX(SIOCGIWMODE)] = (iw_handler) cfg80211_wext_giwmode,
1481 [IW_IOCTL_IDX(SIOCGIWRANGE)] = (iw_handler) cfg80211_wext_giwrange,
1482 [IW_IOCTL_IDX(SIOCSIWAP)] = (iw_handler) cfg80211_wext_siwap,
1483 [IW_IOCTL_IDX(SIOCGIWAP)] = (iw_handler) cfg80211_wext_giwap,
1484 [IW_IOCTL_IDX(SIOCSIWMLME)] = (iw_handler) cfg80211_wext_siwmlme,
1485 [IW_IOCTL_IDX(SIOCSIWSCAN)] = (iw_handler) cfg80211_wext_siwscan,
1486 [IW_IOCTL_IDX(SIOCGIWSCAN)] = (iw_handler) cfg80211_wext_giwscan,
1487 [IW_IOCTL_IDX(SIOCSIWESSID)] = (iw_handler) cfg80211_wext_siwessid,
1488 [IW_IOCTL_IDX(SIOCGIWESSID)] = (iw_handler) cfg80211_wext_giwessid,
1489 [IW_IOCTL_IDX(SIOCSIWRATE)] = (iw_handler) cfg80211_wext_siwrate,
1490 [IW_IOCTL_IDX(SIOCGIWRATE)] = (iw_handler) cfg80211_wext_giwrate,
1491 [IW_IOCTL_IDX(SIOCSIWRTS)] = (iw_handler) cfg80211_wext_siwrts,
1492 [IW_IOCTL_IDX(SIOCGIWRTS)] = (iw_handler) cfg80211_wext_giwrts,
1493 [IW_IOCTL_IDX(SIOCSIWFRAG)] = (iw_handler) cfg80211_wext_siwfrag,
1494 [IW_IOCTL_IDX(SIOCGIWFRAG)] = (iw_handler) cfg80211_wext_giwfrag,
1495 [IW_IOCTL_IDX(SIOCSIWTXPOW)] = (iw_handler) cfg80211_wext_siwtxpower,
1496 [IW_IOCTL_IDX(SIOCGIWTXPOW)] = (iw_handler) cfg80211_wext_giwtxpower,
1497 [IW_IOCTL_IDX(SIOCSIWRETRY)] = (iw_handler) cfg80211_wext_siwretry,
1498 [IW_IOCTL_IDX(SIOCGIWRETRY)] = (iw_handler) cfg80211_wext_giwretry,
1499 [IW_IOCTL_IDX(SIOCSIWENCODE)] = (iw_handler) cfg80211_wext_siwencode,
1500 [IW_IOCTL_IDX(SIOCGIWENCODE)] = (iw_handler) cfg80211_wext_giwencode,
1501 [IW_IOCTL_IDX(SIOCSIWPOWER)] = (iw_handler) cfg80211_wext_siwpower,
1502 [IW_IOCTL_IDX(SIOCGIWPOWER)] = (iw_handler) cfg80211_wext_giwpower,
1503 [IW_IOCTL_IDX(SIOCSIWGENIE)] = (iw_handler) cfg80211_wext_siwgenie,
1504 [IW_IOCTL_IDX(SIOCSIWAUTH)] = (iw_handler) cfg80211_wext_siwauth,
1505 [IW_IOCTL_IDX(SIOCGIWAUTH)] = (iw_handler) cfg80211_wext_giwauth,
1506 [IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
1507 [IW_IOCTL_IDX(SIOCSIWPMKSA)] = (iw_handler) cfg80211_wext_siwpmksa,
1510 const struct iw_handler_def cfg80211_wext_handler = {
1511 .num_standard = ARRAY_SIZE(cfg80211_handlers),
1512 .standard = cfg80211_handlers,
1513 .get_wireless_stats = cfg80211_wireless_stats,