wext.c 29.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2002-2005, Instant802 Networks, Inc.
 * Copyright 2005-2006, Devicescape Software, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/types.h>
#include <linux/slab.h>
#include <linux/skbuff.h>
#include <linux/etherdevice.h>
#include <linux/if_arp.h>
#include <linux/wireless.h>
#include <net/iw_handler.h>
#include <asm/uaccess.h>

#include <net/mac80211.h>
#include "ieee80211_i.h"
J
Johannes Berg 已提交
24 25
#include "led.h"
#include "rate.h"
26 27 28
#include "wpa.h"
#include "aes_ccm.h"

J
Johannes Berg 已提交
29

30
static int ieee80211_set_encryption(struct ieee80211_sub_if_data *sdata, u8 *sta_addr,
J
Johannes Berg 已提交
31 32 33
				    int idx, int alg, int remove,
				    int set_tx_key, const u8 *_key,
				    size_t key_len)
34
{
35
	struct ieee80211_local *local = sdata->local;
36
	struct sta_info *sta;
J
Johannes Berg 已提交
37
	struct ieee80211_key *key;
38
	int err;
39

40 41 42 43 44 45 46 47
	if (alg == ALG_AES_CMAC) {
		if (idx < NUM_DEFAULT_KEYS ||
		    idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) {
			printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d "
			       "(BIP)\n", sdata->dev->name, idx);
			return -EINVAL;
		}
	} else if (idx < 0 || idx >= NUM_DEFAULT_KEYS) {
48
		printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
49
		       sdata->dev->name, idx);
50 51 52
		return -EINVAL;
	}

53
	if (remove) {
54 55 56 57
		rcu_read_lock();

		err = 0;

58 59 60 61
		if (is_broadcast_ether_addr(sta_addr)) {
			key = sdata->keys[idx];
		} else {
			sta = sta_info_get(local, sta_addr);
62 63 64 65
			if (!sta) {
				err = -ENOENT;
				goto out_unlock;
			}
66
			key = sta->key;
67 68
		}

69
		ieee80211_key_free(key);
70 71 72 73 74
	} else {
		key = ieee80211_key_alloc(alg, idx, key_len, _key);
		if (!key)
			return -ENOMEM;

75
		sta = NULL;
76 77 78
		err = 0;

		rcu_read_lock();
79

80 81 82 83 84 85 86 87 88
		if (!is_broadcast_ether_addr(sta_addr)) {
			set_tx_key = 0;
			/*
			 * According to the standard, the key index of a
			 * pairwise key must be zero. However, some AP are
			 * broken when it comes to WEP key indices, so we
			 * work around this.
			 */
			if (idx != 0 && alg != ALG_WEP) {
89
				ieee80211_key_free(key);
90 91
				err = -EINVAL;
				goto out_unlock;
92
			}
93

94 95
			sta = sta_info_get(local, sta_addr);
			if (!sta) {
96
				ieee80211_key_free(key);
97 98
				err = -ENOENT;
				goto out_unlock;
99
			}
100 101
		}

102 103 104 105 106 107 108
		if (alg == ALG_WEP &&
			key_len != LEN_WEP40 && key_len != LEN_WEP104) {
			ieee80211_key_free(key);
			err = -EINVAL;
			goto out_unlock;
		}

109
		ieee80211_key_link(key, sdata, sta);
110

111 112
		if (set_tx_key || (!sta && !sdata->default_key && key))
			ieee80211_set_default_key(sdata, idx);
113 114 115
		if (alg == ALG_AES_CMAC &&
		    (set_tx_key || (!sta && !sdata->default_mgmt_key && key)))
			ieee80211_set_default_mgmt_key(sdata, idx);
116
	}
117

118 119 120 121
 out_unlock:
	rcu_read_unlock();

	return err;
122 123 124 125 126 127 128 129
}

static int ieee80211_ioctl_siwgenie(struct net_device *dev,
				    struct iw_request_info *info,
				    struct iw_point *data, char *extra)
{
	struct ieee80211_sub_if_data *sdata;

130 131 132
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

	if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)
133 134
		return -EOPNOTSUPP;

135
	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
136
		int ret = ieee80211_sta_set_extra_ie(sdata, extra, data->length);
137 138
		if (ret)
			return ret;
139 140
		sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
		ieee80211_sta_req_auth(sdata);
141 142 143 144 145 146 147 148 149 150 151 152
		return 0;
	}

	return -EOPNOTSUPP;
}

static int ieee80211_ioctl_siwfreq(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_freq *freq, char *extra)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);

153 154 155 156
	if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
		sdata->u.ibss.flags &= ~IEEE80211_IBSS_AUTO_CHANNEL_SEL;
	else if (sdata->vif.type == NL80211_IFTYPE_STATION)
		sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
