wext-compat.c 38.9 KB
Newer Older
J
Johannes Berg 已提交
1 2 3 4 5 6 7
/*
 * cfg80211 - wext compat code
 *
 * This is temporary code until all wireless functionality is migrated
 * into cfg80211, when that happens all the exports here go away and
 * we directly assign the wireless handlers of wireless interfaces.
 *
8
 * Copyright 2008-2009	Johannes Berg <johannes@sipsolutions.net>
J
Johannes Berg 已提交
9 10
 */

11
#include <linux/export.h>
J
Johannes Berg 已提交
12 13
#include <linux/wireless.h>
#include <linux/nl80211.h>
14
#include <linux/if_arp.h>
15
#include <linux/etherdevice.h>
16
#include <linux/slab.h>
J
Johannes Berg 已提交
17 18
#include <net/iw_handler.h>
#include <net/cfg80211.h>
19
#include <net/cfg80211-wext.h>
20
#include "wext-compat.h"
J
Johannes Berg 已提交
21
#include "core.h"
22
#include "rdev-ops.h"
J
Johannes Berg 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

int cfg80211_wext_giwname(struct net_device *dev,
			  struct iw_request_info *info,
			  char *name, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct ieee80211_supported_band *sband;
	bool is_ht = false, is_a = false, is_b = false, is_g = false;

	if (!wdev)
		return -EOPNOTSUPP;

	sband = wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
	if (sband) {
		is_a = true;
		is_ht |= sband->ht_cap.ht_supported;
	}

	sband = wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
	if (sband) {
		int i;
		/* Check for mandatory rates */
		for (i = 0; i < sband->n_bitrates; i++) {
			if (sband->bitrates[i].bitrate == 10)
				is_b = true;
			if (sband->bitrates[i].bitrate == 60)
				is_g = true;
		}
		is_ht |= sband->ht_cap.ht_supported;
	}

	strcpy(name, "IEEE 802.11");
	if (is_a)
		strcat(name, "a");
	if (is_b)
		strcat(name, "b");
	if (is_g)
		strcat(name, "g");
	if (is_ht)
		strcat(name, "n");

	return 0;
}
66
EXPORT_SYMBOL_GPL(cfg80211_wext_giwname);
67 68 69 70 71 72 73 74

int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
			  u32 *mode, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev;
	struct vif_params vifparams;
	enum nl80211_iftype type;
75
	int ret;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

	rdev = wiphy_to_dev(wdev->wiphy);

	switch (*mode) {
	case IW_MODE_INFRA:
		type = NL80211_IFTYPE_STATION;
		break;
	case IW_MODE_ADHOC:
		type = NL80211_IFTYPE_ADHOC;
		break;
	case IW_MODE_REPEAT:
		type = NL80211_IFTYPE_WDS;
		break;
	case IW_MODE_MONITOR:
		type = NL80211_IFTYPE_MONITOR;
		break;
	default:
		return -EINVAL;
	}

96 97 98
	if (type == wdev->iftype)
		return 0;

99 100
	memset(&vifparams, 0, sizeof(vifparams));

101 102 103
	cfg80211_lock_rdev(rdev);
	ret = cfg80211_change_iface(rdev, dev, type, NULL, &vifparams);
	cfg80211_unlock_rdev(rdev);
104 105

	return ret;
106
}
107
EXPORT_SYMBOL_GPL(cfg80211_wext_siwmode);
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
			  u32 *mode, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	if (!wdev)
		return -EOPNOTSUPP;

	switch (wdev->iftype) {
	case NL80211_IFTYPE_AP:
		*mode = IW_MODE_MASTER;
		break;
	case NL80211_IFTYPE_STATION:
		*mode = IW_MODE_INFRA;
		break;
	case NL80211_IFTYPE_ADHOC:
		*mode = IW_MODE_ADHOC;
		break;
	case NL80211_IFTYPE_MONITOR:
		*mode = IW_MODE_MONITOR;
		break;
	case NL80211_IFTYPE_WDS:
		*mode = IW_MODE_REPEAT;
		break;
	case NL80211_IFTYPE_AP_VLAN:
		*mode = IW_MODE_SECOND;		/* FIXME */
		break;
	default:
		*mode = IW_MODE_AUTO;
		break;
	}
	return 0;
}
142
EXPORT_SYMBOL_GPL(cfg80211_wext_giwmode);
143 144 145 146 147 148 149 150 151


int cfg80211_wext_giwrange(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_point *data, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct iw_range *range = (struct iw_range *) extra;
	enum ieee80211_band band;
J
Johannes Berg 已提交
152
	int i, c = 0;
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

	if (!wdev)
		return -EOPNOTSUPP;

	data->length = sizeof(struct iw_range);
	memset(range, 0, sizeof(struct iw_range));

	range->we_version_compiled = WIRELESS_EXT;
	range->we_version_source = 21;
	range->retry_capa = IW_RETRY_LIMIT;
	range->retry_flags = IW_RETRY_LIMIT;
	range->min_retry = 0;
	range->max_retry = 255;
	range->min_rts = 0;
	range->max_rts = 2347;
	range->min_frag = 256;
	range->max_frag = 2346;

	range->max_encoding_tokens = 4;

	range->max_qual.updated = IW_QUAL_NOISE_INVALID;

	switch (wdev->wiphy->signal_type) {
	case CFG80211_SIGNAL_TYPE_NONE:
		break;
	case CFG80211_SIGNAL_TYPE_MBM:
		range->max_qual.level = -110;
		range->max_qual.qual = 70;
		range->avg_qual.qual = 35;
		range->max_qual.updated |= IW_QUAL_DBM;
		range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
		range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
		break;
	case CFG80211_SIGNAL_TYPE_UNSPEC:
		range->max_qual.level = 100;
		range->max_qual.qual = 100;
		range->avg_qual.qual = 50;
		range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
		range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
		break;
	}

	range->avg_qual.level = range->max_qual.level / 2;
	range->avg_qual.noise = range->max_qual.noise / 2;
	range->avg_qual.updated = range->max_qual.updated;

J
Johannes Berg 已提交
199 200
	for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
		switch (wdev->wiphy->cipher_suites[i]) {
201
		case WLAN_CIPHER_SUITE_TKIP:
202 203
			range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
					    IW_ENC_CAPA_WPA);
204 205 206
			break;

		case WLAN_CIPHER_SUITE_CCMP:
207 208
			range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
					    IW_ENC_CAPA_WPA2);
