File Index
Files and directories
Sign in
FILE

wext-compat.c

File preview with syntax highlighting.

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