157 158 159 160

	/* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
	if (freq->e == 0) {
		if (freq->m < 0) {
161 162 163 164 165
			if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
				sdata->u.ibss.flags |=
					IEEE80211_IBSS_AUTO_CHANNEL_SEL;
			else if (sdata->vif.type == NL80211_IFTYPE_STATION)
				sdata->u.mgd.flags |=
166
					IEEE80211_STA_AUTO_CHANNEL_SEL;
167 168
			return 0;
		} else
169
			return ieee80211_set_freq(sdata,
170
				ieee80211_channel_to_frequency(freq->m));
171 172 173 174 175
	} else {
		int i, div = 1000000;
		for (i = 0; i < freq->e; i++)
			div /= 10;
		if (div > 0)
176
			return ieee80211_set_freq(sdata, freq->m / div);
177 178 179 180 181 182 183 184 185 186 187 188
		else
			return -EINVAL;
	}
}


static int ieee80211_ioctl_giwfreq(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_freq *freq, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

189
	freq->m = local->hw.conf.channel->center_freq;
190 191 192 193 194 195 196 197 198 199 200 201
	freq->e = 6;

	return 0;
}


static int ieee80211_ioctl_siwessid(struct net_device *dev,
				    struct iw_request_info *info,
				    struct iw_point *data, char *ssid)
{
	struct ieee80211_sub_if_data *sdata;
	size_t len = data->length;
202
	int ret;
203 204 205 206 207 208

	/* iwconfig uses nul termination in SSID.. */
	if (len > 0 && ssid[len - 1] == '\0')
		len--;

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
209
	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
210
		if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
211 212
			if (len > IEEE80211_MAX_SSID_LEN)
				return -EINVAL;
213 214
			memcpy(sdata->u.mgd.ssid, ssid, len);
			sdata->u.mgd.ssid_len = len;
215 216
			return 0;
		}
217

218
		if (data->flags)
219
			sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
220
		else
221 222
			sdata->u.mgd.flags |= IEEE80211_STA_AUTO_SSID_SEL;

223
		ret = ieee80211_sta_set_ssid(sdata, ssid, len);
224 225
		if (ret)
			return ret;
226 227

		ieee80211_sta_req_auth(sdata);
228
		return 0;
229 230
	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
		return ieee80211_ibss_set_ssid(sdata, ssid, len);
231 232 233 234 235 236 237 238 239 240 241 242 243

	return -EOPNOTSUPP;
}


static int ieee80211_ioctl_giwessid(struct net_device *dev,
				    struct iw_request_info *info,
				    struct iw_point *data, char *ssid)
{
	size_t len;

	struct ieee80211_sub_if_data *sdata;
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
244
	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
245
		int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
246 247 248 249 250 251
		if (res == 0) {
			data->length = len;
			data->flags = 1;
		} else
			data->flags = 0;
		return res;
252 253 254 255 256 257 258 259
	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
		int res = ieee80211_ibss_get_ssid(sdata, ssid, &len);
		if (res == 0) {
			data->length = len;
			data->flags = 1;
		} else
			data->flags = 0;
		return res;
260 261 262 263 264 265 266 267 268 269 270 271 272
	}

	return -EOPNOTSUPP;
}


static int ieee80211_ioctl_siwap(struct net_device *dev,
				 struct iw_request_info *info,
				 struct sockaddr *ap_addr, char *extra)
{
	struct ieee80211_sub_if_data *sdata;

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
273
	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
274
		int ret;
275
		if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
276
			memcpy(sdata->u.mgd.bssid, (u8 *) &ap_addr->sa_data,
277 278 279
			       ETH_ALEN);
			return 0;
		}
280
		if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
281
			sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
282 283
				IEEE80211_STA_AUTO_CHANNEL_SEL;
		else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
284
			sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
285
		else
286
			sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
287
		ret = ieee80211_sta_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
288 289
		if (ret)
			return ret;
290
		ieee80211_sta_req_auth(sdata);
291
		return 0;
292 293 294 295 296 297 298 299 300 301
	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
		if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
			sdata->u.ibss.flags |= IEEE80211_IBSS_AUTO_BSSID_SEL |
					       IEEE80211_IBSS_AUTO_CHANNEL_SEL;
		else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
			sdata->u.ibss.flags |= IEEE80211_IBSS_AUTO_BSSID_SEL;
		else
			sdata->u.ibss.flags &= ~IEEE80211_IBSS_AUTO_BSSID_SEL;

		return ieee80211_ibss_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
302
	} else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