209
			break;
210 211 212 213 214 215 216 217 218 219

		case WLAN_CIPHER_SUITE_WEP40:
			range->encoding_size[range->num_encoding_sizes++] =
				WLAN_KEY_LEN_WEP40;
			break;

		case WLAN_CIPHER_SUITE_WEP104:
			range->encoding_size[range->num_encoding_sizes++] =
				WLAN_KEY_LEN_WEP104;
			break;
220 221
		}
	}
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

	for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
		struct ieee80211_supported_band *sband;

		sband = wdev->wiphy->bands[band];

		if (!sband)
			continue;

		for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
			struct ieee80211_channel *chan = &sband->channels[i];

			if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
				range->freq[c].i =
					ieee80211_frequency_to_channel(
						chan->center_freq);
				range->freq[c].m = chan->center_freq;
				range->freq[c].e = 6;
				c++;
			}
		}
	}
	range->num_channels = c;
	range->num_frequency = c;

	IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);

251 252
	if (wdev->wiphy->max_scan_ssids > 0)
		range->scan_capa |= IW_SCAN_CAPA_ESSID;
253 254 255

	return 0;
}
256
EXPORT_SYMBOL_GPL(cfg80211_wext_giwrange);
257

J
Johannes Berg 已提交
258 259 260 261 262 263

/**
 * cfg80211_wext_freq - get wext frequency for non-"auto"
 * @wiphy: the wiphy
 * @freq: the wext freq encoding
 *
264
 * Returns a frequency, or a negative error code, or 0 for auto.
J
Johannes Berg 已提交
265
 */
266
int cfg80211_wext_freq(struct wiphy *wiphy, struct iw_freq *freq)
J
Johannes Berg 已提交
267
{
268
	/*
269
	 * Parse frequency - return 0 for auto and
270 271
	 * -EINVAL for impossible things.
	 */
J
Johannes Berg 已提交
272
	if (freq->e == 0) {
273
		enum ieee80211_band band = IEEE80211_BAND_2GHZ;
J
Johannes Berg 已提交
274
		if (freq->m < 0)
275
			return 0;
276 277 278
		if (freq->m > 14)
			band = IEEE80211_BAND_5GHZ;
		return ieee80211_channel_to_frequency(freq->m, band);
J
Johannes Berg 已提交
279 280 281 282
	} else {
		int i, div = 1000000;
		for (i = 0; i < freq->e; i++)
			div /= 10;
283
		if (div <= 0)
284 285
			return -EINVAL;
		return freq->m / div;
J
Johannes Berg 已提交
286 287
	}
}
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

int cfg80211_wext_siwrts(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *rts, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	u32 orts = wdev->wiphy->rts_threshold;
	int err;

	if (rts->disabled || !rts->fixed)
		wdev->wiphy->rts_threshold = (u32) -1;
	else if (rts->value < 0)
		return -EINVAL;
	else
		wdev->wiphy->rts_threshold = rts->value;

305
	err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_RTS_THRESHOLD);
306 307 308 309 310
	if (err)
		wdev->wiphy->rts_threshold = orts;

	return err;
}
311
EXPORT_SYMBOL_GPL(cfg80211_wext_siwrts);
312 313 314 315 316 317 318 319 320 321 322 323 324

int cfg80211_wext_giwrts(struct net_device *dev,
			 struct iw_request_info *info,
			 struct iw_param *rts, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	rts->value = wdev->wiphy->rts_threshold;
	rts->disabled = rts->value == (u32) -1;
	rts->fixed = 1;

	return 0;
}
325
EXPORT_SYMBOL_GPL(cfg80211_wext_giwrts);
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

int cfg80211_wext_siwfrag(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *frag, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	u32 ofrag = wdev->wiphy->frag_threshold;
	int err;

	if (frag->disabled || !frag->fixed)
		wdev->wiphy->frag_threshold = (u32) -1;
	else if (frag->value < 256)
		return -EINVAL;
	else {
		/* Fragment length must be even, so strip LSB. */
		wdev->wiphy->frag_threshold = frag->value & ~0x1;
	}

345
	err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_FRAG_THRESHOLD);
346 347 348 349 350
	if (err)
		wdev->wiphy->frag_threshold = ofrag;

	return err;
}
351
EXPORT_SYMBOL_GPL(cfg80211_wext_siwfrag);
352 353 354 355 356 357 358 359 360 361 362 363 364

int cfg80211_wext_giwfrag(struct net_device *dev,
			  struct iw_request_info *info,
			  struct iw_param *frag, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	frag->value = wdev->wiphy->frag_threshold;
	frag->disabled = frag->value == (u32) -1;
	frag->fixed = 1;

	return 0;
}
365
EXPORT_SYMBOL_GPL(cfg80211_wext_giwfrag);
366

367 368 369
static int cfg80211_wext_siwretry(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_param *retry, char *extra)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	u32 changed = 0;
	u8 olong = wdev->wiphy->retry_long;
	u8 oshort = wdev->wiphy->retry_short;
	int err;

	if (retry->disabled ||
	    (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
		return -EINVAL;

	if (retry->flags & IW_RETRY_LONG) {
		wdev->wiphy->retry_long = retry->value;
		changed |= WIPHY_PARAM_RETRY_LONG;
	} else if (retry->flags & IW_RETRY_SHORT) {
		wdev->wiphy->retry_short = retry->value;
		changed |= WIPHY_PARAM_RETRY_SHORT;
	} else {
		wdev->wiphy->retry_short = retry->value;
		wdev->wiphy->retry_long = retry->value;
		changed |= WIPHY_PARAM_RETRY_LONG;
		changed |= WIPHY_PARAM_RETRY_SHORT;
	}

	if (!changed)
		return 0;

398
	err = rdev_set_wiphy_params(rdev, changed);
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	if (err) {
		wdev->wiphy->retry_short = oshort;
		wdev->wiphy->retry_long = olong;
	}

	return err;
}

int cfg80211_wext_giwretry(struct net_device *dev,
			   struct iw_request_info *info,
			   struct iw_param *retry, char *extra)
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	retry->disabled = 0;

	if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
		/*
		 * First return short value, iwconfig will ask long value
		 * later if needed
		 */
		retry->flags |= IW_RETRY_LIMIT;
		retry->value = wdev->wiphy->retry_short;
		if (wdev->wiphy->retry_long != wdev->wiphy->retry_short)
			retry->flags |= IW_RETRY_LONG;

		return 0;
	}

	if (retry->flags & IW_RETRY_LONG) {
		retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
		retry->value = wdev->wiphy->retry_long;
	}

	return 0;
}
435
EXPORT_SYMBOL_GPL(cfg80211_wext_giwretry);
436

