mxser: fix compiler warning when building without CONFIG_PCI
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / ieee80211_ioctl.c
blobf0224c2311d29a9df5826bfe46def0a93cf3866f
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/etherdevice.h>
17 #include <linux/if_arp.h>
18 #include <linux/wireless.h>
19 #include <net/iw_handler.h>
20 #include <asm/uaccess.h>
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "ieee80211_rate.h"
25 #include "wpa.h"
26 #include "aes_ccm.h"
29 static int ieee80211_set_encryption(struct net_device *dev, u8 *sta_addr,
30 int idx, int alg, int remove,
31 int set_tx_key, const u8 *_key,
32 size_t key_len)
34 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
35 int ret = 0;
36 struct sta_info *sta;
37 struct ieee80211_key *key;
38 struct ieee80211_sub_if_data *sdata;
40 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
42 if (idx < 0 || idx >= NUM_DEFAULT_KEYS) {
43 printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
44 dev->name, idx);
45 return -EINVAL;
48 if (is_broadcast_ether_addr(sta_addr)) {
49 sta = NULL;
50 key = sdata->keys[idx];
51 } else {
52 set_tx_key = 0;
54 * According to the standard, the key index of a pairwise
55 * key must be zero. However, some AP are broken when it
56 * comes to WEP key indices, so we work around this.
58 if (idx != 0 && alg != ALG_WEP) {
59 printk(KERN_DEBUG "%s: set_encrypt - non-zero idx for "
60 "individual key\n", dev->name);
61 return -EINVAL;
64 sta = sta_info_get(local, sta_addr);
65 if (!sta) {
66 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
67 DECLARE_MAC_BUF(mac);
68 printk(KERN_DEBUG "%s: set_encrypt - unknown addr "
69 "%s\n",
70 dev->name, print_mac(mac, sta_addr));
71 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
73 return -ENOENT;
76 key = sta->key;
79 if (remove) {
80 ieee80211_key_free(key);
81 key = NULL;
82 } else {
84 * Automatically frees any old key if present.
86 key = ieee80211_key_alloc(sdata, sta, alg, idx, key_len, _key);
87 if (!key) {
88 ret = -ENOMEM;
89 goto err_out;
93 if (set_tx_key || (!sta && !sdata->default_key && key))
94 ieee80211_set_default_key(sdata, idx);
96 ret = 0;
97 err_out:
98 if (sta)
99 sta_info_put(sta);
100 return ret;
103 static int ieee80211_ioctl_siwgenie(struct net_device *dev,
104 struct iw_request_info *info,
105 struct iw_point *data, char *extra)
107 struct ieee80211_sub_if_data *sdata;
109 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
111 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)
112 return -EOPNOTSUPP;
114 if (sdata->type == IEEE80211_IF_TYPE_STA ||
115 sdata->type == IEEE80211_IF_TYPE_IBSS) {
116 int ret = ieee80211_sta_set_extra_ie(dev, extra, data->length);
117 if (ret)
118 return ret;
119 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
120 ieee80211_sta_req_auth(dev, &sdata->u.sta);
121 return 0;
124 return -EOPNOTSUPP;
127 static int ieee80211_ioctl_giwname(struct net_device *dev,
128 struct iw_request_info *info,
129 char *name, char *extra)
131 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
133 switch (local->hw.conf.phymode) {
134 case MODE_IEEE80211A:
135 strcpy(name, "IEEE 802.11a");
136 break;
137 case MODE_IEEE80211B:
138 strcpy(name, "IEEE 802.11b");
139 break;
140 case MODE_IEEE80211G:
141 strcpy(name, "IEEE 802.11g");
142 break;
143 default:
144 strcpy(name, "IEEE 802.11");
145 break;
148 return 0;
152 static int ieee80211_ioctl_giwrange(struct net_device *dev,
153 struct iw_request_info *info,
154 struct iw_point *data, char *extra)
156 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
157 struct iw_range *range = (struct iw_range *) extra;
158 struct ieee80211_hw_mode *mode = NULL;
159 int c = 0;
161 data->length = sizeof(struct iw_range);
162 memset(range, 0, sizeof(struct iw_range));
164 range->we_version_compiled = WIRELESS_EXT;
165 range->we_version_source = 21;
166 range->retry_capa = IW_RETRY_LIMIT;
167 range->retry_flags = IW_RETRY_LIMIT;
168 range->min_retry = 0;
169 range->max_retry = 255;
170 range->min_rts = 0;
171 range->max_rts = 2347;
172 range->min_frag = 256;
173 range->max_frag = 2346;
175 range->encoding_size[0] = 5;
176 range->encoding_size[1] = 13;
177 range->num_encoding_sizes = 2;
178 range->max_encoding_tokens = NUM_DEFAULT_KEYS;
180 range->max_qual.qual = local->hw.max_signal;
181 range->max_qual.level = local->hw.max_rssi;
182 range->max_qual.noise = local->hw.max_noise;
183 range->max_qual.updated = local->wstats_flags;
185 range->avg_qual.qual = local->hw.max_signal/2;
186 range->avg_qual.level = 0;
187 range->avg_qual.noise = 0;
188 range->avg_qual.updated = local->wstats_flags;
190 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
191 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
193 list_for_each_entry(mode, &local->modes_list, list) {
194 int i = 0;
196 if (!(local->enabled_modes & (1 << mode->mode)) ||
197 (local->hw_modes & local->enabled_modes &
198 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
199 continue;
201 while (i < mode->num_channels && c < IW_MAX_FREQUENCIES) {
202 struct ieee80211_channel *chan = &mode->channels[i];
204 if (chan->flag & IEEE80211_CHAN_W_SCAN) {
205 range->freq[c].i = chan->chan;
206 range->freq[c].m = chan->freq * 100000;
207 range->freq[c].e = 1;
208 c++;
210 i++;
213 range->num_channels = c;
214 range->num_frequency = c;
216 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
217 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
218 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
219 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
221 return 0;
225 static int ieee80211_ioctl_siwmode(struct net_device *dev,
226 struct iw_request_info *info,
227 __u32 *mode, char *extra)
229 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
230 int type;
232 if (sdata->type == IEEE80211_IF_TYPE_VLAN)
233 return -EOPNOTSUPP;
235 switch (*mode) {
236 case IW_MODE_INFRA:
237 type = IEEE80211_IF_TYPE_STA;
238 break;
239 case IW_MODE_ADHOC:
240 type = IEEE80211_IF_TYPE_IBSS;
241 break;
242 case IW_MODE_MONITOR:
243 type = IEEE80211_IF_TYPE_MNTR;
244 break;
245 default:
246 return -EINVAL;
249 if (type == sdata->type)
250 return 0;
251 if (netif_running(dev))
252 return -EBUSY;
254 ieee80211_if_reinit(dev);
255 ieee80211_if_set_type(dev, type);
257 return 0;
261 static int ieee80211_ioctl_giwmode(struct net_device *dev,
262 struct iw_request_info *info,
263 __u32 *mode, char *extra)
265 struct ieee80211_sub_if_data *sdata;
267 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
268 switch (sdata->type) {
269 case IEEE80211_IF_TYPE_AP:
270 *mode = IW_MODE_MASTER;
271 break;
272 case IEEE80211_IF_TYPE_STA:
273 *mode = IW_MODE_INFRA;
274 break;
275 case IEEE80211_IF_TYPE_IBSS:
276 *mode = IW_MODE_ADHOC;
277 break;
278 case IEEE80211_IF_TYPE_MNTR:
279 *mode = IW_MODE_MONITOR;
280 break;
281 case IEEE80211_IF_TYPE_WDS:
282 *mode = IW_MODE_REPEAT;
283 break;
284 case IEEE80211_IF_TYPE_VLAN:
285 *mode = IW_MODE_SECOND; /* FIXME */
286 break;
287 default:
288 *mode = IW_MODE_AUTO;
289 break;
291 return 0;
294 int ieee80211_set_channel(struct ieee80211_local *local, int channel, int freq)
296 struct ieee80211_hw_mode *mode;
297 int c, set = 0;
298 int ret = -EINVAL;
300 list_for_each_entry(mode, &local->modes_list, list) {
301 if (!(local->enabled_modes & (1 << mode->mode)))
302 continue;
303 for (c = 0; c < mode->num_channels; c++) {
304 struct ieee80211_channel *chan = &mode->channels[c];
305 if (chan->flag & IEEE80211_CHAN_W_SCAN &&
306 ((chan->chan == channel) || (chan->freq == freq))) {
307 local->oper_channel = chan;
308 local->oper_hw_mode = mode;
309 set++;
314 if (set) {
315 if (local->sta_scanning)
316 ret = 0;
317 else
318 ret = ieee80211_hw_config(local);
320 rate_control_clear(local);
323 return ret;
326 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
327 struct iw_request_info *info,
328 struct iw_freq *freq, char *extra)
330 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
331 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
333 if (sdata->type == IEEE80211_IF_TYPE_STA)
334 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
336 /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
337 if (freq->e == 0) {
338 if (freq->m < 0) {
339 if (sdata->type == IEEE80211_IF_TYPE_STA)
340 sdata->u.sta.flags |=
341 IEEE80211_STA_AUTO_CHANNEL_SEL;
342 return 0;
343 } else
344 return ieee80211_set_channel(local, freq->m, -1);
345 } else {
346 int i, div = 1000000;
347 for (i = 0; i < freq->e; i++)
348 div /= 10;
349 if (div > 0)
350 return ieee80211_set_channel(local, -1, freq->m / div);
351 else
352 return -EINVAL;
357 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
358 struct iw_request_info *info,
359 struct iw_freq *freq, char *extra)
361 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
363 /* TODO: in station mode (Managed/Ad-hoc) might need to poll low-level
364 * driver for the current channel with firmware-based management */
366 freq->m = local->hw.conf.freq;
367 freq->e = 6;
369 return 0;
373 static int ieee80211_ioctl_siwessid(struct net_device *dev,
374 struct iw_request_info *info,
375 struct iw_point *data, char *ssid)
377 struct ieee80211_sub_if_data *sdata;
378 size_t len = data->length;
380 /* iwconfig uses nul termination in SSID.. */
381 if (len > 0 && ssid[len - 1] == '\0')
382 len--;
384 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
385 if (sdata->type == IEEE80211_IF_TYPE_STA ||
386 sdata->type == IEEE80211_IF_TYPE_IBSS) {
387 int ret;
388 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
389 if (len > IEEE80211_MAX_SSID_LEN)
390 return -EINVAL;
391 memcpy(sdata->u.sta.ssid, ssid, len);
392 sdata->u.sta.ssid_len = len;
393 return 0;
395 if (data->flags)
396 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
397 else
398 sdata->u.sta.flags |= IEEE80211_STA_AUTO_SSID_SEL;
399 ret = ieee80211_sta_set_ssid(dev, ssid, len);
400 if (ret)
401 return ret;
402 ieee80211_sta_req_auth(dev, &sdata->u.sta);
403 return 0;
406 if (sdata->type == IEEE80211_IF_TYPE_AP) {
407 memcpy(sdata->u.ap.ssid, ssid, len);
408 memset(sdata->u.ap.ssid + len, 0,
409 IEEE80211_MAX_SSID_LEN - len);
410 sdata->u.ap.ssid_len = len;
411 return ieee80211_if_config(dev);
413 return -EOPNOTSUPP;
417 static int ieee80211_ioctl_giwessid(struct net_device *dev,
418 struct iw_request_info *info,
419 struct iw_point *data, char *ssid)
421 size_t len;
423 struct ieee80211_sub_if_data *sdata;
424 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
425 if (sdata->type == IEEE80211_IF_TYPE_STA ||
426 sdata->type == IEEE80211_IF_TYPE_IBSS) {
427 int res = ieee80211_sta_get_ssid(dev, ssid, &len);
428 if (res == 0) {
429 data->length = len;
430 data->flags = 1;
431 } else
432 data->flags = 0;
433 return res;
436 if (sdata->type == IEEE80211_IF_TYPE_AP) {
437 len = sdata->u.ap.ssid_len;
438 if (len > IW_ESSID_MAX_SIZE)
439 len = IW_ESSID_MAX_SIZE;
440 memcpy(ssid, sdata->u.ap.ssid, len);
441 data->length = len;
442 data->flags = 1;
443 return 0;
445 return -EOPNOTSUPP;
449 static int ieee80211_ioctl_siwap(struct net_device *dev,
450 struct iw_request_info *info,
451 struct sockaddr *ap_addr, char *extra)
453 struct ieee80211_sub_if_data *sdata;
455 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
456 if (sdata->type == IEEE80211_IF_TYPE_STA ||
457 sdata->type == IEEE80211_IF_TYPE_IBSS) {
458 int ret;
459 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
460 memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
461 ETH_ALEN);
462 return 0;
464 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
465 sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
466 IEEE80211_STA_AUTO_CHANNEL_SEL;
467 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
468 sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
469 else
470 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
471 ret = ieee80211_sta_set_bssid(dev, (u8 *) &ap_addr->sa_data);
472 if (ret)
473 return ret;
474 ieee80211_sta_req_auth(dev, &sdata->u.sta);
475 return 0;
476 } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
477 if (memcmp(sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
478 ETH_ALEN) == 0)
479 return 0;
480 return ieee80211_if_update_wds(dev, (u8 *) &ap_addr->sa_data);
483 return -EOPNOTSUPP;
487 static int ieee80211_ioctl_giwap(struct net_device *dev,
488 struct iw_request_info *info,
489 struct sockaddr *ap_addr, char *extra)
491 struct ieee80211_sub_if_data *sdata;
493 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
494 if (sdata->type == IEEE80211_IF_TYPE_STA ||
495 sdata->type == IEEE80211_IF_TYPE_IBSS) {
496 ap_addr->sa_family = ARPHRD_ETHER;
497 memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
498 return 0;
499 } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
500 ap_addr->sa_family = ARPHRD_ETHER;
501 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
502 return 0;
505 return -EOPNOTSUPP;
509 static int ieee80211_ioctl_siwscan(struct net_device *dev,
510 struct iw_request_info *info,
511 struct iw_point *data, char *extra)
513 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
514 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
515 u8 *ssid = NULL;
516 size_t ssid_len = 0;
518 if (!netif_running(dev))
519 return -ENETDOWN;
521 switch (sdata->type) {
522 case IEEE80211_IF_TYPE_STA:
523 case IEEE80211_IF_TYPE_IBSS:
524 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID) {
525 ssid = sdata->u.sta.ssid;
526 ssid_len = sdata->u.sta.ssid_len;
528 break;
529 case IEEE80211_IF_TYPE_AP:
530 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID) {
531 ssid = sdata->u.ap.ssid;
532 ssid_len = sdata->u.ap.ssid_len;
534 break;
535 default:
536 return -EOPNOTSUPP;
539 return ieee80211_sta_req_scan(dev, ssid, ssid_len);
543 static int ieee80211_ioctl_giwscan(struct net_device *dev,
544 struct iw_request_info *info,
545 struct iw_point *data, char *extra)
547 int res;
548 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
549 if (local->sta_scanning)
550 return -EAGAIN;
551 res = ieee80211_sta_scan_results(dev, extra, data->length);
552 if (res >= 0) {
553 data->length = res;
554 return 0;
556 data->length = 0;
557 return res;
561 static int ieee80211_ioctl_siwrate(struct net_device *dev,
562 struct iw_request_info *info,
563 struct iw_param *rate, char *extra)
565 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
566 struct ieee80211_hw_mode *mode;
567 int i;
568 u32 target_rate = rate->value / 100000;
569 struct ieee80211_sub_if_data *sdata;
571 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
572 if (!sdata->bss)
573 return -ENODEV;
574 mode = local->oper_hw_mode;
575 /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
576 * target_rate = X, rate->fixed = 1 means only rate X
577 * target_rate = X, rate->fixed = 0 means all rates <= X */
578 sdata->bss->max_ratectrl_rateidx = -1;
579 sdata->bss->force_unicast_rateidx = -1;
580 if (rate->value < 0)
581 return 0;
582 for (i=0; i< mode->num_rates; i++) {
583 struct ieee80211_rate *rates = &mode->rates[i];
584 int this_rate = rates->rate;
586 if (target_rate == this_rate) {
587 sdata->bss->max_ratectrl_rateidx = i;
588 if (rate->fixed)
589 sdata->bss->force_unicast_rateidx = i;
590 break;
593 return 0;
596 static int ieee80211_ioctl_giwrate(struct net_device *dev,
597 struct iw_request_info *info,
598 struct iw_param *rate, char *extra)
600 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
601 struct sta_info *sta;
602 struct ieee80211_sub_if_data *sdata;
604 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
605 if (sdata->type == IEEE80211_IF_TYPE_STA)
606 sta = sta_info_get(local, sdata->u.sta.bssid);
607 else
608 return -EOPNOTSUPP;
609 if (!sta)
610 return -ENODEV;
611 if (sta->txrate < local->oper_hw_mode->num_rates)
612 rate->value = local->oper_hw_mode->rates[sta->txrate].rate * 100000;
613 else
614 rate->value = 0;
615 sta_info_put(sta);
616 return 0;
619 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
620 struct iw_request_info *info,
621 union iwreq_data *data, char *extra)
623 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
624 bool need_reconfig = 0;
626 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
627 return -EINVAL;
628 if (data->txpower.flags & IW_TXPOW_RANGE)
629 return -EINVAL;
630 if (!data->txpower.fixed)
631 return -EINVAL;
633 if (local->hw.conf.power_level != data->txpower.value) {
634 local->hw.conf.power_level = data->txpower.value;
635 need_reconfig = 1;
637 if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
638 local->hw.conf.radio_enabled = !(data->txpower.disabled);
639 need_reconfig = 1;
641 if (need_reconfig) {
642 ieee80211_hw_config(local);
643 /* The return value of hw_config is not of big interest here,
644 * as it doesn't say that it failed because of _this_ config
645 * change or something else. Ignore it. */
648 return 0;
651 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
652 struct iw_request_info *info,
653 union iwreq_data *data, char *extra)
655 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
657 data->txpower.fixed = 1;
658 data->txpower.disabled = !(local->hw.conf.radio_enabled);
659 data->txpower.value = local->hw.conf.power_level;
660 data->txpower.flags = IW_TXPOW_DBM;
662 return 0;
665 static int ieee80211_ioctl_siwrts(struct net_device *dev,
666 struct iw_request_info *info,
667 struct iw_param *rts, char *extra)
669 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
671 if (rts->disabled)
672 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
673 else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
674 return -EINVAL;
675 else
676 local->rts_threshold = rts->value;
678 /* If the wlan card performs RTS/CTS in hardware/firmware,
679 * configure it here */
681 if (local->ops->set_rts_threshold)
682 local->ops->set_rts_threshold(local_to_hw(local),
683 local->rts_threshold);
685 return 0;
688 static int ieee80211_ioctl_giwrts(struct net_device *dev,
689 struct iw_request_info *info,
690 struct iw_param *rts, char *extra)
692 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
694 rts->value = local->rts_threshold;
695 rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
696 rts->fixed = 1;
698 return 0;
702 static int ieee80211_ioctl_siwfrag(struct net_device *dev,
703 struct iw_request_info *info,
704 struct iw_param *frag, char *extra)
706 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
708 if (frag->disabled)
709 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
710 else if (frag->value < 256 ||
711 frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
712 return -EINVAL;
713 else {
714 /* Fragment length must be even, so strip LSB. */
715 local->fragmentation_threshold = frag->value & ~0x1;
718 /* If the wlan card performs fragmentation in hardware/firmware,
719 * configure it here */
721 if (local->ops->set_frag_threshold)
722 local->ops->set_frag_threshold(
723 local_to_hw(local),
724 local->fragmentation_threshold);
726 return 0;
729 static int ieee80211_ioctl_giwfrag(struct net_device *dev,
730 struct iw_request_info *info,
731 struct iw_param *frag, char *extra)
733 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
735 frag->value = local->fragmentation_threshold;
736 frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
737 frag->fixed = 1;
739 return 0;
743 static int ieee80211_ioctl_siwretry(struct net_device *dev,
744 struct iw_request_info *info,
745 struct iw_param *retry, char *extra)
747 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
749 if (retry->disabled ||
750 (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
751 return -EINVAL;
753 if (retry->flags & IW_RETRY_MAX)
754 local->long_retry_limit = retry->value;
755 else if (retry->flags & IW_RETRY_MIN)
756 local->short_retry_limit = retry->value;
757 else {
758 local->long_retry_limit = retry->value;
759 local->short_retry_limit = retry->value;
762 if (local->ops->set_retry_limit) {
763 return local->ops->set_retry_limit(
764 local_to_hw(local),
765 local->short_retry_limit,
766 local->long_retry_limit);
769 return 0;
773 static int ieee80211_ioctl_giwretry(struct net_device *dev,
774 struct iw_request_info *info,
775 struct iw_param *retry, char *extra)
777 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
779 retry->disabled = 0;
780 if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
781 /* first return min value, iwconfig will ask max value
782 * later if needed */
783 retry->flags |= IW_RETRY_LIMIT;
784 retry->value = local->short_retry_limit;
785 if (local->long_retry_limit != local->short_retry_limit)
786 retry->flags |= IW_RETRY_MIN;
787 return 0;
789 if (retry->flags & IW_RETRY_MAX) {
790 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
791 retry->value = local->long_retry_limit;
794 return 0;
797 static int ieee80211_ioctl_siwmlme(struct net_device *dev,
798 struct iw_request_info *info,
799 struct iw_point *data, char *extra)
801 struct ieee80211_sub_if_data *sdata;
802 struct iw_mlme *mlme = (struct iw_mlme *) extra;
804 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
805 if (sdata->type != IEEE80211_IF_TYPE_STA &&
806 sdata->type != IEEE80211_IF_TYPE_IBSS)
807 return -EINVAL;
809 switch (mlme->cmd) {
810 case IW_MLME_DEAUTH:
811 /* TODO: mlme->addr.sa_data */
812 return ieee80211_sta_deauthenticate(dev, mlme->reason_code);
813 case IW_MLME_DISASSOC:
814 /* TODO: mlme->addr.sa_data */
815 return ieee80211_sta_disassociate(dev, mlme->reason_code);
816 default:
817 return -EOPNOTSUPP;
822 static int ieee80211_ioctl_siwencode(struct net_device *dev,
823 struct iw_request_info *info,
824 struct iw_point *erq, char *keybuf)
826 struct ieee80211_sub_if_data *sdata;
827 int idx, i, alg = ALG_WEP;
828 u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
829 int remove = 0;
831 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
833 idx = erq->flags & IW_ENCODE_INDEX;
834 if (idx == 0) {
835 if (sdata->default_key)
836 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
837 if (sdata->default_key == sdata->keys[i]) {
838 idx = i;
839 break;
842 } else if (idx < 1 || idx > 4)
843 return -EINVAL;
844 else
845 idx--;
847 if (erq->flags & IW_ENCODE_DISABLED)
848 remove = 1;
849 else if (erq->length == 0) {
850 /* No key data - just set the default TX key index */
851 ieee80211_set_default_key(sdata, idx);
852 return 0;
855 return ieee80211_set_encryption(
856 dev, bcaddr,
857 idx, alg, remove,
858 !sdata->default_key,
859 keybuf, erq->length);
863 static int ieee80211_ioctl_giwencode(struct net_device *dev,
864 struct iw_request_info *info,
865 struct iw_point *erq, char *key)
867 struct ieee80211_sub_if_data *sdata;
868 int idx, i;
870 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
872 idx = erq->flags & IW_ENCODE_INDEX;
873 if (idx < 1 || idx > 4) {
874 idx = -1;
875 if (!sdata->default_key)
876 idx = 0;
877 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
878 if (sdata->default_key == sdata->keys[i]) {
879 idx = i;
880 break;
883 if (idx < 0)
884 return -EINVAL;
885 } else
886 idx--;
888 erq->flags = idx + 1;
890 if (!sdata->keys[idx]) {
891 erq->length = 0;
892 erq->flags |= IW_ENCODE_DISABLED;
893 return 0;
896 memcpy(key, sdata->keys[idx]->conf.key,
897 min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
898 erq->length = sdata->keys[idx]->conf.keylen;
899 erq->flags |= IW_ENCODE_ENABLED;
901 return 0;
904 static int ieee80211_ioctl_siwauth(struct net_device *dev,
905 struct iw_request_info *info,
906 struct iw_param *data, char *extra)
908 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
909 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
910 int ret = 0;
912 switch (data->flags & IW_AUTH_INDEX) {
913 case IW_AUTH_WPA_VERSION:
914 case IW_AUTH_CIPHER_PAIRWISE:
915 case IW_AUTH_CIPHER_GROUP:
916 case IW_AUTH_WPA_ENABLED:
917 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
918 break;
919 case IW_AUTH_KEY_MGMT:
920 if (sdata->type != IEEE80211_IF_TYPE_STA)
921 ret = -EINVAL;
922 else {
924 * Key management was set by wpa_supplicant,
925 * we only need this to associate to a network
926 * that has privacy enabled regardless of not
927 * having a key.
929 sdata->u.sta.key_management_enabled = !!data->value;
931 break;
932 case IW_AUTH_80211_AUTH_ALG:
933 if (sdata->type == IEEE80211_IF_TYPE_STA ||
934 sdata->type == IEEE80211_IF_TYPE_IBSS)
935 sdata->u.sta.auth_algs = data->value;
936 else
937 ret = -EOPNOTSUPP;
938 break;
939 case IW_AUTH_PRIVACY_INVOKED:
940 if (local->ops->set_privacy_invoked)
941 ret = local->ops->set_privacy_invoked(
942 local_to_hw(local), data->value);
943 break;
944 default:
945 ret = -EOPNOTSUPP;
946 break;
948 return ret;
951 /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
952 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
954 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
955 struct iw_statistics *wstats = &local->wstats;
956 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
957 struct sta_info *sta = NULL;
959 if (sdata->type == IEEE80211_IF_TYPE_STA ||
960 sdata->type == IEEE80211_IF_TYPE_IBSS)
961 sta = sta_info_get(local, sdata->u.sta.bssid);
962 if (!sta) {
963 wstats->discard.fragment = 0;
964 wstats->discard.misc = 0;
965 wstats->qual.qual = 0;
966 wstats->qual.level = 0;
967 wstats->qual.noise = 0;
968 wstats->qual.updated = IW_QUAL_ALL_INVALID;
969 } else {
970 wstats->qual.level = sta->last_rssi;
971 wstats->qual.qual = sta->last_signal;
972 wstats->qual.noise = sta->last_noise;
973 wstats->qual.updated = local->wstats_flags;
974 sta_info_put(sta);
976 return wstats;
979 static int ieee80211_ioctl_giwauth(struct net_device *dev,
980 struct iw_request_info *info,
981 struct iw_param *data, char *extra)
983 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
984 int ret = 0;
986 switch (data->flags & IW_AUTH_INDEX) {
987 case IW_AUTH_80211_AUTH_ALG:
988 if (sdata->type == IEEE80211_IF_TYPE_STA ||
989 sdata->type == IEEE80211_IF_TYPE_IBSS)
990 data->value = sdata->u.sta.auth_algs;
991 else
992 ret = -EOPNOTSUPP;
993 break;
994 default:
995 ret = -EOPNOTSUPP;
996 break;
998 return ret;
1002 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1003 struct iw_request_info *info,
1004 struct iw_point *erq, char *extra)
1006 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1007 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
1008 int uninitialized_var(alg), idx, i, remove = 0;
1010 switch (ext->alg) {
1011 case IW_ENCODE_ALG_NONE:
1012 remove = 1;
1013 break;
1014 case IW_ENCODE_ALG_WEP:
1015 alg = ALG_WEP;
1016 break;
1017 case IW_ENCODE_ALG_TKIP:
1018 alg = ALG_TKIP;
1019 break;
1020 case IW_ENCODE_ALG_CCMP:
1021 alg = ALG_CCMP;
1022 break;
1023 default:
1024 return -EOPNOTSUPP;
1027 if (erq->flags & IW_ENCODE_DISABLED)
1028 remove = 1;
1030 idx = erq->flags & IW_ENCODE_INDEX;
1031 if (idx < 1 || idx > 4) {
1032 idx = -1;
1033 if (!sdata->default_key)
1034 idx = 0;
1035 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1036 if (sdata->default_key == sdata->keys[i]) {
1037 idx = i;
1038 break;
1041 if (idx < 0)
1042 return -EINVAL;
1043 } else
1044 idx--;
1046 return ieee80211_set_encryption(dev, ext->addr.sa_data, idx, alg,
1047 remove,
1048 ext->ext_flags &
1049 IW_ENCODE_EXT_SET_TX_KEY,
1050 ext->key, ext->key_len);
1054 /* Structures to export the Wireless Handlers */
1056 static const iw_handler ieee80211_handler[] =
1058 (iw_handler) NULL, /* SIOCSIWCOMMIT */
1059 (iw_handler) ieee80211_ioctl_giwname, /* SIOCGIWNAME */
1060 (iw_handler) NULL, /* SIOCSIWNWID */
1061 (iw_handler) NULL, /* SIOCGIWNWID */
1062 (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
1063 (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
1064 (iw_handler) ieee80211_ioctl_siwmode, /* SIOCSIWMODE */
1065 (iw_handler) ieee80211_ioctl_giwmode, /* SIOCGIWMODE */
1066 (iw_handler) NULL, /* SIOCSIWSENS */
1067 (iw_handler) NULL, /* SIOCGIWSENS */
1068 (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
1069 (iw_handler) ieee80211_ioctl_giwrange, /* SIOCGIWRANGE */
1070 (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
1071 (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
1072 (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
1073 (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
1074 (iw_handler) NULL, /* SIOCSIWSPY */
1075 (iw_handler) NULL, /* SIOCGIWSPY */
1076 (iw_handler) NULL, /* SIOCSIWTHRSPY */
1077 (iw_handler) NULL, /* SIOCGIWTHRSPY */
1078 (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
1079 (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
1080 (iw_handler) ieee80211_ioctl_siwmlme, /* SIOCSIWMLME */
1081 (iw_handler) NULL, /* SIOCGIWAPLIST */
1082 (iw_handler) ieee80211_ioctl_siwscan, /* SIOCSIWSCAN */
1083 (iw_handler) ieee80211_ioctl_giwscan, /* SIOCGIWSCAN */
1084 (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
1085 (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
1086 (iw_handler) NULL, /* SIOCSIWNICKN */
1087 (iw_handler) NULL, /* SIOCGIWNICKN */
1088 (iw_handler) NULL, /* -- hole -- */
1089 (iw_handler) NULL, /* -- hole -- */
1090 (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
1091 (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
1092 (iw_handler) ieee80211_ioctl_siwrts, /* SIOCSIWRTS */
1093 (iw_handler) ieee80211_ioctl_giwrts, /* SIOCGIWRTS */
1094 (iw_handler) ieee80211_ioctl_siwfrag, /* SIOCSIWFRAG */
1095 (iw_handler) ieee80211_ioctl_giwfrag, /* SIOCGIWFRAG */
1096 (iw_handler) ieee80211_ioctl_siwtxpower, /* SIOCSIWTXPOW */
1097 (iw_handler) ieee80211_ioctl_giwtxpower, /* SIOCGIWTXPOW */
1098 (iw_handler) ieee80211_ioctl_siwretry, /* SIOCSIWRETRY */
1099 (iw_handler) ieee80211_ioctl_giwretry, /* SIOCGIWRETRY */
1100 (iw_handler) ieee80211_ioctl_siwencode, /* SIOCSIWENCODE */
1101 (iw_handler) ieee80211_ioctl_giwencode, /* SIOCGIWENCODE */
1102 (iw_handler) NULL, /* SIOCSIWPOWER */
1103 (iw_handler) NULL, /* SIOCGIWPOWER */
1104 (iw_handler) NULL, /* -- hole -- */
1105 (iw_handler) NULL, /* -- hole -- */
1106 (iw_handler) ieee80211_ioctl_siwgenie, /* SIOCSIWGENIE */
1107 (iw_handler) NULL, /* SIOCGIWGENIE */
1108 (iw_handler) ieee80211_ioctl_siwauth, /* SIOCSIWAUTH */
1109 (iw_handler) ieee80211_ioctl_giwauth, /* SIOCGIWAUTH */
1110 (iw_handler) ieee80211_ioctl_siwencodeext, /* SIOCSIWENCODEEXT */
1111 (iw_handler) NULL, /* SIOCGIWENCODEEXT */
1112 (iw_handler) NULL, /* SIOCSIWPMKSA */
1113 (iw_handler) NULL, /* -- hole -- */
1116 const struct iw_handler_def ieee80211_iw_handler_def =
1118 .num_standard = ARRAY_SIZE(ieee80211_handler),
1119 .standard = (iw_handler *) ieee80211_handler,
1120 .get_wireless_stats = ieee80211_get_wireless_stats,