303 304 305 306 307 308 309 310 311 312 313 314 315 316
		/*
		 * If it is necessary to update the WDS peer address
		 * while the interface is running, then we need to do
		 * more work here, namely if it is running we need to
		 * add a new and remove the old STA entry, this is
		 * normally handled by _open() and _stop().
		 */
		if (netif_running(dev))
			return -EBUSY;

		memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
		       ETH_ALEN);

		return 0;
317 318 319 320 321 322 323 324 325 326 327 328 329
	}

	return -EOPNOTSUPP;
}


static int ieee80211_ioctl_giwap(struct net_device *dev,
				 struct iw_request_info *info,
				 struct sockaddr *ap_addr, char *extra)
{
	struct ieee80211_sub_if_data *sdata;

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
330 331
	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
		if (sdata->u.mgd.state == IEEE80211_STA_MLME_ASSOCIATED) {
332
			ap_addr->sa_family = ARPHRD_ETHER;
333 334
			memcpy(&ap_addr->sa_data, sdata->u.mgd.bssid, ETH_ALEN);
		} else
335
			memset(&ap_addr->sa_data, 0, ETH_ALEN);
336 337 338 339 340 341 342 343
		return 0;
	} else if (sdata->vif.type == NL80211_IFTYPE_ADHOC) {
		if (sdata->u.ibss.state == IEEE80211_IBSS_MLME_JOINED) {
			ap_addr->sa_family = ARPHRD_ETHER;
			memcpy(&ap_addr->sa_data, sdata->u.ibss.bssid, ETH_ALEN);
		} else
			memset(&ap_addr->sa_data, 0, ETH_ALEN);
		return 0;
344
	} else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
345 346 347 348 349 350 351 352 353
		ap_addr->sa_family = ARPHRD_ETHER;
		memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
		return 0;
	}

	return -EOPNOTSUPP;
}


354 355 356 357 358
static int ieee80211_ioctl_siwrate(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_param *rate, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
359
	int i, err = -EINVAL;
360 361
	u32 target_rate = rate->value / 100000;
	struct ieee80211_sub_if_data *sdata;
362
	struct ieee80211_supported_band *sband;
363 364

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
365 366 367

	sband = local->hw.wiphy->bands[local->hw.conf.channel->band];

368 369 370
	/* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
	 * target_rate = X, rate->fixed = 1 means only rate X
	 * target_rate = X, rate->fixed = 0 means all rates <= X */
371 372
	sdata->max_ratectrl_rateidx = -1;
	sdata->force_unicast_rateidx = -1;
373 374
	if (rate->value < 0)
		return 0;
375 376 377 378

	for (i=0; i< sband->n_bitrates; i++) {
		struct ieee80211_rate *brate = &sband->bitrates[i];
		int this_rate = brate->bitrate;
379 380

		if (target_rate == this_rate) {
381
			sdata->max_ratectrl_rateidx = i;
382
			if (rate->fixed)
383
				sdata->force_unicast_rateidx = i;
384 385
			err = 0;
			break;
386 387
		}
	}
388
	return err;
389 390
}

391 392 393 394 395 396 397
static int ieee80211_ioctl_giwrate(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_param *rate, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct sta_info *sta;
	struct ieee80211_sub_if_data *sdata;
398
	struct ieee80211_supported_band *sband;
399 400

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
401

402
	if (sdata->vif.type != NL80211_IFTYPE_STATION)
403
		return -EOPNOTSUPP;
404 405 406

	sband = local->hw.wiphy->bands[local->hw.conf.channel->band];

407 408
	rcu_read_lock();

409
	sta = sta_info_get(local, sdata->u.mgd.bssid);
410

411 412
	if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
		rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
413 414
	else
		rate->value = 0;
415 416 417 418 419 420

	rcu_read_unlock();

	if (!sta)
		return -ENODEV;

421
	rate->value *= 100000;
422

423 424 425
	return 0;
}

426 427 428 429 430
static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
				      struct iw_request_info *info,
				      union iwreq_data *data, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
431
	struct ieee80211_channel* chan = local->hw.conf.channel;
432
	u32 reconf_flags = 0;
433
	int new_power_level;
434 435 436 437 438

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

442 443 444
	if (data->txpower.fixed)
		new_power_level = min(data->txpower.value, chan->max_power);
	else /* Automatic power level setting */
445
		new_power_level = chan->max_power;
446

447
	local->user_power_level = new_power_level;
448
	if (local->hw.conf.power_level != new_power_level)
449
		reconf_flags |= IEEE80211_CONF_CHANGE_POWER;
450

451 452
	if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
		local->hw.conf.radio_enabled = !(data->txpower.disabled);
453
		reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