J
Johannes Berg 已提交
437
static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
438 439 440
				     struct net_device *dev, bool pairwise,
				     const u8 *addr, bool remove, bool tx_key,
				     int idx, struct key_params *params)
441 442
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
J
Johannes Berg 已提交
443
	int err, i;
444
	bool rejoin = false;
J
Johannes Berg 已提交
445

446 447 448
	if (pairwise && !addr)
		return -EINVAL;

J
Johannes Berg 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461
	if (!wdev->wext.keys) {
		wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
					      GFP_KERNEL);
		if (!wdev->wext.keys)
			return -ENOMEM;
		for (i = 0; i < 6; i++)
			wdev->wext.keys->params[i].key =
				wdev->wext.keys->data[i];
	}

	if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
	    wdev->iftype != NL80211_IFTYPE_STATION)
		return -EOPNOTSUPP;
462 463

	if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
J
Johannes Berg 已提交
464 465 466
		if (!wdev->current_bss)
			return -ENOLINK;

467 468 469 470 471 472 473 474 475
		if (!rdev->ops->set_default_mgmt_key)
			return -EOPNOTSUPP;

		if (idx < 4 || idx > 5)
			return -EINVAL;
	} else if (idx < 0 || idx > 3)
		return -EINVAL;

	if (remove) {
J
Johannes Berg 已提交
476
		err = 0;
477 478 479 480 481 482 483 484 485 486
		if (wdev->current_bss) {
			/*
			 * If removing the current TX key, we will need to
			 * join a new IBSS without the privacy bit clear.
			 */
			if (idx == wdev->wext.default_key &&
			    wdev->iftype == NL80211_IFTYPE_ADHOC) {
				__cfg80211_leave_ibss(rdev, wdev->netdev, true);
				rejoin = true;
			}
487 488 489 490 491

			if (!pairwise && addr &&
			    !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
				err = -ENOENT;
			else
492 493
				err = rdev_del_key(rdev, dev, idx, pairwise,
						   addr);
494
		}
495
		wdev->wext.connect.privacy = false;
496 497 498 499 500 501
		/*
		 * Applications using wireless extensions expect to be
		 * able to delete keys that don't exist, so allow that.
		 */
		if (err == -ENOENT)
			err = 0;
502
		if (!err) {
J
Johannes Berg 已提交
503 504 505 506
			if (!addr) {
				wdev->wext.keys->params[idx].key_len = 0;
				wdev->wext.keys->params[idx].cipher = 0;
			}
507 508 509 510 511
			if (idx == wdev->wext.default_key)
				wdev->wext.default_key = -1;
			else if (idx == wdev->wext.default_mgmt_key)
				wdev->wext.default_mgmt_key = -1;
		}
512 513 514

		if (!err && rejoin)
			err = cfg80211_ibss_wext_join(rdev, wdev);
515

516
		return err;
J
Johannes Berg 已提交
517
	}
518

J
Johannes Berg 已提交
519 520 521
	if (addr)
		tx_key = false;

522
	if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
J
Johannes Berg 已提交
523
		return -EINVAL;
524

J
Johannes Berg 已提交
525 526
	err = 0;
	if (wdev->current_bss)
527
		err = rdev_add_key(rdev, dev, idx, pairwise, addr, params);
J
Johannes Berg 已提交
528 529 530 531 532 533 534 535 536 537
	if (err)
		return err;

	if (!addr) {
		wdev->wext.keys->params[idx] = *params;
		memcpy(wdev->wext.keys->data[idx],
			params->key, params->key_len);
		wdev->wext.keys->params[idx].key =
			wdev->wext.keys->data[idx];
	}
538

Z
Zhu Yi 已提交
539 540
	if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
	     params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
J
Johannes Berg 已提交
541
	    (tx_key || (!addr && wdev->wext.default_key == -1))) {
542 543 544 545 546 547 548 549 550 551 552
		if (wdev->current_bss) {
			/*
			 * If we are getting a new TX key from not having
			 * had one before we need to join a new IBSS with
			 * the privacy bit set.
			 */
			if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
			    wdev->wext.default_key == -1) {
				__cfg80211_leave_ibss(rdev, wdev->netdev, true);
				rejoin = true;
			}
553
			err = rdev_set_default_key(rdev, dev, idx, true, true);
554 555
		}
		if (!err) {
J
Johannes Berg 已提交
556
			wdev->wext.default_key = idx;
557 558 559
			if (rejoin)
				err = cfg80211_ibss_wext_join(rdev, wdev);
		}
J
Johannes Berg 已提交
560 561
		return err;
	}
562

J
Johannes Berg 已提交
563 564 565
	if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
	    (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
		if (wdev->current_bss)
566
			err = rdev_set_default_mgmt_key(rdev, dev, idx);
J
Johannes Berg 已提交
567 568 569
		if (!err)
			wdev->wext.default_mgmt_key = idx;
		return err;
570
	}
J
Johannes Berg 已提交
571 572 573 574 575

	return 0;
}

static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
576 577 578
				   struct net_device *dev, bool pairwise,
				   const u8 *addr, bool remove, bool tx_key,
				   int idx, struct key_params *params)
J
Johannes Berg 已提交
579 580 581
{
	int err;

582 583
	/* devlist mutex needed for possible IBSS re-join */
	mutex_lock(&rdev->devlist_mtx);
J
Johannes Berg 已提交
584
	wdev_lock(dev->ieee80211_ptr);
585 586
	err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
					remove, tx_key, idx, params);
J
Johannes Berg 已提交
587
	wdev_unlock(dev->ieee80211_ptr);
588
	mutex_unlock(&rdev->devlist_mtx);
J
Johannes Berg 已提交
589 590

	return err;
591 592
}

593 594 595
static int cfg80211_wext_siwencode(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_point *erq, char *keybuf)
596 597 598 599 600 601 602
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	int idx, err;
	bool remove = false;
	struct key_params params;

