wext.c 30.4 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
	if (idx < 0 || idx >= NUM_DEFAULT_KEYS) {
		printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
42
		       sdata->dev->name, idx);
43 44 45
		return -EINVAL;
	}

46
	if (remove) {
47 48 49 50
		rcu_read_lock();

		err = 0;

51 52 53 54
		if (is_broadcast_ether_addr(sta_addr)) {
			key = sdata->keys[idx];
		} else {
			sta = sta_info_get(local, sta_addr);
55 56 57 58
			if (!sta) {
				err = -ENOENT;
				goto out_unlock;
			}
59
			key = sta->key;
60 61
		}

62
		ieee80211_key_free(key);
63 64 65 66 67
	} else {
		key = ieee80211_key_alloc(alg, idx, key_len, _key);
		if (!key)
			return -ENOMEM;

68
		sta = NULL;
69 70 71
		err = 0;

		rcu_read_lock();
72

73 74 75 76 77 78 79 80 81
		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) {
82
				ieee80211_key_free(key);
83 84
				err = -EINVAL;
				goto out_unlock;
85
			}
86

87 88
			sta = sta_info_get(local, sta_addr);
			if (!sta) {
89
				ieee80211_key_free(key);
90 91
				err = -ENOENT;
				goto out_unlock;
92
			}
93 94
		}

95 96 97 98 99 100 101
		if (alg == ALG_WEP &&
			key_len != LEN_WEP40 && key_len != LEN_WEP104) {
			ieee80211_key_free(key);
			err = -EINVAL;
			goto out_unlock;
		}

102
		ieee80211_key_link(key, sdata, sta);
103

104 105 106
		if (set_tx_key || (!sta && !sdata->default_key && key))
			ieee80211_set_default_key(sdata, idx);
	}
107

108 109 110 111
 out_unlock:
	rcu_read_unlock();

	return err;
112 113 114 115 116 117 118 119
}

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;

120 121 122
	sdata = IEEE80211_DEV_TO_SUB_IF(dev);

	if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)
123 124
		return -EOPNOTSUPP;

125 126
	if (sdata->vif.type == NL80211_IFTYPE_STATION ||
	    sdata->vif.type == NL80211_IFTYPE_ADHOC) {
127
		int ret = ieee80211_sta_set_extra_ie(sdata, extra, data->length);
128 129
		if (ret)
			return ret;
130
		sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
131
		ieee80211_sta_req_auth(sdata, &sdata->u.sta);
132 133 134 135 136 137 138 139 140 141 142 143
		return 0;
	}

	return -EOPNOTSUPP;
}

static int ieee80211_ioctl_giwrange(struct net_device *dev,
				 struct iw_request_info *info,
				 struct iw_point *data, char *extra)
{
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct iw_range *range = (struct iw_range *) extra;
144
	enum ieee80211_band band;
145
	int c = 0;
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	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->encoding_size[0] = 5;
	range->encoding_size[1] = 13;
	range->num_encoding_sizes = 2;
	range->max_encoding_tokens = NUM_DEFAULT_KEYS;

166 167 168 169 170 171 172 173 174 175 176 177 178 179
	if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC ||
	    local->hw.flags & IEEE80211_HW_SIGNAL_DB)
		range->max_qual.level = local->hw.max_signal;
	else if  (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
		range->max_qual.level = -110;
	else
		range->max_qual.level = 0;

	if (local->hw.flags & IEEE80211_HW_NOISE_DBM)
		range->max_qual.noise = -110;
	else
		range->max_qual.noise = 0;

	range->max_qual.qual = 100;
180 181
	range->max_qual.updated = local->wstats_flags;

182 183 184 185
	range->avg_qual.qual = 50;
	/* not always true but better than nothing */
	range->avg_qual.level = range->max_qual.level / 2;
	range->avg_qual.noise = range->max_qual.noise / 2;
186 187 188 189 190
	range->avg_qual.updated = local->wstats_flags;

	range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
			  IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;

191

192 193 194 195 196 197 198
	for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
		int i;
		struct ieee80211_supported_band *sband;

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

		if (!sband)
199 200
			continue;

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

204 205 206 207 208 209
			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;
210 211 212 213 214 215 216
				c++;
			}
		}
	}
	range->num_channels = c;
	range->num_frequency = c;