I
Ivo van Doorn 已提交
454
		ieee80211_led_radio(local, local->hw.conf.radio_enabled);
455
	}
456

457 458
	if (reconf_flags)
		ieee80211_hw_config(local, reconf_flags);
459 460 461 462

	return 0;
}

463 464 465 466 467 468 469 470 471 472 473 474 475 476
static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
				   struct iw_request_info *info,
				   union iwreq_data *data, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	data->txpower.fixed = 1;
	data->txpower.disabled = !(local->hw.conf.radio_enabled);
	data->txpower.value = local->hw.conf.power_level;
	data->txpower.flags = IW_TXPOW_DBM;

	return 0;
}

477 478 479 480 481 482 483 484
static int ieee80211_ioctl_siwrts(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_param *rts, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	if (rts->disabled)
		local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
485 486 487
	else if (!rts->fixed)
		/* if the rts value is not fixed, then take default */
		local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
	else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
		return -EINVAL;
	else
		local->rts_threshold = rts->value;

	/* If the wlan card performs RTS/CTS in hardware/firmware,
	 * configure it here */

	if (local->ops->set_rts_threshold)
		local->ops->set_rts_threshold(local_to_hw(local),
					     local->rts_threshold);

	return 0;
}

static int ieee80211_ioctl_giwrts(struct net_device *dev,
				  struct iw_request_info *info,
				  struct iw_param *rts, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	rts->value = local->rts_threshold;
	rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
	rts->fixed = 1;

	return 0;
}


static int ieee80211_ioctl_siwfrag(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_param *frag, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	if (frag->disabled)
		local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
525 526
	else if (!frag->fixed)
		local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
	else if (frag->value < 256 ||
		 frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
		return -EINVAL;
	else {
		/* Fragment length must be even, so strip LSB. */
		local->fragmentation_threshold = frag->value & ~0x1;
	}

	return 0;
}

static int ieee80211_ioctl_giwfrag(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_param *frag, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	frag->value = local->fragmentation_threshold;
	frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
	frag->fixed = 1;

	return 0;
}


static int ieee80211_ioctl_siwretry(struct net_device *dev,
				    struct iw_request_info *info,
				    struct iw_param *retry, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

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

562 563 564 565 566 567 568
	if (retry->flags & IW_RETRY_MAX) {
		local->hw.conf.long_frame_max_tx_count = retry->value;
	} else if (retry->flags & IW_RETRY_MIN) {
		local->hw.conf.short_frame_max_tx_count = retry->value;
	} else {
		local->hw.conf.long_frame_max_tx_count = retry->value;
		local->hw.conf.short_frame_max_tx_count = retry->value;
569 570
	}

571
	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587

	return 0;
}


static int ieee80211_ioctl_giwretry(struct net_device *dev,
				    struct iw_request_info *info,
				    struct iw_param *retry, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

	retry->disabled = 0;
	if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
		/* first return min value, iwconfig will ask max value
		 * later if needed */
		retry->flags |= IW_RETRY_LIMIT;
588 589 590
		retry->value = local->hw.conf.short_frame_max_tx_count;
		if (local->hw.conf.long_frame_max_tx_count !=
		    local->hw.conf.short_frame_max_tx_count)
591 592 593 594 595
			retry->flags |= IW_RETRY_MIN;
		return 0;
	}
	if (retry->flags & IW_RETRY_MAX) {
		retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
596
		retry->value = local->hw.conf.long_frame_max_tx_count;
597 598 599 600 601 602 603 604 605 606 607 608 609
	}

	return 0;
}

static int ieee80211_ioctl_siwmlme(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_point *data, char *extra)
{
	struct ieee80211_sub_if_data *sdata;
	struct iw_mlme *mlme = (struct iw_mlme *) extra;

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
610
	if (!(sdata->vif.type == NL80211_IFTYPE_STATION))
611 612 613 614 615
		return -EINVAL;

	switch (mlme->cmd) {
	case IW_MLME_DEAUTH:
		/* TODO: mlme->addr.sa_data */
616
		return ieee80211_sta_deauthenticate(sdata, mlme->reason_code);
617 618
	case IW_MLME_DISASSOC:
		/* TODO: mlme->addr.sa_data */
619
		return ieee80211_sta_disassociate(sdata, mlme->reason_code);
620 621 622 623 624 625 626 627 628 629 630 631 632
	default:
		return -EOPNOTSUPP;
	}
}


static int ieee80211_ioctl_siwencode(struct net_device *dev,
				     struct iw_request_info *info,
				     struct iw_point *erq, char *keybuf)
{
	struct ieee80211_sub_if_data *sdata;
	int idx, i, alg = ALG_WEP;
	u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
J
Johannes Berg 已提交
633
	int remove = 0;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