J
Johannes Berg 已提交
603 604 605 606
	if (wdev->iftype != NL80211_IFTYPE_STATION &&
	    wdev->iftype != NL80211_IFTYPE_ADHOC)
		return -EOPNOTSUPP;

607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
	/* no use -- only MFP (set_default_mgmt_key) is optional */
	if (!rdev->ops->del_key ||
	    !rdev->ops->add_key ||
	    !rdev->ops->set_default_key)
		return -EOPNOTSUPP;

	idx = erq->flags & IW_ENCODE_INDEX;
	if (idx == 0) {
		idx = wdev->wext.default_key;
		if (idx < 0)
			idx = 0;
	} else if (idx < 1 || idx > 4)
		return -EINVAL;
	else
		idx--;

	if (erq->flags & IW_ENCODE_DISABLED)
		remove = true;
	else if (erq->length == 0) {
		/* No key data - just set the default TX key index */
J
Johannes Berg 已提交
627 628 629
		err = 0;
		wdev_lock(wdev);
		if (wdev->current_bss)
630 631
			err = rdev_set_default_key(rdev, dev, idx, true,
						   true);
632 633
		if (!err)
			wdev->wext.default_key = idx;
J
Johannes Berg 已提交
634
		wdev_unlock(wdev);
635 636 637 638 639 640 641 642 643 644 645 646 647
		return err;
	}

	memset(&params, 0, sizeof(params));
	params.key = keybuf;
	params.key_len = erq->length;
	if (erq->length == 5)
		params.cipher = WLAN_CIPHER_SUITE_WEP40;
	else if (erq->length == 13)
		params.cipher = WLAN_CIPHER_SUITE_WEP104;
	else if (!remove)
		return -EINVAL;

648
	return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
649 650 651 652
				       wdev->wext.default_key == -1,
				       idx, &params);
}

653 654 655
static int cfg80211_wext_siwencodeext(struct net_device *dev,
				      struct iw_request_info *info,
				      struct iw_point *erq, char *extra)
656 657 658 659 660 661 662 663 664 665
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
	const u8 *addr;
	int idx;
	bool remove = false;
	struct key_params params;
	u32 cipher;

J
Johannes Berg 已提交
666 667 668 669
	if (wdev->iftype != NL80211_IFTYPE_STATION &&
	    wdev->iftype != NL80211_IFTYPE_ADHOC)
		return -EOPNOTSUPP;

670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
	/* no use -- only MFP (set_default_mgmt_key) is optional */
	if (!rdev->ops->del_key ||
	    !rdev->ops->add_key ||
	    !rdev->ops->set_default_key)
		return -EOPNOTSUPP;

	switch (ext->alg) {
	case IW_ENCODE_ALG_NONE:
		remove = true;
		cipher = 0;
		break;
	case IW_ENCODE_ALG_WEP:
		if (ext->key_len == 5)
			cipher = WLAN_CIPHER_SUITE_WEP40;
		else if (ext->key_len == 13)
			cipher = WLAN_CIPHER_SUITE_WEP104;
		else
			return -EINVAL;
		break;
	case IW_ENCODE_ALG_TKIP:
		cipher = WLAN_CIPHER_SUITE_TKIP;
		break;
	case IW_ENCODE_ALG_CCMP:
		cipher = WLAN_CIPHER_SUITE_CCMP;
		break;
	case IW_ENCODE_ALG_AES_CMAC:
		cipher = WLAN_CIPHER_SUITE_AES_CMAC;
		break;
	default:
		return -EOPNOTSUPP;
	}

	if (erq->flags & IW_ENCODE_DISABLED)
		remove = true;

	idx = erq->flags & IW_ENCODE_INDEX;
	if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
		if (idx < 4 || idx > 5) {
			idx = wdev->wext.default_mgmt_key;
			if (idx < 0)
				return -EINVAL;
		} else
			idx--;
	} else {
		if (idx < 1 || idx > 4) {
			idx = wdev->wext.default_key;
			if (idx < 0)
				return -EINVAL;
		} else
			idx--;
	}

	addr = ext->addr.sa_data;
	if (is_broadcast_ether_addr(addr))
		addr = NULL;

	memset(&params, 0, sizeof(params));
	params.key = ext->key;
	params.key_len = ext->key_len;
	params.cipher = cipher;

731 732 733 734 735
	if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
		params.seq = ext->rx_seq;
		params.seq_len = 6;
	}

736
	return cfg80211_set_encryption(
737 738 739
			rdev, dev,
			!(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
			addr, remove,
740 741 742 743
			ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
			idx, &params);
}

744 745 746
static int cfg80211_wext_giwencode(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_point *erq, char *keybuf)
747 748
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
J
Johannes Berg 已提交
749
	int idx;
750

J
Johannes Berg 已提交
751 752
	if (wdev->iftype != NL80211_IFTYPE_STATION &&
	    wdev->iftype != NL80211_IFTYPE_ADHOC)
753 754 755 756 757 758 759 760 761 762 763 764 765 766
		return -EOPNOTSUPP;

	idx = erq->flags & IW_ENCODE_INDEX;
	if (idx == 0) {
		idx = wdev->wext.default_key;
		if (idx < 0)
			idx = 0;
	} else if (idx < 1 || idx > 4)
		return -EINVAL;
	else
		idx--;

	erq->flags = idx + 1;

J
Johannes Berg 已提交
767
	if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
768 769 770 771 772
		erq->flags |= IW_ENCODE_DISABLED;
		erq->length = 0;
		return 0;
	}

J
Johannes Berg 已提交
773 774 775 776 777 778
	erq->length = min_t(size_t, erq->length,
			    wdev->wext.keys->params[idx].key_len);
	memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
	erq->flags |= IW_ENCODE_ENABLED;

	return 0;
779
}
780

781 782 783
static int cfg80211_wext_siwfreq(struct net_device *dev,
				 struct iw_request_info *info,
				 struct iw_freq *wextfreq, char *extra)
784 785 786
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
787 788 789
	struct cfg80211_chan_def chandef = {
		._type = NL80211_CHAN_NO_HT,
	};
790
	int freq, err;
