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;
359 err = rdev_set_wiphy_params(rdev, changed);
361 wdev->wiphy->retry_short = oshort;
362 wdev->wiphy->retry_long = olong;
368 int cfg80211_wext_giwretry(struct net_device *dev,
369 struct iw_request_info *info,
370 struct iw_param *retry, char *extra)
372 struct wireless_dev *wdev = dev->ieee80211_ptr;
376 if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
378 * First return short value, iwconfig will ask long value
381 retry->flags |= IW_RETRY_LIMIT | IW_RETRY_SHORT;
382 retry->value = wdev->wiphy->retry_short;
383 if (wdev->wiphy->retry_long == wdev->wiphy->retry_short)
384 retry->flags |= IW_RETRY_LONG;
389 if (retry->flags & IW_RETRY_LONG) {
390 retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
391 retry->value = wdev->wiphy->retry_long;
396 EXPORT_WEXT_HANDLER(cfg80211_wext_giwretry);
398 static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
399 struct net_device *dev, bool pairwise,
400 const u8 *addr, bool remove, bool tx_key,
401 int idx, struct key_params *params)
403 struct wireless_dev *wdev = dev->ieee80211_ptr;
407 if (pairwise && !addr)
411 * In many cases we won't actually need this, but it's better
412 * to do it first in case the allocation fails. Don't use wext.
414 if (!wdev->wext.keys) {
415 wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
417 if (!wdev->wext.keys)
419 for (i = 0; i < CFG80211_MAX_WEP_KEYS; i++)
420 wdev->wext.keys->params[i].key =
421 wdev->wext.keys->data[i];
424 if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
425 wdev->iftype != NL80211_IFTYPE_STATION)
428 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
429 if (!wdev->current_bss)
432 if (!rdev->ops->set_default_mgmt_key)
435 if (idx < 4 || idx > 5)
437 } else if (idx < 0 || idx > 3)
442 if (wdev->current_bss) {
444 * If removing the current TX key, we will need to
445 * join a new IBSS without the privacy bit clear.
447 if (idx == wdev->wext.default_key &&
448 wdev->iftype == NL80211_IFTYPE_ADHOC) {
449 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
453 if (!pairwise && addr &&
454 !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
457 err = rdev_del_key(rdev, dev, idx, pairwise,
460 wdev->wext.connect.privacy = false;
462 * Applications using wireless extensions expect to be
463 * able to delete keys that don't exist, so allow that.
468 if (!addr && idx < 4) {
469 memset(wdev->wext.keys->data[idx], 0,
470 sizeof(wdev->wext.keys->data[idx]));
471 wdev->wext.keys->params[idx].key_len = 0;
472 wdev->wext.keys->params[idx].cipher = 0;
474 if (idx == wdev->wext.default_key)
475 wdev->wext.default_key = -1;
476 else if (idx == wdev->wext.default_mgmt_key)
477 wdev->wext.default_mgmt_key = -1;
481 err = cfg80211_ibss_wext_join(rdev, wdev);
489 if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
493 if (wdev->current_bss)
494 err = rdev_add_key(rdev, dev, idx, pairwise, addr, params);
495 else if (params->cipher != WLAN_CIPHER_SUITE_WEP40 &&
496 params->cipher != WLAN_CIPHER_SUITE_WEP104)
502 * We only need to store WEP keys, since they're the only keys that
503 * can be be set before a connection is established and persist after
506 if (!addr && (params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
507 params->cipher == WLAN_CIPHER_SUITE_WEP104)) {
508 wdev->wext.keys->params[idx] = *params;
509 memcpy(wdev->wext.keys->data[idx],
510 params->key, params->key_len);
511 wdev->wext.keys->params[idx].key =
512 wdev->wext.keys->data[idx];
515 if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
516 params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
517 (tx_key || (!addr && wdev->wext.default_key == -1))) {
518 if (wdev->current_bss) {
520 * If we are getting a new TX key from not having
521 * had one before we need to join a new IBSS with
522 * the privacy bit set.
524 if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
525 wdev->wext.default_key == -1) {
526 __cfg80211_leave_ibss(rdev, wdev->netdev, true);
529 err = rdev_set_default_key(rdev, dev, idx, true, true);
532 wdev->wext.default_key = idx;
534 err = cfg80211_ibss_wext_join(rdev, wdev);
539 if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
540 (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
541 if (wdev->current_bss)
542 err = rdev_set_default_mgmt_key(rdev, dev, idx);
544 wdev->wext.default_mgmt_key = idx;
551 static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
552 struct net_device *dev, bool pairwise,
553 const u8 *addr, bool remove, bool tx_key,
554 int idx, struct key_params *params)
558 wdev_lock(dev->ieee80211_ptr);
559 err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
560 remove, tx_key, idx, params);
561 wdev_unlock(dev->ieee80211_ptr);
566 static int cfg80211_wext_siwencode(struct net_device *dev,
567 struct iw_request_info *info,
568 struct iw_point *erq, char *keybuf)
570 struct wireless_dev *wdev = dev->ieee80211_ptr;
571 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
574 struct key_params params;
576 if (wdev->iftype != NL80211_IFTYPE_STATION &&
577 wdev->iftype != NL80211_IFTYPE_ADHOC)
580 /* no use -- only MFP (set_default_mgmt_key) is optional */
581 if (!rdev->ops->del_key ||
582 !rdev->ops->add_key ||
583 !rdev->ops->set_default_key)
586 idx = erq->flags & IW_ENCODE_INDEX;
588 idx = wdev->wext.default_key;
591 } else if (idx < 1 || idx > 4)
596 if (erq->flags & IW_ENCODE_DISABLED)
598 else if (erq->length == 0) {
599 /* No key data - just set the default TX key index */
602 if (wdev->current_bss)
603 err = rdev_set_default_key(rdev, dev, idx, true,
606 wdev->wext.default_key = idx;
611 memset(¶ms, 0, sizeof(params));
613 params.key_len = erq->length;
614 if (erq->length == 5)
615 params.cipher = WLAN_CIPHER_SUITE_WEP40;
616 else if (erq->length == 13)
617 params.cipher = WLAN_CIPHER_SUITE_WEP104;
621 return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
622 wdev->wext.default_key == -1,
626 static int cfg80211_wext_siwencodeext(struct net_device *dev,
627 struct iw_request_info *info,
628 struct iw_point *erq, char *extra)
630 struct wireless_dev *wdev = dev->ieee80211_ptr;
631 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
632 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
636 struct key_params params;
639 if (wdev->iftype != NL80211_IFTYPE_STATION &&
640 wdev->iftype != NL80211_IFTYPE_ADHOC)
643 /* no use -- only MFP (set_default_mgmt_key) is optional */
644 if (!rdev->ops->del_key ||
645 !rdev->ops->add_key ||
646 !rdev->ops->set_default_key)
650 case IW_ENCODE_ALG_NONE:
654 case IW_ENCODE_ALG_WEP:
655 if (ext->key_len == 5)
656 cipher = WLAN_CIPHER_SUITE_WEP40;
657 else if (ext->key_len == 13)
658 cipher = WLAN_CIPHER_SUITE_WEP104;
662 case IW_ENCODE_ALG_TKIP:
663 cipher = WLAN_CIPHER_SUITE_TKIP;
665 case IW_ENCODE_ALG_CCMP:
666 cipher = WLAN_CIPHER_SUITE_CCMP;
668 case IW_ENCODE_ALG_AES_CMAC:
669 cipher = WLAN_CIPHER_SUITE_AES_CMAC;
675 if (erq->flags & IW_ENCODE_DISABLED)
678 idx = erq->flags & IW_ENCODE_INDEX;
679 if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
680 if (idx < 4 || idx > 5) {
681 idx = wdev->wext.default_mgmt_key;
687 if (idx < 1 || idx > 4) {
688 idx = wdev->wext.default_key;
695 addr = ext->addr.sa_data;
696 if (is_broadcast_ether_addr(addr))
699 memset(¶ms, 0, sizeof(params));
700 params.key = ext->key;
701 params.key_len = ext->key_len;
702 params.cipher = cipher;
704 if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
705 params.seq = ext->rx_seq;
709 return cfg80211_set_encryption(
711 !(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
713 ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
717 static int cfg80211_wext_giwencode(struct net_device *dev,
718 struct iw_request_info *info,
719 struct iw_point *erq, char *keybuf)
721 struct wireless_dev *wdev = dev->ieee80211_ptr;
724 if (wdev->iftype != NL80211_IFTYPE_STATION &&
725 wdev->iftype != NL80211_IFTYPE_ADHOC)
728 idx = erq->flags & IW_ENCODE_INDEX;
730 idx = wdev->wext.default_key;
733 } else if (idx < 1 || idx > 4)
738 erq->flags = idx + 1;
740 if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
741 erq->flags |= IW_ENCODE_DISABLED;
746 erq->length = min_t(size_t, erq->length,
747 wdev->wext.keys->params[idx].key_len);
748 memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
749 erq->flags |= IW_ENCODE_ENABLED;
754 static int cfg80211_wext_siwfreq(struct net_device *dev,
755 struct iw_request_info *info,
756 struct iw_freq *wextfreq, char *extra)
758 struct wireless_dev *wdev = dev->ieee80211_ptr;
759 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
760 struct cfg80211_chan_def chandef = {
761 .width = NL80211_CHAN_WIDTH_20_NOHT,
765 switch (wdev->iftype) {
766 case NL80211_IFTYPE_STATION:
767 return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
768 case NL80211_IFTYPE_ADHOC:
769 return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
770 case NL80211_IFTYPE_MONITOR:
771 freq = cfg80211_wext_freq(wextfreq);
776 chandef.center_freq1 = freq;
777 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
780 return cfg80211_set_monitor_channel(rdev, &chandef);
781 case NL80211_IFTYPE_MESH_POINT:
782 freq = cfg80211_wext_freq(wextfreq);
787 chandef.center_freq1 = freq;
788 chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
791 return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
797 static int cfg80211_wext_giwfreq(struct net_device *dev,
798 struct iw_request_info *info,
799 struct iw_freq *freq, char *extra)
801 struct wireless_dev *wdev = dev->ieee80211_ptr;
802 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
803 struct cfg80211_chan_def chandef = {};
806 switch (wdev->iftype) {
807 case NL80211_IFTYPE_STATION:
808 return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
809 case NL80211_IFTYPE_ADHOC:
810 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
811 case NL80211_IFTYPE_MONITOR:
812 if (!rdev->ops->get_channel)
815 ret = rdev_get_channel(rdev, wdev, &chandef);
818 freq->m = chandef.chan->center_freq;
826 static int cfg80211_wext_siwtxpower(struct net_device *dev,
827 struct iw_request_info *info,
828 union iwreq_data *data, char *extra)
830 struct wireless_dev *wdev = dev->ieee80211_ptr;
831 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
832 enum nl80211_tx_power_setting type;
835 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
837 if (data->txpower.flags & IW_TXPOW_RANGE)
840 if (!rdev->ops->set_tx_power)
843 /* only change when not disabling */
844 if (!data->txpower.disabled) {
845 rfkill_set_sw_state(rdev->rfkill, false);
847 if (data->txpower.fixed) {
849 * wext doesn't support negative values, see
850 * below where it's for automatic
852 if (data->txpower.value < 0)
854 dbm = data->txpower.value;
855 type = NL80211_TX_POWER_FIXED;
856 /* TODO: do regulatory check! */
859 * Automatic power level setting, max being the value
860 * passed in from userland.
862 if (data->txpower.value < 0) {
863 type = NL80211_TX_POWER_AUTOMATIC;
865 dbm = data->txpower.value;
866 type = NL80211_TX_POWER_LIMITED;
870 rfkill_set_sw_state(rdev->rfkill, true);
871 schedule_work(&rdev->rfkill_sync);
875 return rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
878 static int cfg80211_wext_giwtxpower(struct net_device *dev,
879 struct iw_request_info *info,
880 union iwreq_data *data, char *extra)
882 struct wireless_dev *wdev = dev->ieee80211_ptr;
883 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
886 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
888 if (data->txpower.flags & IW_TXPOW_RANGE)
891 if (!rdev->ops->get_tx_power)
894 err = rdev_get_tx_power(rdev, wdev, &val);
898 /* well... oh well */
899 data->txpower.fixed = 1;
900 data->txpower.disabled = rfkill_blocked(rdev->rfkill);
901 data->txpower.value = val;
902 data->txpower.flags = IW_TXPOW_DBM;
907 static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
915 if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
916 IW_AUTH_ALG_SHARED_KEY |
920 if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
922 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
925 if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
927 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
930 if (auth_alg & IW_AUTH_ALG_LEAP) {
932 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
936 wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
941 static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
943 if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
944 IW_AUTH_WPA_VERSION_WPA2|
945 IW_AUTH_WPA_VERSION_DISABLED))
948 if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
949 (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
950 IW_AUTH_WPA_VERSION_WPA2)))
953 if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
954 wdev->wext.connect.crypto.wpa_versions &=
955 ~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
957 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
958 wdev->wext.connect.crypto.wpa_versions |=
959 NL80211_WPA_VERSION_1;
961 if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
962 wdev->wext.connect.crypto.wpa_versions |=
963 NL80211_WPA_VERSION_2;
968 static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
970 if (cipher & IW_AUTH_CIPHER_WEP40)
971 wdev->wext.connect.crypto.cipher_group =
972 WLAN_CIPHER_SUITE_WEP40;
973 else if (cipher & IW_AUTH_CIPHER_WEP104)
974 wdev->wext.connect.crypto.cipher_group =
975 WLAN_CIPHER_SUITE_WEP104;
976 else if (cipher & IW_AUTH_CIPHER_TKIP)
977 wdev->wext.connect.crypto.cipher_group =
978 WLAN_CIPHER_SUITE_TKIP;
979 else if (cipher & IW_AUTH_CIPHER_CCMP)
980 wdev->wext.connect.crypto.cipher_group =
981 WLAN_CIPHER_SUITE_CCMP;
982 else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
983 wdev->wext.connect.crypto.cipher_group =
984 WLAN_CIPHER_SUITE_AES_CMAC;
985 else if (cipher & IW_AUTH_CIPHER_NONE)
986 wdev->wext.connect.crypto.cipher_group = 0;
993 static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
996 u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
998 if (cipher & IW_AUTH_CIPHER_WEP40) {
999 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
1003 if (cipher & IW_AUTH_CIPHER_WEP104) {
1004 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
1008 if (cipher & IW_AUTH_CIPHER_TKIP) {
1009 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
1013 if (cipher & IW_AUTH_CIPHER_CCMP) {
1014 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
1018 if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
1019 ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
1023 BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
1025 wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
1031 static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
1033 int nr_akm_suites = 0;
1035 if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
1036 IW_AUTH_KEY_MGMT_PSK))
1039 if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
1040 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1041 WLAN_AKM_SUITE_8021X;
1045 if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
1046 wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
1051 wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
1056 static int cfg80211_wext_siwauth(struct net_device *dev,
1057 struct iw_request_info *info,
1058 struct iw_param *data, char *extra)
1060 struct wireless_dev *wdev = dev->ieee80211_ptr;
1062 if (wdev->iftype != NL80211_IFTYPE_STATION)
1065 switch (data->flags & IW_AUTH_INDEX) {
1066 case IW_AUTH_PRIVACY_INVOKED:
1067 wdev->wext.connect.privacy = data->value;
1069 case IW_AUTH_WPA_VERSION:
1070 return cfg80211_set_wpa_version(wdev, data->value);
1071 case IW_AUTH_CIPHER_GROUP:
1072 return cfg80211_set_cipher_group(wdev, data->value);
1073 case IW_AUTH_KEY_MGMT:
1074 return cfg80211_set_key_mgt(wdev, data->value);
1075 case IW_AUTH_CIPHER_PAIRWISE:
1076 return cfg80211_set_cipher_pairwise(wdev, data->value);
1077 case IW_AUTH_80211_AUTH_ALG:
1078 return cfg80211_set_auth_alg(wdev, data->value);
1079 case IW_AUTH_WPA_ENABLED:
1080 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
1081 case IW_AUTH_DROP_UNENCRYPTED:
1089 static int cfg80211_wext_giwauth(struct net_device *dev,
1090 struct iw_request_info *info,
1091 struct iw_param *data, char *extra)
1093 /* XXX: what do we need? */
1098 static int cfg80211_wext_siwpower(struct net_device *dev,
1099 struct iw_request_info *info,
1100 struct iw_param *wrq, char *extra)
1102 struct wireless_dev *wdev = dev->ieee80211_ptr;
1103 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1105 int timeout = wdev->ps_timeout;
1108 if (wdev->iftype != NL80211_IFTYPE_STATION)
1111 if (!rdev->ops->set_power_mgmt)
1114 if (wrq->disabled) {
1117 switch (wrq->flags & IW_POWER_MODE) {
1118 case IW_POWER_ON: /* If not specified */
1119 case IW_POWER_MODE: /* If set all mask */
1120 case IW_POWER_ALL_R: /* If explicitely state all */
1123 default: /* Otherwise we ignore */
1127 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
1130 if (wrq->flags & IW_POWER_TIMEOUT)
1131 timeout = wrq->value / 1000;
1134 err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
1139 wdev->ps_timeout = timeout;
1145 static int cfg80211_wext_giwpower(struct net_device *dev,
1146 struct iw_request_info *info,
1147 struct iw_param *wrq, char *extra)
1149 struct wireless_dev *wdev = dev->ieee80211_ptr;
1151 wrq->disabled = !wdev->ps;
1156 static int cfg80211_wds_wext_siwap(struct net_device *dev,
1157 struct iw_request_info *info,
1158 struct sockaddr *addr, char *extra)
1160 struct wireless_dev *wdev = dev->ieee80211_ptr;
1161 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1164 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1167 if (addr->sa_family != ARPHRD_ETHER)
1170 if (netif_running(dev))
1173 if (!rdev->ops->set_wds_peer)
1176 err = rdev_set_wds_peer(rdev, dev, (u8 *)&addr->sa_data);
1180 memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
1185 static int cfg80211_wds_wext_giwap(struct net_device *dev,
1186 struct iw_request_info *info,
1187 struct sockaddr *addr, char *extra)
1189 struct wireless_dev *wdev = dev->ieee80211_ptr;
1191 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
1194 addr->sa_family = ARPHRD_ETHER;
1195 memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
1200 static int cfg80211_wext_siwrate(struct net_device *dev,
1201 struct iw_request_info *info,
1202 struct iw_param *rate, char *extra)
1204 struct wireless_dev *wdev = dev->ieee80211_ptr;
1205 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1206 struct cfg80211_bitrate_mask mask;
1208 struct ieee80211_supported_band *sband;
1212 if (!rdev->ops->set_bitrate_mask)
1215 memset(&mask, 0, sizeof(mask));
1219 if (rate->value < 0) {
1221 } else if (rate->fixed) {
1222 fixed = rate->value / 100000;
1224 maxrate = rate->value / 100000;
1227 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1228 sband = wdev->wiphy->bands[band];
1231 for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
1232 struct ieee80211_rate *srate = &sband->bitrates[ridx];
1233 if (fixed == srate->bitrate) {
1234 mask.control[band].legacy = 1 << ridx;
1238 if (srate->bitrate <= maxrate) {
1239 mask.control[band].legacy |= 1 << ridx;
1248 return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
1251 static int cfg80211_wext_giwrate(struct net_device *dev,
1252 struct iw_request_info *info,
1253 struct iw_param *rate, char *extra)
1255 struct wireless_dev *wdev = dev->ieee80211_ptr;
1256 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1257 /* we are under RTNL - globally locked - so can use a static struct */
1258 static struct station_info sinfo;
1262 if (wdev->iftype != NL80211_IFTYPE_STATION)
1265 if (!rdev->ops->get_station)
1270 if (wdev->current_bss)
1271 memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
1278 err = rdev_get_station(rdev, dev, addr, &sinfo);
1282 if (!(sinfo.filled & BIT(NL80211_STA_INFO_TX_BITRATE)))
1285 rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
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(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;
1337 case CFG80211_SIGNAL_TYPE_UNSPEC:
1338 if (sinfo.filled & BIT(NL80211_STA_INFO_SIGNAL)) {
1339 wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
1340 wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
1341 wstats.qual.level = sinfo.signal;
1342 wstats.qual.qual = sinfo.signal;
1346 wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
1347 wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
1350 wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1351 if (sinfo.filled & BIT(NL80211_STA_INFO_RX_DROP_MISC))
1352 wstats.discard.misc = sinfo.rx_dropped_misc;
1353 if (sinfo.filled & BIT(NL80211_STA_INFO_TX_FAILED))
1354 wstats.discard.retries = sinfo.tx_failed;
1359 static int cfg80211_wext_siwap(struct net_device *dev,
1360 struct iw_request_info *info,
1361 struct sockaddr *ap_addr, char *extra)
1363 struct wireless_dev *wdev = dev->ieee80211_ptr;
1365 switch (wdev->iftype) {
1366 case NL80211_IFTYPE_ADHOC:
1367 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
1368 case NL80211_IFTYPE_STATION:
1369 return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
1370 case NL80211_IFTYPE_WDS:
1371 return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
1377 static int cfg80211_wext_giwap(struct net_device *dev,
1378 struct iw_request_info *info,
1379 struct sockaddr *ap_addr, char *extra)
1381 struct wireless_dev *wdev = dev->ieee80211_ptr;
1383 switch (wdev->iftype) {
1384 case NL80211_IFTYPE_ADHOC:
1385 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
1386 case NL80211_IFTYPE_STATION:
1387 return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
1388 case NL80211_IFTYPE_WDS:
1389 return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
1395 static int cfg80211_wext_siwessid(struct net_device *dev,
1396 struct iw_request_info *info,
1397 struct iw_point *data, char *ssid)
1399 struct wireless_dev *wdev = dev->ieee80211_ptr;
1401 switch (wdev->iftype) {
1402 case NL80211_IFTYPE_ADHOC:
1403 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
1404 case NL80211_IFTYPE_STATION:
1405 return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
1411 static int cfg80211_wext_giwessid(struct net_device *dev,
1412 struct iw_request_info *info,
1413 struct iw_point *data, char *ssid)
1415 struct wireless_dev *wdev = dev->ieee80211_ptr;
1420 switch (wdev->iftype) {
1421 case NL80211_IFTYPE_ADHOC:
1422 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
1423 case NL80211_IFTYPE_STATION:
1424 return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
1430 static int cfg80211_wext_siwpmksa(struct net_device *dev,
1431 struct iw_request_info *info,
1432 struct iw_point *data, char *extra)
1434 struct wireless_dev *wdev = dev->ieee80211_ptr;
1435 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
1436 struct cfg80211_pmksa cfg_pmksa;
1437 struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
1439 memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
1441 if (wdev->iftype != NL80211_IFTYPE_STATION)
1444 cfg_pmksa.bssid = pmksa->bssid.sa_data;
1445 cfg_pmksa.pmkid = pmksa->pmkid;
1447 switch (pmksa->cmd) {
1449 if (!rdev->ops->set_pmksa)
1452 return rdev_set_pmksa(rdev, dev, &cfg_pmksa);
1454 case IW_PMKSA_REMOVE:
1455 if (!rdev->ops->del_pmksa)
1458 return rdev_del_pmksa(rdev, dev, &cfg_pmksa);
1460 case IW_PMKSA_FLUSH:
1461 if (!rdev->ops->flush_pmksa)
1464 return rdev_flush_pmksa(rdev, dev);
1471 static const iw_handler cfg80211_handlers[] = {
1472 [IW_IOCTL_IDX(SIOCGIWNAME)] = (iw_handler) cfg80211_wext_giwname,
1473 [IW_IOCTL_IDX(SIOCSIWFREQ)] = (iw_handler) cfg80211_wext_siwfreq,
1474 [IW_IOCTL_IDX(SIOCGIWFREQ)] = (iw_handler) cfg80211_wext_giwfreq,
1475 [IW_IOCTL_IDX(SIOCSIWMODE)] = (iw_handler) cfg80211_wext_siwmode,
1476 [IW_IOCTL_IDX(SIOCGIWMODE)] = (iw_handler) cfg80211_wext_giwmode,
1477 [IW_IOCTL_IDX(SIOCGIWRANGE)] = (iw_handler) cfg80211_wext_giwrange,
1478 [IW_IOCTL_IDX(SIOCSIWAP)] = (iw_handler) cfg80211_wext_siwap,
1479 [IW_IOCTL_IDX(SIOCGIWAP)] = (iw_handler) cfg80211_wext_giwap,
1480 [IW_IOCTL_IDX(SIOCSIWMLME)] = (iw_handler) cfg80211_wext_siwmlme,
1481 [IW_IOCTL_IDX(SIOCSIWSCAN)] = (iw_handler) cfg80211_wext_siwscan,
1482 [IW_IOCTL_IDX(SIOCGIWSCAN)] = (iw_handler) cfg80211_wext_giwscan,
1483 [IW_IOCTL_IDX(SIOCSIWESSID)] = (iw_handler) cfg80211_wext_siwessid,
1484 [IW_IOCTL_IDX(SIOCGIWESSID)] = (iw_handler) cfg80211_wext_giwessid,
1485 [IW_IOCTL_IDX(SIOCSIWRATE)] = (iw_handler) cfg80211_wext_siwrate,
1486 [IW_IOCTL_IDX(SIOCGIWRATE)] = (iw_handler) cfg80211_wext_giwrate,
1487 [IW_IOCTL_IDX(SIOCSIWRTS)] = (iw_handler) cfg80211_wext_siwrts,
1488 [IW_IOCTL_IDX(SIOCGIWRTS)] = (iw_handler) cfg80211_wext_giwrts,
1489 [IW_IOCTL_IDX(SIOCSIWFRAG)] = (iw_handler) cfg80211_wext_siwfrag,
1490 [IW_IOCTL_IDX(SIOCGIWFRAG)] = (iw_handler) cfg80211_wext_giwfrag,
1491 [IW_IOCTL_IDX(SIOCSIWTXPOW)] = (iw_handler) cfg80211_wext_siwtxpower,
1492 [IW_IOCTL_IDX(SIOCGIWTXPOW)] = (iw_handler) cfg80211_wext_giwtxpower,
1493 [IW_IOCTL_IDX(SIOCSIWRETRY)] = (iw_handler) cfg80211_wext_siwretry,
1494 [IW_IOCTL_IDX(SIOCGIWRETRY)] = (iw_handler) cfg80211_wext_giwretry,
1495 [IW_IOCTL_IDX(SIOCSIWENCODE)] = (iw_handler) cfg80211_wext_siwencode,
1496 [IW_IOCTL_IDX(SIOCGIWENCODE)] = (iw_handler) cfg80211_wext_giwencode,
1497 [IW_IOCTL_IDX(SIOCSIWPOWER)] = (iw_handler) cfg80211_wext_siwpower,
1498 [IW_IOCTL_IDX(SIOCGIWPOWER)] = (iw_handler) cfg80211_wext_giwpower,
1499 [IW_IOCTL_IDX(SIOCSIWGENIE)] = (iw_handler) cfg80211_wext_siwgenie,
1500 [IW_IOCTL_IDX(SIOCSIWAUTH)] = (iw_handler) cfg80211_wext_siwauth,
1501 [IW_IOCTL_IDX(SIOCGIWAUTH)] = (iw_handler) cfg80211_wext_giwauth,
1502 [IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
1503 [IW_IOCTL_IDX(SIOCSIWPMKSA)] = (iw_handler) cfg80211_wext_siwpmksa,
1506 const struct iw_handler_def cfg80211_wext_handler = {
1507 .num_standard = ARRAY_SIZE(cfg80211_handlers),
1508 .standard = cfg80211_handlers,
1509 .get_wireless_stats = cfg80211_wireless_stats,