	idx = erq->flags & IW_ENCODE_INDEX;
	if (idx == 0) {
		if (sdata->default_key)
			for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
				if (sdata->default_key == sdata->keys[i]) {
					idx = i;
					break;
				}
			}
	} else if (idx < 1 || idx > 4)
		return -EINVAL;
	else
		idx--;

	if (erq->flags & IW_ENCODE_DISABLED)
J
Johannes Berg 已提交
652
		remove = 1;
653 654
	else if (erq->length == 0) {
		/* No key data - just set the default TX key index */
J
Johannes Berg 已提交
655
		ieee80211_set_default_key(sdata, idx);
656 657 658 659
		return 0;
	}

	return ieee80211_set_encryption(
660
		sdata, bcaddr,
J
Johannes Berg 已提交
661
		idx, alg, remove,
662 663 664 665 666 667 668 669 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
		!sdata->default_key,
		keybuf, erq->length);
}


static int ieee80211_ioctl_giwencode(struct net_device *dev,
				     struct iw_request_info *info,
				     struct iw_point *erq, char *key)
{
	struct ieee80211_sub_if_data *sdata;
	int idx, i;

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

	idx = erq->flags & IW_ENCODE_INDEX;
	if (idx < 1 || idx > 4) {
		idx = -1;
		if (!sdata->default_key)
			idx = 0;
		else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
			if (sdata->default_key == sdata->keys[i]) {
				idx = i;
				break;
			}
		}
		if (idx < 0)
			return -EINVAL;
	} else
		idx--;

	erq->flags = idx + 1;

	if (!sdata->keys[idx]) {
		erq->length = 0;
		erq->flags |= IW_ENCODE_DISABLED;
		return 0;
	}

700
	memcpy(key, sdata->keys[idx]->conf.key,
J
Johannes Berg 已提交
701
	       min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
702
	erq->length = sdata->keys[idx]->conf.keylen;
703 704
	erq->flags |= IW_ENCODE_ENABLED;

705
	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
706
		switch (sdata->u.mgd.auth_alg) {
707 708 709 710 711 712 713 714 715 716
		case WLAN_AUTH_OPEN:
		case WLAN_AUTH_LEAP:
			erq->flags |= IW_ENCODE_OPEN;
			break;
		case WLAN_AUTH_SHARED_KEY:
			erq->flags |= IW_ENCODE_RESTRICTED;
			break;
		}
	}

717 718 719
	return 0;
}

720 721 722 723 724
static int ieee80211_ioctl_siwpower(struct net_device *dev,
				    struct iw_request_info *info,
				    struct iw_param *wrq,
				    char *extra)
{
725
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
726 727
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct ieee80211_conf *conf = &local->hw.conf;
728
	int ret = 0, timeout = 0;
729 730
	bool ps;

731 732 733
	if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
		return -EOPNOTSUPP;

734 735
	if (sdata->vif.type != NL80211_IFTYPE_STATION)
		return -EINVAL;
736 737

	if (wrq->disabled) {
738
		ps = false;
739
		timeout = 0;
740
		goto set;
741 742 743 744 745 746
	}

	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 */
747
		ps = true;
748
		break;
749
	default:                /* Otherwise we ignore */
750
		return -EINVAL;
751 752
	}

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

756 757
	if (wrq->flags & IW_POWER_TIMEOUT)
		timeout = wrq->value / 1000;
758

759
 set:
760
	if (ps == local->powersave && timeout == conf->dynamic_ps_timeout)
761 762
		return ret;

763
	local->powersave = ps;
764
	conf->dynamic_ps_timeout = timeout;
765

766
	if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
767 768
		ret = ieee80211_hw_config(local,
					  IEEE80211_CONF_CHANGE_DYNPS_TIMEOUT);
769

770
	if (!(sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED))
771 772 773 774 775 776 777 778 779
		return ret;

	if (conf->dynamic_ps_timeout > 0 &&
	    !(local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)) {
		mod_timer(&local->dynamic_ps_timer, jiffies +
			  msecs_to_jiffies(conf->dynamic_ps_timeout));
	} else {
		if (local->powersave) {
			if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
780
				ieee80211_send_nullfunc(local, sdata, 1);
781 782 783 784 785 786 787 788
			conf->flags |= IEEE80211_CONF_PS;
			ret = ieee80211_hw_config(local,
					IEEE80211_CONF_CHANGE_PS);
		} else {
			conf->flags &= ~IEEE80211_CONF_PS;
			ret = ieee80211_hw_config(local,
					IEEE80211_CONF_CHANGE_PS);
			if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
789
				ieee80211_send_nullfunc(local, sdata, 0);
790 791
			del_timer_sync(&local->dynamic_ps_timer);
			cancel_work_sync(&local->dynamic_ps_enable_work);
792
		}
793 794 795
	}

	return ret;