791 792 793

	switch (wdev->iftype) {
	case NL80211_IFTYPE_STATION:
794
		return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
795
	case NL80211_IFTYPE_ADHOC:
796
		return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
797
	case NL80211_IFTYPE_MONITOR:
798 799 800 801
		freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
		if (freq < 0)
			return freq;
		if (freq == 0)
802
			return -EINVAL;
803 804 805
		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
		if (!chandef.chan)
			return -EINVAL;
806
		mutex_lock(&rdev->devlist_mtx);
807
		err = cfg80211_set_monitor_channel(rdev, &chandef);
808
		mutex_unlock(&rdev->devlist_mtx);
809
		return err;
810 811 812 813 814 815
	case NL80211_IFTYPE_MESH_POINT:
		freq = cfg80211_wext_freq(wdev->wiphy, wextfreq);
		if (freq < 0)
			return freq;
		if (freq == 0)
			return -EINVAL;
816 817 818
		chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
		if (!chandef.chan)
			return -EINVAL;
819
		mutex_lock(&rdev->devlist_mtx);
820
		err = cfg80211_set_mesh_channel(rdev, wdev, &chandef);
821 822
		mutex_unlock(&rdev->devlist_mtx);
		return err;
823 824
	default:
		return -EOPNOTSUPP;
825 826 827
	}
}

828 829 830
static int cfg80211_wext_giwfreq(struct net_device *dev,
				 struct iw_request_info *info,
				 struct iw_freq *freq, char *extra)
831 832
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
833
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
834 835
	struct cfg80211_chan_def chandef;
	int ret;
836 837 838 839 840 841

	switch (wdev->iftype) {
	case NL80211_IFTYPE_STATION:
		return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
	case NL80211_IFTYPE_ADHOC:
		return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
842
	case NL80211_IFTYPE_MONITOR:
843
		if (!rdev->ops->get_channel)
844 845
			return -EINVAL;

846 847 848 849
		ret = rdev_get_channel(rdev, wdev, &chandef);
		if (ret)
			return ret;
		freq->m = chandef.chan->center_freq;
850 851
		freq->e = 6;
		return 0;
852
	default:
853
		return -EINVAL;
854 855 856
	}
}

857 858 859
static int cfg80211_wext_siwtxpower(struct net_device *dev,
				    struct iw_request_info *info,
				    union iwreq_data *data, char *extra)
860 861 862
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
863
	enum nl80211_tx_power_setting type;
864 865 866 867 868 869 870 871 872 873 874 875
	int dbm = 0;

	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
		return -EINVAL;
	if (data->txpower.flags & IW_TXPOW_RANGE)
		return -EINVAL;

	if (!rdev->ops->set_tx_power)
		return -EOPNOTSUPP;

	/* only change when not disabling */
	if (!data->txpower.disabled) {
J
Johannes Berg 已提交
876 877
		rfkill_set_sw_state(rdev->rfkill, false);

878 879 880 881 882 883 884 885
		if (data->txpower.fixed) {
			/*
			 * wext doesn't support negative values, see
			 * below where it's for automatic
			 */
			if (data->txpower.value < 0)
				return -EINVAL;
			dbm = data->txpower.value;
886
			type = NL80211_TX_POWER_FIXED;
887 888 889 890 891 892 893
			/* TODO: do regulatory check! */
		} else {
			/*
			 * Automatic power level setting, max being the value
			 * passed in from userland.
			 */
			if (data->txpower.value < 0) {
894
				type = NL80211_TX_POWER_AUTOMATIC;
895 896
			} else {
				dbm = data->txpower.value;
897
				type = NL80211_TX_POWER_LIMITED;
898 899 900
			}
		}
	} else {
J
Johannes Berg 已提交
901 902 903
		rfkill_set_sw_state(rdev->rfkill, true);
		schedule_work(&rdev->rfkill_sync);
		return 0;
904 905
	}

906
	return rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
907 908
}

909 910 911
static int cfg80211_wext_giwtxpower(struct net_device *dev,
				    struct iw_request_info *info,
				    union iwreq_data *data, char *extra)
912 913 914 915 916 917 918 919 920 921 922 923 924
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	int err, val;

	if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
		return -EINVAL;
	if (data->txpower.flags & IW_TXPOW_RANGE)
		return -EINVAL;

	if (!rdev->ops->get_tx_power)
		return -EOPNOTSUPP;

925
	err = rdev_get_tx_power(rdev, wdev, &val);
J
Johannes Berg 已提交
926
	if (err)
927 928 929 930
		return err;

	/* well... oh well */
	data->txpower.fixed = 1;
J
Johannes Berg 已提交
931
	data->txpower.disabled = rfkill_blocked(rdev->rfkill);
932 933 934 935 936
	data->txpower.value = val;
	data->txpower.flags = IW_TXPOW_DBM;

	return 0;
}
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
				 s32 auth_alg)
{
	int nr_alg = 0;

	if (!auth_alg)
		return -EINVAL;

	if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
			 IW_AUTH_ALG_SHARED_KEY |
			 IW_AUTH_ALG_LEAP))
		return -EINVAL;

	if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
		nr_alg++;
		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
	}

	if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
		nr_alg++;
		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
	}

	if (auth_alg & IW_AUTH_ALG_LEAP) {
		nr_alg++;
		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
	}

	if (nr_alg > 1)
		wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;

	return 0;
}

static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
{
	if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
975 976
			     IW_AUTH_WPA_VERSION_WPA2|
		             IW_AUTH_WPA_VERSION_DISABLED))
977 978
		return -EINVAL;

979 980 981 982 983 984 985 986 987
	if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
	    (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
			     IW_AUTH_WPA_VERSION_WPA2)))
		return -EINVAL;

	if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
		wdev->wext.connect.crypto.wpa_versions &=
			~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);

988 989 990 991 992 993 994 995 996 997 998
	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
		wdev->wext.connect.crypto.wpa_versions |=
			NL80211_WPA_VERSION_1;

	if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
		wdev->wext.connect.crypto.wpa_versions |=
			NL80211_WPA_VERSION_2;

	return 0;
}