217 218 219 220
	IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
	IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);

D
Dan Williams 已提交
221 222
	range->scan_capa |= IW_SCAN_CAPA_ESSID;

223 224 225 226 227 228 229 230 231 232
	return 0;
}


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);

233 234
	if (sdata->vif.type == NL80211_IFTYPE_ADHOC ||
	    sdata->vif.type == NL80211_IFTYPE_STATION)
235
		sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
236 237 238 239

	/* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
	if (freq->e == 0) {
		if (freq->m < 0) {
240 241
			if (sdata->vif.type == NL80211_IFTYPE_ADHOC ||
			    sdata->vif.type == NL80211_IFTYPE_STATION)
242 243
				sdata->u.sta.flags |=
					IEEE80211_STA_AUTO_CHANNEL_SEL;
244 245
			return 0;
		} else
246
			return ieee80211_set_freq(sdata,
247
				ieee80211_channel_to_frequency(freq->m));
248 249 250 251 252
	} else {
		int i, div = 1000000;
		for (i = 0; i < freq->e; i++)
			div /= 10;
		if (div > 0)
253
			return ieee80211_set_freq(sdata, freq->m / div);
254 255 256 257 258 259 260 261 262 263 264 265
		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);

266
	freq->m = local->hw.conf.channel->center_freq;
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	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;

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

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
285 286
	if (sdata->vif.type == NL80211_IFTYPE_STATION ||
	    sdata->vif.type == NL80211_IFTYPE_ADHOC) {
287
		int ret;
288
		if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
289 290 291 292 293 294
			if (len > IEEE80211_MAX_SSID_LEN)
				return -EINVAL;
			memcpy(sdata->u.sta.ssid, ssid, len);
			sdata->u.sta.ssid_len = len;
			return 0;
		}
295 296 297 298
		if (data->flags)
			sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
		else
			sdata->u.sta.flags |= IEEE80211_STA_AUTO_SSID_SEL;
299
		ret = ieee80211_sta_set_ssid(sdata, ssid, len);
300 301
		if (ret)
			return ret;
302
		ieee80211_sta_req_auth(sdata, &sdata->u.sta);
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
		return 0;
	}

	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);
318 319
	if (sdata->vif.type == NL80211_IFTYPE_STATION ||
	    sdata->vif.type == NL80211_IFTYPE_ADHOC) {
320
		int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
		if (res == 0) {
			data->length = len;
			data->flags = 1;
		} else
			data->flags = 0;
		return res;
	}

	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);
340 341
	if (sdata->vif.type == NL80211_IFTYPE_STATION ||
	    sdata->vif.type == NL80211_IFTYPE_ADHOC) {
342
		int ret;
343
		if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
344 345 346 347
			memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
			       ETH_ALEN);
			return 0;
		}
348 349 350 351 352
		if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
			sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
				IEEE80211_STA_AUTO_CHANNEL_SEL;
		else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
			sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
353
		else
354
			sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
355
		ret = ieee80211_sta_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
356 357
		if (ret)
			return ret;
358
		ieee80211_sta_req_auth(sdata, &sdata->u.sta);
359
		return 0;
360
	} else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
361 362 363 364 365 366 367 368 369 370 371 372 373 374
		/*
		 * 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;
375 376 377 378 379 380 381 382 383 384 385 386 387
	}

	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);
388 389
	if (sdata->vif.type == NL80211_IFTYPE_STATION ||
	    sdata->vif.type == NL80211_IFTYPE_ADHOC) {
390 391
		if (sdata->u.sta.state == IEEE80211_STA_MLME_ASSOCIATED ||
		    sdata->u.sta.state == IEEE80211_STA_MLME_IBSS_JOINED) {
392 393 394 395 396 397 398
			ap_addr->sa_family = ARPHRD_ETHER;
			memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
			return 0;
		} else {
			memset(&ap_addr->sa_data, 0, ETH_ALEN);
			return 0;
		}
399
	} else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
400 401 402 403 404 405 406 407 408 409 410
		ap_addr->sa_family = ARPHRD_ETHER;
		memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
		return 0;
	}

	return -EOPNOTSUPP;
}


static int ieee80211_ioctl_siwscan(struct net_device *dev,
				   struct iw_request_info *info,
411
				   union iwreq_data *wrqu, char *extra)
412 413
{
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
414
	struct iw_scan_req *req = NULL;
415 416 417 418 419 420
	u8 *ssid = NULL;
	size_t ssid_len = 0;

	if (!netif_running(dev))
		return -ENETDOWN;

421 422
	if (sdata->vif.type != NL80211_IFTYPE_STATION &&
	    sdata->vif.type != NL80211_IFTYPE_ADHOC &&
423
	    sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
424 425 426
		return -EOPNOTSUPP;

	/* if SSID was specified explicitly then use that */