796 797 798 799 800 801 802 803 804
}

static int ieee80211_ioctl_giwpower(struct net_device *dev,
				    struct iw_request_info *info,
				    union iwreq_data *wrqu,
				    char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);

805
	wrqu->power.disabled = !local->powersave;
806 807 808 809

	return 0;
}

810 811 812 813 814 815 816 817 818 819 820 821 822
static int ieee80211_ioctl_siwauth(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_param *data, char *extra)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int ret = 0;

	switch (data->flags & IW_AUTH_INDEX) {
	case IW_AUTH_WPA_VERSION:
	case IW_AUTH_CIPHER_GROUP:
	case IW_AUTH_WPA_ENABLED:
	case IW_AUTH_RX_UNENCRYPTED_EAPOL:
	case IW_AUTH_KEY_MGMT:
823
	case IW_AUTH_CIPHER_GROUP_MGMT:
824
		break;
825 826 827 828
	case IW_AUTH_CIPHER_PAIRWISE:
		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
			if (data->value & (IW_AUTH_CIPHER_WEP40 |
			    IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_TKIP))
829
				sdata->u.mgd.flags |=
830 831
					IEEE80211_STA_TKIP_WEP_USED;
			else
832
				sdata->u.mgd.flags &=
833 834 835
					~IEEE80211_STA_TKIP_WEP_USED;
		}
		break;
836 837 838
	case IW_AUTH_DROP_UNENCRYPTED:
		sdata->drop_unencrypted = !!data->value;
		break;
839
	case IW_AUTH_PRIVACY_INVOKED:
840
		if (sdata->vif.type != NL80211_IFTYPE_STATION)
841 842
			ret = -EINVAL;
		else {
843
			sdata->u.mgd.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
844
			/*
845 846 847
			 * Privacy invoked by wpa_supplicant, store the
			 * value and allow associating to a protected
			 * network without having a key up front.
848
			 */
849
			if (data->value)
850
				sdata->u.mgd.flags |=
851
					IEEE80211_STA_PRIVACY_INVOKED;
852 853 854
		}
		break;
	case IW_AUTH_80211_AUTH_ALG:
855 856
		if (sdata->vif.type == NL80211_IFTYPE_STATION)
			sdata->u.mgd.auth_algs = data->value;
857 858 859
		else
			ret = -EOPNOTSUPP;
		break;
860
	case IW_AUTH_MFP:
861 862 863 864
		if (!(sdata->local->hw.flags & IEEE80211_HW_MFP_CAPABLE)) {
			ret = -EOPNOTSUPP;
			break;
		}
865
		if (sdata->vif.type == NL80211_IFTYPE_STATION) {
866 867
			switch (data->value) {
			case IW_AUTH_MFP_DISABLED:
868
				sdata->u.mgd.mfp = IEEE80211_MFP_DISABLED;
869 870
				break;
			case IW_AUTH_MFP_OPTIONAL:
871
				sdata->u.mgd.mfp = IEEE80211_MFP_OPTIONAL;
872 873
				break;
			case IW_AUTH_MFP_REQUIRED:
874
				sdata->u.mgd.mfp = IEEE80211_MFP_REQUIRED;
875 876 877 878 879
				break;
			default:
				ret = -EINVAL;
			}
		} else
880 881
			ret = -EOPNOTSUPP;
		break;
882 883 884 885 886 887 888
	default:
		ret = -EOPNOTSUPP;
		break;
	}
	return ret;
}

889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
static u8 ieee80211_get_wstats_flags(struct ieee80211_local *local)
{
	u8 wstats_flags = 0;

	wstats_flags |= local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
					   IEEE80211_HW_SIGNAL_DBM) ?
				IW_QUAL_QUAL_UPDATED : IW_QUAL_QUAL_INVALID;
	wstats_flags |= local->hw.flags & IEEE80211_HW_NOISE_DBM ?
				IW_QUAL_NOISE_UPDATED : IW_QUAL_NOISE_INVALID;
	if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
		wstats_flags |= IW_QUAL_DBM;

	return wstats_flags;
}

904 905 906 907 908 909 910 911
/* Get wireless statistics.  Called by /proc/net/wireless and by SIOCGIWSTATS */
static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct iw_statistics *wstats = &local->wstats;
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct sta_info *sta = NULL;

J
Johannes Berg 已提交
912 913
	rcu_read_lock();