999
static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
{
	if (cipher & IW_AUTH_CIPHER_WEP40)
		wdev->wext.connect.crypto.cipher_group =
			WLAN_CIPHER_SUITE_WEP40;
	else if (cipher & IW_AUTH_CIPHER_WEP104)
		wdev->wext.connect.crypto.cipher_group =
			WLAN_CIPHER_SUITE_WEP104;
	else if (cipher & IW_AUTH_CIPHER_TKIP)
		wdev->wext.connect.crypto.cipher_group =
			WLAN_CIPHER_SUITE_TKIP;
	else if (cipher & IW_AUTH_CIPHER_CCMP)
		wdev->wext.connect.crypto.cipher_group =
			WLAN_CIPHER_SUITE_CCMP;
	else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
		wdev->wext.connect.crypto.cipher_group =
			WLAN_CIPHER_SUITE_AES_CMAC;
1016 1017
	else if (cipher & IW_AUTH_CIPHER_NONE)
		wdev->wext.connect.crypto.cipher_group = 0;
1018 1019 1020 1021 1022 1023
	else
		return -EINVAL;

	return 0;
}

1024
static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
{
	int nr_ciphers = 0;
	u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;

	if (cipher & IW_AUTH_CIPHER_WEP40) {
		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
		nr_ciphers++;
	}

	if (cipher & IW_AUTH_CIPHER_WEP104) {
		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
		nr_ciphers++;
	}

	if (cipher & IW_AUTH_CIPHER_TKIP) {
		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
		nr_ciphers++;
	}

	if (cipher & IW_AUTH_CIPHER_CCMP) {
		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
		nr_ciphers++;
	}

	if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
		ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
		nr_ciphers++;
	}

	BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);

	wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;

	return 0;
}


1062
static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
{
	int nr_akm_suites = 0;

	if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
			IW_AUTH_KEY_MGMT_PSK))
		return -EINVAL;

	if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
			WLAN_AKM_SUITE_8021X;
		nr_akm_suites++;
	}

	if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
		wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
			WLAN_AKM_SUITE_PSK;
		nr_akm_suites++;
	}

	wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;

	return 0;
}

1087 1088 1089
static int cfg80211_wext_siwauth(struct net_device *dev,
				 struct iw_request_info *info,
				 struct iw_param *data, char *extra)
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	if (wdev->iftype != NL80211_IFTYPE_STATION)
		return -EOPNOTSUPP;

	switch (data->flags & IW_AUTH_INDEX) {
	case IW_AUTH_PRIVACY_INVOKED:
		wdev->wext.connect.privacy = data->value;
		return 0;
	case IW_AUTH_WPA_VERSION:
		return cfg80211_set_wpa_version(wdev, data->value);
	case IW_AUTH_CIPHER_GROUP:
		return cfg80211_set_cipher_group(wdev, data->value);
	case IW_AUTH_KEY_MGMT:
		return cfg80211_set_key_mgt(wdev, data->value);
	case IW_AUTH_CIPHER_PAIRWISE:
		return cfg80211_set_cipher_pairwise(wdev, data->value);
	case IW_AUTH_80211_AUTH_ALG:
		return cfg80211_set_auth_alg(wdev, data->value);
	case IW_AUTH_WPA_ENABLED:
	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
	case IW_AUTH_DROP_UNENCRYPTED:
	case IW_AUTH_MFP:
		return 0;
	default:
		return -EOPNOTSUPP;
	}
}

1120 1121 1122
static int cfg80211_wext_giwauth(struct net_device *dev,
				 struct iw_request_info *info,
				 struct iw_param *data, char *extra)
1123 1124 1125 1126 1127
{
	/* XXX: what do we need? */

	return -EOPNOTSUPP;
}
J
Johannes Berg 已提交
1128

1129 1130 1131
static int cfg80211_wext_siwpower(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_param *wrq, char *extra)
J
Johannes Berg 已提交
1132 1133 1134
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
K
Kalle Valo 已提交
1135 1136
	bool ps = wdev->ps;
	int timeout = wdev->ps_timeout;
J
Johannes Berg 已提交
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	int err;

	if (wdev->iftype != NL80211_IFTYPE_STATION)
		return -EINVAL;

	if (!rdev->ops->set_power_mgmt)
		return -EOPNOTSUPP;

	if (wrq->disabled) {
		ps = false;
	} else {
		switch (wrq->flags & IW_POWER_MODE) {
		case IW_POWER_ON:       /* If not specified */
		case IW_POWER_MODE:     /* If set all mask */
		case IW_POWER_ALL_R:    /* If explicitely state all */
			ps = true;
			break;
		default:                /* Otherwise we ignore */
			return -EINVAL;
		}

		if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
			return -EINVAL;

		if (wrq->flags & IW_POWER_TIMEOUT)
			timeout = wrq->value / 1000;
	}

1165
	err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
J
Johannes Berg 已提交
1166 1167 1168
	if (err)
		return err;

K
Kalle Valo 已提交
1169 1170
	wdev->ps = ps;
	wdev->ps_timeout = timeout;
J
Johannes Berg 已提交
1171 1172 1173 1174 1175

	return 0;

}

1176 1177 1178
static int cfg80211_wext_giwpower(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_param *wrq, char *extra)
J
Johannes Berg 已提交
1179 1180 1181
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

K
Kalle Valo 已提交
1182
	wrq->disabled = !wdev->ps;
J
Johannes Berg 已提交
1183 1184 1185

	return 0;
}
J
Johannes Berg 已提交
1186

J
Johannes Berg 已提交
1187 1188 1189
static int cfg80211_wds_wext_siwap(struct net_device *dev,
				   struct iw_request_info *info,
				   struct sockaddr *addr, char *extra)
J
Johannes Berg 已提交
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	int err;

	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
		return -EINVAL;

	if (addr->sa_family != ARPHRD_ETHER)
		return -EINVAL;

	if (netif_running(dev))
		return -EBUSY;

	if (!rdev->ops->set_wds_peer)
		return -EOPNOTSUPP;

1207
	err = rdev_set_wds_peer(rdev, dev, (u8 *)&addr->sa_data);
J
Johannes Berg 已提交
1208 1209 1210 1211 1212 1213 1214 1215
	if (err)
		return err;

	memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);

	return 0;
}

J
Johannes Berg 已提交
1216 1217 1218
static int cfg80211_wds_wext_giwap(struct net_device *dev,
				   struct iw_request_info *info,
				   struct sockaddr *addr, char *extra)
J
Johannes Berg 已提交
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
		return -EINVAL;

	addr->sa_family = ARPHRD_ETHER;
	memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);

	return 0;
}
J
Johannes Berg 已提交
1230

1231 1232 1233
static int cfg80211_wext_siwrate(struct net_device *dev,
				 struct iw_request_info *info,
				 struct iw_param *rate, char *extra)