427 428 429 430 431
	if (wrqu->data.length == sizeof(struct iw_scan_req) &&
	    wrqu->data.flags & IW_SCAN_THIS_ESSID) {
		req = (struct iw_scan_req *)extra;
		ssid = req->essid;
		ssid_len = req->essid_len;
432
	}
433

434
	return ieee80211_request_scan(sdata, ssid, ssid_len);
435 436 437 438 439 440 441 442 443
}


static int ieee80211_ioctl_giwscan(struct net_device *dev,
				   struct iw_request_info *info,
				   struct iw_point *data, char *extra)
{
	int res;
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
444 445 446
	struct ieee80211_sub_if_data *sdata;

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
Z
Zhu Yi 已提交
447

448
	if (local->sw_scanning || local->hw_scanning)
449
		return -EAGAIN;
Z
Zhu Yi 已提交
450

451
	res = ieee80211_scan_results(local, info, extra, data->length);
452 453 454 455 456 457 458 459 460
	if (res >= 0) {
		data->length = res;
		return 0;
	}
	data->length = 0;
	return res;
}


461 462 463 464 465
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);
466
	int i, err = -EINVAL;
467 468
	u32 target_rate = rate->value / 100000;
	struct ieee80211_sub_if_data *sdata;
469
	struct ieee80211_supported_band *sband;
470 471

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
472 473 474

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

475 476 477
	/* 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 */
478 479
	sdata->max_ratectrl_rateidx = -1;
	sdata->force_unicast_rateidx = -1;
480 481
	if (rate->value < 0)
		return 0;
482 483 484 485

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

		if (target_rate == this_rate) {
488
			sdata->max_ratectrl_rateidx = i;
489
			if (rate->fixed)
490
				sdata->force_unicast_rateidx = i;
491 492
			err = 0;
			break;
493 494
		}
	}
495
	return err;
496 497
}

498 499 500 501 502 503 504
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;
505
	struct ieee80211_supported_band *sband;
506 507

	sdata = IEEE80211_DEV_TO_SUB_IF(dev);
508

509
	if (sdata->vif.type != NL80211_IFTYPE_STATION)
510
		return -EOPNOTSUPP;
511 512 513

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

514 515 516 517
	rcu_read_lock();

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

518 519
	if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
		rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
520 521
	else
		rate->value = 0;
522 523 524 525 526 527

	rcu_read_unlock();

	if (!sta)
		return -ENODEV;

528
	rate->value *= 100000;
529

530 531 532
	return 0;
}

533 534 535 536 537
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);
538
	struct ieee80211_channel* chan = local->hw.conf.channel;
539
	u32 reconf_flags = 0;