914 915 916
	if (sdata->vif.type == NL80211_IFTYPE_STATION)
		sta = sta_info_get(local, sdata->u.mgd.bssid);

917 918 919 920 921 922 923 924
	if (!sta) {
		wstats->discard.fragment = 0;
		wstats->discard.misc = 0;
		wstats->qual.qual = 0;
		wstats->qual.level = 0;
		wstats->qual.noise = 0;
		wstats->qual.updated = IW_QUAL_ALL_INVALID;
	} else {
925 926
		wstats->qual.level = sta->last_signal;
		wstats->qual.qual = sta->last_qual;
927
		wstats->qual.noise = sta->last_noise;
928
		wstats->qual.updated = ieee80211_get_wstats_flags(local);
929
	}
J
Johannes Berg 已提交
930 931 932

	rcu_read_unlock();

933 934 935 936 937 938 939 940 941 942 943 944
	return wstats;
}

static int ieee80211_ioctl_giwauth(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_param *data, char *extra)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	int ret = 0;

	switch (data->flags & IW_AUTH_INDEX) {
	case IW_AUTH_80211_AUTH_ALG:
945 946
		if (sdata->vif.type == NL80211_IFTYPE_STATION)
			data->value = sdata->u.mgd.auth_algs;
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
		else
			ret = -EOPNOTSUPP;
		break;
	default:
		ret = -EOPNOTSUPP;
		break;
	}
	return ret;
}


static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
					struct iw_request_info *info,
					struct iw_point *erq, char *extra)
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
	struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
J
Johannes Berg 已提交
964
	int uninitialized_var(alg), idx, i, remove = 0;
965 966 967

	switch (ext->alg) {
	case IW_ENCODE_ALG_NONE:
J
Johannes Berg 已提交
968
		remove = 1;
969 970 971 972 973 974 975 976 977 978
		break;
	case IW_ENCODE_ALG_WEP:
		alg = ALG_WEP;
		break;
	case IW_ENCODE_ALG_TKIP:
		alg = ALG_TKIP;
		break;
	case IW_ENCODE_ALG_CCMP:
		alg = ALG_CCMP;
		break;
979 980 981
	case IW_ENCODE_ALG_AES_CMAC:
		alg = ALG_AES_CMAC;
		break;
982 983 984 985 986
	default:
		return -EOPNOTSUPP;
	}

	if (erq->flags & IW_ENCODE_DISABLED)
J
Johannes Berg 已提交
987
		remove = 1;
988 989

	idx = erq->flags & IW_ENCODE_INDEX;
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	if (alg == ALG_AES_CMAC) {
		if (idx < NUM_DEFAULT_KEYS + 1 ||
		    idx > NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) {
			idx = -1;
			if (!sdata->default_mgmt_key)
				idx = 0;
			else for (i = NUM_DEFAULT_KEYS;
				  i < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
				  i++) {
				if (sdata->default_mgmt_key == sdata->keys[i])
				{
					idx = i;
					break;
				}
1004
			}
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
			if (idx < 0)
				return -EINVAL;
		} else
			idx--;
	} else {
		if (idx < 1 || idx > 4) {
			idx = -1;
			if (!sdata->default_key)
				idx = 0;
			else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
				if (sdata->default_key == sdata->keys[i]) {
					idx = i;
					break;
				}
			}
			if (idx < 0)
				return -EINVAL;
		} else
			idx--;
	}
1025

1026
	return ieee80211_set_encryption(sdata, ext->addr.sa_data, idx, alg,
J
Johannes Berg 已提交
1027
					remove,
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
					ext->ext_flags &
					IW_ENCODE_EXT_SET_TX_KEY,
					ext->key, ext->key_len);
}


/* Structures to export the Wireless Handlers */