J
Johannes Berg 已提交
1234 1235 1236 1237
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	struct cfg80211_bitrate_mask mask;
1238 1239 1240 1241
	u32 fixed, maxrate;
	struct ieee80211_supported_band *sband;
	int band, ridx;
	bool match = false;
J
Johannes Berg 已提交
1242 1243 1244 1245

	if (!rdev->ops->set_bitrate_mask)
		return -EOPNOTSUPP;

1246 1247
	memset(&mask, 0, sizeof(mask));
	fixed = 0;
1248
	maxrate = (u32)-1;
J
Johannes Berg 已提交
1249 1250 1251 1252

	if (rate->value < 0) {
		/* nothing */
	} else if (rate->fixed) {
1253
		fixed = rate->value / 100000;
J
Johannes Berg 已提交
1254
	} else {
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
		maxrate = rate->value / 100000;
	}

	for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
		sband = wdev->wiphy->bands[band];
		if (sband == NULL)
			continue;
		for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
			struct ieee80211_rate *srate = &sband->bitrates[ridx];
			if (fixed == srate->bitrate) {
				mask.control[band].legacy = 1 << ridx;
				match = true;
				break;
			}
			if (srate->bitrate <= maxrate) {
				mask.control[band].legacy |= 1 << ridx;
				match = true;
			}
		}
J
Johannes Berg 已提交
1274 1275
	}

1276 1277 1278
	if (!match)
		return -EINVAL;

1279
	return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
J
Johannes Berg 已提交
1280 1281
}

1282 1283 1284
static int cfg80211_wext_giwrate(struct net_device *dev,
				 struct iw_request_info *info,
				 struct iw_param *rate, char *extra)
J
Johannes Berg 已提交
1285 1286 1287 1288 1289
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	/* we are under RTNL - globally locked - so can use a static struct */
	static struct station_info sinfo;
J
Johannes Berg 已提交
1290
	u8 addr[ETH_ALEN];
J
Johannes Berg 已提交
1291 1292 1293 1294 1295 1296 1297 1298
	int err;

	if (wdev->iftype != NL80211_IFTYPE_STATION)
		return -EOPNOTSUPP;

	if (!rdev->ops->get_station)
		return -EOPNOTSUPP;

J
Johannes Berg 已提交
1299 1300
	err = 0;
	wdev_lock(wdev);
1301
	if (wdev->current_bss)
J
Johannes Berg 已提交
1302
		memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
1303
	else
J
Johannes Berg 已提交
1304 1305 1306 1307
		err = -EOPNOTSUPP;
	wdev_unlock(wdev);
	if (err)
		return err;
J
Johannes Berg 已提交
1308

1309
	err = rdev_get_station(rdev, dev, addr, &sinfo);
J
Johannes Berg 已提交
1310 1311 1312 1313 1314 1315
	if (err)
		return err;

	if (!(sinfo.filled & STATION_INFO_TX_BITRATE))
		return -EOPNOTSUPP;

1316
	rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
J
Johannes Berg 已提交
1317 1318 1319

	return 0;
}
1320 1321

/* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
1322
static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
1323 1324 1325 1326 1327 1328
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	/* we are under RTNL - globally locked - so can use static structs */
	static struct iw_statistics wstats;
	static struct station_info sinfo;
J
Johannes Berg 已提交
1329
	u8 bssid[ETH_ALEN];
1330 1331 1332 1333 1334 1335 1336

	if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
		return NULL;

	if (!rdev->ops->get_station)
		return NULL;

J
Johannes Berg 已提交
1337 1338 1339 1340
	/* Grab BSSID of current BSS, if any */
	wdev_lock(wdev);
	if (!wdev->current_bss) {
		wdev_unlock(wdev);
1341
		return NULL;
J
Johannes Berg 已提交
1342 1343 1344
	}
	memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
	wdev_unlock(wdev);
1345

1346
	if (rdev_get_station(rdev, dev, bssid, &sinfo))
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
		return NULL;

	memset(&wstats, 0, sizeof(wstats));

	switch (rdev->wiphy.signal_type) {
	case CFG80211_SIGNAL_TYPE_MBM:
		if (sinfo.filled & STATION_INFO_SIGNAL) {
			int sig = sinfo.signal;
			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
			wstats.qual.updated |= IW_QUAL_DBM;
			wstats.qual.level = sig;
			if (sig < -110)
				sig = -110;
			else if (sig > -40)
				sig = -40;
			wstats.qual.qual = sig + 110;
			break;
		}
	case CFG80211_SIGNAL_TYPE_UNSPEC:
		if (sinfo.filled & STATION_INFO_SIGNAL) {
			wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
			wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
			wstats.qual.level = sinfo.signal;
			wstats.qual.qual = sinfo.signal;
			break;
		}
	default:
		wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
		wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
	}

	wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
1380 1381 1382 1383
	if (sinfo.filled & STATION_INFO_RX_DROP_MISC)
		wstats.discard.misc = sinfo.rx_dropped_misc;
	if (sinfo.filled & STATION_INFO_TX_FAILED)
		wstats.discard.retries = sinfo.tx_failed;
1384 1385 1386

	return &wstats;
}
J
Johannes Berg 已提交
1387

1388 1389 1390
static int cfg80211_wext_siwap(struct net_device *dev,
			       struct iw_request_info *info,
			       struct sockaddr *ap_addr, char *extra)
J
Johannes Berg 已提交
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	switch (wdev->iftype) {
	case NL80211_IFTYPE_ADHOC:
		return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
	case NL80211_IFTYPE_STATION:
		return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
	case NL80211_IFTYPE_WDS:
		return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
	default:
		return -EOPNOTSUPP;
	}
}

1406 1407 1408
static int cfg80211_wext_giwap(struct net_device *dev,
			       struct iw_request_info *info,
			       struct sockaddr *ap_addr, char *extra)
J
Johannes Berg 已提交
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	switch (wdev->iftype) {
	case NL80211_IFTYPE_ADHOC:
		return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
	case NL80211_IFTYPE_STATION:
		return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
	case NL80211_IFTYPE_WDS:
		return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
	default:
		return -EOPNOTSUPP;
	}
}
1423

1424 1425 1426
static int cfg80211_wext_siwessid(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_point *data, char *ssid)
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

	switch (wdev->iftype) {
	case NL80211_IFTYPE_ADHOC:
		return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
	case NL80211_IFTYPE_STATION:
		return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
	default:
		return -EOPNOTSUPP;
	}
}