540
	int new_power_level;
541 542 543 544 545

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

549 550 551
	if (data->txpower.fixed)
		new_power_level = min(data->txpower.value, chan->max_power);
	else /* Automatic power level setting */
552
		new_power_level = chan->max_power;
553

554
	local->user_power_level = new_power_level;
555
	if (local->hw.conf.power_level != new_power_level)
556
		reconf_flags |= IEEE80211_CONF_CHANGE_POWER;
557

558 559
	if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
		local->hw.conf.radio_enabled = !(data->txpower.disabled);
560
		reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
I
Ivo van Doorn 已提交
561
		ieee80211_led_radio(local, local->hw.conf.radio_enabled);
562
	}
563

564 565
	if (reconf_flags)
		ieee80211_hw_config(local, reconf_flags);
566 567 568 569

	return 0;
}

570 571 572 573 574 575 576 577 578 579 580 581 582 583
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;
}

584 585 586 587 588 589 590 591
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;
592 593 594
	else if (!rts->fixed)
		/* if the rts value is not fixed, then take default */
		local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
	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;
632 633
	else if (!frag->fixed)
		local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	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;

669 670 671 672 673 674 675
	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;
676 677
	}

678
	ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

	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;
695 696 697
		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)
698 699 700 701 702
			retry->flags |= IW_RETRY_MIN;
		return 0;
	}
	if (retry->flags & IW_RETRY_MAX) {
		retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
703
		retry->value = local->hw.conf.long_frame_max_tx_count;
704 705 706 707 708 709 710 711 712 713 714 715 716
	}

	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);
717 718
	if (sdata->vif.type != NL80211_IFTYPE_STATION &&
	    sdata->vif.type != NL80211_IFTYPE_ADHOC)
719 720 721 722 723
		return -EINVAL;

	switch (mlme->cmd) {
	case IW_MLME_DEAUTH:
		/* TODO: mlme->addr.sa_data */
724
		return ieee80211_sta_deauthenticate(sdata, mlme->reason_code);
725 726
	case IW_MLME_DISASSOC:
		/* TODO: mlme->addr.sa_data */
727
		return ieee80211_sta_disassociate(sdata, mlme->reason_code);
728 729 730 731 732 733 734 735 736 737 738 739 740
	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 已提交
741
	int remove = 0;
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759

	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 已提交
760
		remove = 1;
761 762
	else if (erq->length == 0) {
		/* No key data - just set the default TX key index */
J
Johannes Berg 已提交
763
		ieee80211_set_default_key(sdata, idx);
764 765 766 767
		return 0;
	}

	return ieee80211_set_encryption(
768
		sdata, bcaddr,
J
Johannes Berg 已提交
769
		idx, alg, remove,
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
		!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;
	}

808
	memcpy(key, sdata->keys[idx]->conf.key,
J
Johannes Berg 已提交
809
	       min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
810
	erq->length = sdata->keys[idx]->conf.keylen;
811 812
	erq->flags |= IW_ENCODE_ENABLED;

813
	if (sdata->vif.type == NL80211_IFTYPE_STATION) {
814 815 816 817 818 819 820 821 822 823 824 825
		struct ieee80211_if_sta *ifsta = &sdata->u.sta;
		switch (ifsta->auth_alg) {
		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;
		}
	}

826 827 828
	return 0;
}

829 830 831 832 833
static int ieee80211_ioctl_siwpower(struct net_device *dev,
				    struct iw_request_info *info,
				    struct iw_param *wrq,
				    char *extra)
{
834
	struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
835 836
	struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
	struct ieee80211_conf *conf = &local->hw.conf;
837
	int ret = 0, timeout = 0;
838 839
	bool ps;

840 841 842
	if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
		return -EOPNOTSUPP;

843 844
	if (sdata->vif.type != NL80211_IFTYPE_STATION)
		return -EINVAL;
845 846

	if (wrq->disabled) {
847
		ps = false;
848
		timeout = 0;
849
		goto set;
850 851 852 853 854 855
	}

	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 */
856
		ps = true;
857
		break;
858
	default:                /* Otherwise we ignore */
859
		return -EINVAL;
860 861
	}

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

865 866
	if (wrq->flags & IW_POWER_TIMEOUT)
		timeout = wrq->value / 1000;
867

868
 set:
869
	if (ps == local->powersave && timeout == conf->dynamic_ps_timeout)
870 871
		return ret;

872
	local->powersave = ps;
873
	conf->dynamic_ps_timeout = timeout;
874

875
	if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
876 877
		ret = ieee80211_hw_config(local,
					  IEEE80211_CONF_CHANGE_DYNPS_TIMEOUT);
878 879 880 881 882 883 884 885 886 887 888

	if (!(sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED))
		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)