static const iw_handler ieee80211_handler[] =
{
	(iw_handler) NULL,				/* SIOCSIWCOMMIT */
J
Johannes Berg 已提交
1039
	(iw_handler) cfg80211_wext_giwname,		/* SIOCGIWNAME */
1040 1041 1042 1043
	(iw_handler) NULL,				/* SIOCSIWNWID */
	(iw_handler) NULL,				/* SIOCGIWNWID */
	(iw_handler) ieee80211_ioctl_siwfreq,		/* SIOCSIWFREQ */
	(iw_handler) ieee80211_ioctl_giwfreq,		/* SIOCGIWFREQ */
1044 1045
	(iw_handler) cfg80211_wext_siwmode,		/* SIOCSIWMODE */
	(iw_handler) cfg80211_wext_giwmode,		/* SIOCGIWMODE */
1046 1047 1048
	(iw_handler) NULL,				/* SIOCSIWSENS */
	(iw_handler) NULL,				/* SIOCGIWSENS */
	(iw_handler) NULL /* not used */,		/* SIOCSIWRANGE */
1049
	(iw_handler) cfg80211_wext_giwrange,		/* SIOCGIWRANGE */
1050 1051 1052 1053
	(iw_handler) NULL /* not used */,		/* SIOCSIWPRIV */
	(iw_handler) NULL /* kernel code */,		/* SIOCGIWPRIV */
	(iw_handler) NULL /* not used */,		/* SIOCSIWSTATS */
	(iw_handler) NULL /* kernel code */,		/* SIOCGIWSTATS */
J
Johannes Berg 已提交
1054 1055 1056 1057
	(iw_handler) NULL,				/* SIOCSIWSPY */
	(iw_handler) NULL,				/* SIOCGIWSPY */
	(iw_handler) NULL,				/* SIOCSIWTHRSPY */
	(iw_handler) NULL,				/* SIOCGIWTHRSPY */
1058 1059 1060 1061
	(iw_handler) ieee80211_ioctl_siwap,		/* SIOCSIWAP */
	(iw_handler) ieee80211_ioctl_giwap,		/* SIOCGIWAP */
	(iw_handler) ieee80211_ioctl_siwmlme,		/* SIOCSIWMLME */
	(iw_handler) NULL,				/* SIOCGIWAPLIST */
1062 1063
	(iw_handler) cfg80211_wext_siwscan,		/* SIOCSIWSCAN */
	(iw_handler) cfg80211_wext_giwscan,		/* SIOCGIWSCAN */
1064 1065 1066 1067 1068 1069
	(iw_handler) ieee80211_ioctl_siwessid,		/* SIOCSIWESSID */
	(iw_handler) ieee80211_ioctl_giwessid,		/* SIOCGIWESSID */
	(iw_handler) NULL,				/* SIOCSIWNICKN */
	(iw_handler) NULL,				/* SIOCGIWNICKN */
	(iw_handler) NULL,				/* -- hole -- */
	(iw_handler) NULL,				/* -- hole -- */
1070
	(iw_handler) ieee80211_ioctl_siwrate,		/* SIOCSIWRATE */
1071
	(iw_handler) ieee80211_ioctl_giwrate,		/* SIOCGIWRATE */
1072 1073 1074 1075
	(iw_handler) ieee80211_ioctl_siwrts,		/* SIOCSIWRTS */
	(iw_handler) ieee80211_ioctl_giwrts,		/* SIOCGIWRTS */
	(iw_handler) ieee80211_ioctl_siwfrag,		/* SIOCSIWFRAG */
	(iw_handler) ieee80211_ioctl_giwfrag,		/* SIOCGIWFRAG */
1076
	(iw_handler) ieee80211_ioctl_siwtxpower,	/* SIOCSIWTXPOW */
1077
	(iw_handler) ieee80211_ioctl_giwtxpower,	/* SIOCGIWTXPOW */
1078 1079 1080 1081
	(iw_handler) ieee80211_ioctl_siwretry,		/* SIOCSIWRETRY */
	(iw_handler) ieee80211_ioctl_giwretry,		/* SIOCGIWRETRY */
	(iw_handler) ieee80211_ioctl_siwencode,		/* SIOCSIWENCODE */
	(iw_handler) ieee80211_ioctl_giwencode,		/* SIOCGIWENCODE */
1082 1083
	(iw_handler) ieee80211_ioctl_siwpower,		/* SIOCSIWPOWER */
	(iw_handler) ieee80211_ioctl_giwpower,		/* SIOCGIWPOWER */
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	(iw_handler) NULL,				/* -- hole -- */
	(iw_handler) NULL,				/* -- hole -- */
	(iw_handler) ieee80211_ioctl_siwgenie,		/* SIOCSIWGENIE */
	(iw_handler) NULL,				/* SIOCGIWGENIE */
	(iw_handler) ieee80211_ioctl_siwauth,		/* SIOCSIWAUTH */
	(iw_handler) ieee80211_ioctl_giwauth,		/* SIOCGIWAUTH */
	(iw_handler) ieee80211_ioctl_siwencodeext,	/* SIOCSIWENCODEEXT */
	(iw_handler) NULL,				/* SIOCGIWENCODEEXT */
	(iw_handler) NULL,				/* SIOCSIWPMKSA */
	(iw_handler) NULL,				/* -- hole -- */
};

const struct iw_handler_def ieee80211_iw_handler_def =
{
	.num_standard	= ARRAY_SIZE(ieee80211_handler),
	.standard	= (iw_handler *) ieee80211_handler,
	.get_wireless_stats = ieee80211_get_wireless_stats,
};