1440 1441 1442
static int cfg80211_wext_giwessid(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_point *data, char *ssid)
1443 1444 1445
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;

1446 1447 1448
	data->flags = 0;
	data->length = 0;

1449 1450 1451 1452 1453 1454 1455 1456 1457
	switch (wdev->iftype) {
	case NL80211_IFTYPE_ADHOC:
		return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
	case NL80211_IFTYPE_STATION:
		return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
	default:
		return -EOPNOTSUPP;
	}
}
1458

1459 1460 1461
static int cfg80211_wext_siwpmksa(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_point *data, char *extra)
1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
{
	struct wireless_dev *wdev = dev->ieee80211_ptr;
	struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
	struct cfg80211_pmksa cfg_pmksa;
	struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;

	memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));

	if (wdev->iftype != NL80211_IFTYPE_STATION)
		return -EINVAL;

	cfg_pmksa.bssid = pmksa->bssid.sa_data;
	cfg_pmksa.pmkid = pmksa->pmkid;

	switch (pmksa->cmd) {
	case IW_PMKSA_ADD:
		if (!rdev->ops->set_pmksa)
			return -EOPNOTSUPP;

1481
		return rdev_set_pmksa(rdev, dev, &cfg_pmksa);
1482 1483 1484 1485 1486

	case IW_PMKSA_REMOVE:
		if (!rdev->ops->del_pmksa)
			return -EOPNOTSUPP;

1487
		return rdev_del_pmksa(rdev, dev, &cfg_pmksa);
1488 1489 1490 1491 1492

	case IW_PMKSA_FLUSH:
		if (!rdev->ops->flush_pmksa)
			return -EOPNOTSUPP;

1493
		return rdev_flush_pmksa(rdev, dev);
1494 1495 1496 1497 1498 1499

	default:
		return -EOPNOTSUPP;
	}
}

1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
static const iw_handler cfg80211_handlers[] = {
	[IW_IOCTL_IDX(SIOCGIWNAME)]	= (iw_handler) cfg80211_wext_giwname,
	[IW_IOCTL_IDX(SIOCSIWFREQ)]	= (iw_handler) cfg80211_wext_siwfreq,
	[IW_IOCTL_IDX(SIOCGIWFREQ)]	= (iw_handler) cfg80211_wext_giwfreq,
	[IW_IOCTL_IDX(SIOCSIWMODE)]	= (iw_handler) cfg80211_wext_siwmode,
	[IW_IOCTL_IDX(SIOCGIWMODE)]	= (iw_handler) cfg80211_wext_giwmode,
	[IW_IOCTL_IDX(SIOCGIWRANGE)]	= (iw_handler) cfg80211_wext_giwrange,
	[IW_IOCTL_IDX(SIOCSIWAP)]	= (iw_handler) cfg80211_wext_siwap,
	[IW_IOCTL_IDX(SIOCGIWAP)]	= (iw_handler) cfg80211_wext_giwap,
	[IW_IOCTL_IDX(SIOCSIWMLME)]	= (iw_handler) cfg80211_wext_siwmlme,
	[IW_IOCTL_IDX(SIOCSIWSCAN)]	= (iw_handler) cfg80211_wext_siwscan,
	[IW_IOCTL_IDX(SIOCGIWSCAN)]	= (iw_handler) cfg80211_wext_giwscan,
	[IW_IOCTL_IDX(SIOCSIWESSID)]	= (iw_handler) cfg80211_wext_siwessid,
	[IW_IOCTL_IDX(SIOCGIWESSID)]	= (iw_handler) cfg80211_wext_giwessid,
	[IW_IOCTL_IDX(SIOCSIWRATE)]	= (iw_handler) cfg80211_wext_siwrate,
	[IW_IOCTL_IDX(SIOCGIWRATE)]	= (iw_handler) cfg80211_wext_giwrate,
	[IW_IOCTL_IDX(SIOCSIWRTS)]	= (iw_handler) cfg80211_wext_siwrts,
	[IW_IOCTL_IDX(SIOCGIWRTS)]	= (iw_handler) cfg80211_wext_giwrts,
	[IW_IOCTL_IDX(SIOCSIWFRAG)]	= (iw_handler) cfg80211_wext_siwfrag,
	[IW_IOCTL_IDX(SIOCGIWFRAG)]	= (iw_handler) cfg80211_wext_giwfrag,
	[IW_IOCTL_IDX(SIOCSIWTXPOW)]	= (iw_handler) cfg80211_wext_siwtxpower,
	[IW_IOCTL_IDX(SIOCGIWTXPOW)]	= (iw_handler) cfg80211_wext_giwtxpower,
	[IW_IOCTL_IDX(SIOCSIWRETRY)]	= (iw_handler) cfg80211_wext_siwretry,
	[IW_IOCTL_IDX(SIOCGIWRETRY)]	= (iw_handler) cfg80211_wext_giwretry,
	[IW_IOCTL_IDX(SIOCSIWENCODE)]	= (iw_handler) cfg80211_wext_siwencode,
	[IW_IOCTL_IDX(SIOCGIWENCODE)]	= (iw_handler) cfg80211_wext_giwencode,
	[IW_IOCTL_IDX(SIOCSIWPOWER)]	= (iw_handler) cfg80211_wext_siwpower,
	[IW_IOCTL_IDX(SIOCGIWPOWER)]	= (iw_handler) cfg80211_wext_giwpower,
	[IW_IOCTL_IDX(SIOCSIWGENIE)]	= (iw_handler) cfg80211_wext_siwgenie,
	[IW_IOCTL_IDX(SIOCSIWAUTH)]	= (iw_handler) cfg80211_wext_siwauth,
	[IW_IOCTL_IDX(SIOCGIWAUTH)]	= (iw_handler) cfg80211_wext_giwauth,
	[IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
1532
	[IW_IOCTL_IDX(SIOCSIWPMKSA)]	= (iw_handler) cfg80211_wext_siwpmksa,
1533 1534 1535 1536 1537 1538 1539
};

const struct iw_handler_def cfg80211_wext_handler = {
	.num_standard		= ARRAY_SIZE(cfg80211_handlers),
	.standard		= cfg80211_handlers,
	.get_wireless_stats = cfg80211_wireless_stats,
};