889
				ieee80211_send_nullfunc(local, sdata, 1);
890 891 892 893 894 895 896 897
			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)
898
				ieee80211_send_nullfunc(local, sdata, 0);
899
		}
900 901 902
	}

	return ret;
903 904 905 906 907 908 909 910 911
}

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);

912
	wrqu->power.disabled = !local->powersave;
913 914 915 916

	return 0;
}

917 918 919 920 921 922 923 924 925 926 927 928 929
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:
930
	case IW_AUTH_CIPHER_GROUP_MGMT:
931
		break;
932 933 934 935 936 937 938 939 940 941 942
	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))
				sdata->u.sta.flags |=
					IEEE80211_STA_TKIP_WEP_USED;
			else
				sdata->u.sta.flags &=
					~IEEE80211_STA_TKIP_WEP_USED;
		}
		break;
943 944 945
	case IW_AUTH_DROP_UNENCRYPTED:
		sdata->drop_unencrypted = !!data->value;
		break;
946
	case IW_AUTH_PRIVACY_INVOKED:
947
		if (sdata->vif.type != NL80211_IFTYPE_STATION)
948 949
			ret = -EINVAL;
		else {
950
			sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
951
			/*
952 953 954
			 * Privacy invoked by wpa_supplicant, store the
			 * value and allow associating to a protected
			 * network without having a key up front.
955
			 */
956 957 958
			if (data->value)
				sdata->u.sta.flags |=
					IEEE80211_STA_PRIVACY_INVOKED;
959 960 961
		}
		break;
	case IW_AUTH_80211_AUTH_ALG:
962 963
		if (sdata->vif.type == NL80211_IFTYPE_STATION ||
		    sdata->vif.type == NL80211_IFTYPE_ADHOC)
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
			sdata->u.sta.auth_algs = data->value;
		else
			ret = -EOPNOTSUPP;
		break;
	default:
		ret = -EOPNOTSUPP;
		break;
	}
	return ret;
}

/* 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 已提交
983 984
	rcu_read_lock();

985 986
	if (sdata->vif.type == NL80211_IFTYPE_STATION ||
	    sdata->vif.type == NL80211_IFTYPE_ADHOC)
987 988 989 990 991 992 993 994 995
		sta = sta_info_get(local, sdata->u.sta.bssid);
	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 {
996 997
		wstats->qual.level = sta->last_signal;
		wstats->qual.qual = sta->last_qual;
998 999 1000
		wstats->qual.noise = sta->last_noise;
		wstats->qual.updated = local->wstats_flags;
	}
J
Johannes Berg 已提交
1001 1002 1003

	rcu_read_unlock();

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	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:
1016 1017
		if (sdata->vif.type == NL80211_IFTYPE_STATION ||
		    sdata->vif.type == NL80211_IFTYPE_ADHOC)
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
			data->value = sdata->u.sta.auth_algs;
		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 已提交
1036
	int uninitialized_var(alg), idx, i, remove = 0;
1037 1038 1039

	switch (ext->alg) {
	case IW_ENCODE_ALG_NONE:
J
Johannes Berg 已提交
1040
		remove = 1;
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
		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;
	default:
		return -EOPNOTSUPP;
	}

	if (erq->flags & IW_ENCODE_DISABLED)
J
Johannes Berg 已提交
1056
		remove = 1;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073

	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--;

1074
	return ieee80211_set_encryption(sdata, ext->addr.sa_data, idx, alg,
J
Johannes Berg 已提交
1075
					remove,
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
					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 已提交
1087
	(iw_handler) cfg80211_wext_giwname,		/* SIOCGIWNAME */
1088 1089 1090 1091
	(iw_handler) NULL,				/* SIOCSIWNWID */
	(iw_handler) NULL,				/* SIOCGIWNWID */
	(iw_handler) ieee80211_ioctl_siwfreq,		/* SIOCSIWFREQ */
	(iw_handler) ieee80211_ioctl_giwfreq,		/* SIOCGIWFREQ */
1092 1093
	(iw_handler) cfg80211_wext_siwmode,		/* SIOCSIWMODE */
	(iw_handler) cfg80211_wext_giwmode,		/* SIOCGIWMODE */
1094 1095 1096 1097 1098 1099 1100 1101
	(iw_handler) NULL,				/* SIOCSIWSENS */
	(iw_handler) NULL,				/* SIOCGIWSENS */
	(iw_handler) NULL /* not used */,		/* SIOCSIWRANGE */
	(iw_handler) ieee80211_ioctl_giwrange,		/* SIOCGIWRANGE */
	(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 已提交
1102 1103 1104 1105
	(iw_handler) NULL,				/* SIOCSIWSPY */
	(iw_handler) NULL,				/* SIOCGIWSPY */
	(iw_handler) NULL,				/* SIOCSIWTHRSPY */
	(iw_handler) NULL,				/* SIOCGIWTHRSPY */
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
	(iw_handler) ieee80211_ioctl_siwap,		/* SIOCSIWAP */
	(iw_handler) ieee80211_ioctl_giwap,		/* SIOCGIWAP */
	(iw_handler) ieee80211_ioctl_siwmlme,		/* SIOCSIWMLME */
	(iw_handler) NULL,				/* SIOCGIWAPLIST */
	(iw_handler) ieee80211_ioctl_siwscan,		/* SIOCSIWSCAN */
	(iw_handler) ieee80211_ioctl_giwscan,		/* SIOCGIWSCAN */
	(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 -- */
1118
	(iw_handler) ieee80211_ioctl_siwrate,		/* SIOCSIWRATE */
1119
	(iw_handler) ieee80211_ioctl_giwrate,		/* SIOCGIWRATE */
1120 1121 1122 1123
	(iw_handler) ieee80211_ioctl_siwrts,		/* SIOCSIWRTS */
	(iw_handler) ieee80211_ioctl_giwrts,		/* SIOCGIWRTS */
	(iw_handler) ieee80211_ioctl_siwfrag,		/* SIOCSIWFRAG */
	(iw_handler) ieee80211_ioctl_giwfrag,		/* SIOCGIWFRAG */
1124
	(iw_handler) ieee80211_ioctl_siwtxpower,	/* SIOCSIWTXPOW */
1125
	(iw_handler) ieee80211_ioctl_giwtxpower,	/* SIOCGIWTXPOW */
1126 1127 1128 1129
	(iw_handler) ieee80211_ioctl_siwretry,		/* SIOCSIWRETRY */
	(iw_handler) ieee80211_ioctl_giwretry,		/* SIOCGIWRETRY */
	(iw_handler) ieee80211_ioctl_siwencode,		/* SIOCSIWENCODE */
	(iw_handler) ieee80211_ioctl_giwencode,		/* SIOCGIWENCODE */
1130 1131
	(iw_handler) ieee80211_ioctl_siwpower,		/* SIOCSIWPOWER */
	(iw_handler) ieee80211_ioctl_giwpower,		/* SIOCGIWPOWER */
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	(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,
};