add git checkout support
[openwrt/openwrt.git] / package / mac80211 / src / mac80211 / ieee80211_ioctl.c
1 /*
2 * Copyright 2002-2005, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc.
4 *
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 */
9
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>
21
22 #include <net/mac80211.h>
23 #include "ieee80211_i.h"
24 #include "ieee80211_rate.h"
25 #include "wpa.h"
26 #include "aes_ccm.h"
27
28
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)
33 {
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;
39
40 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
41
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;
46 }
47
48 if (is_broadcast_ether_addr(sta_addr)) {
49 sta = NULL;
50 key = sdata->keys[idx];
51 } else {
52 set_tx_key = 0;
53 /*
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.
57 */
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;
62 }
63
64 sta = sta_info_get(local, sta_addr);
65 if (!sta) {
66 #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
67 printk(KERN_DEBUG "%s: set_encrypt - unknown addr "
68 MAC_FMT "\n",
69 dev->name, MAC_ARG(sta_addr));
70 #endif /* CONFIG_MAC80211_VERBOSE_DEBUG */
71
72 return -ENOENT;
73 }
74
75 key = sta->key;
76 }
77
78 if (remove) {
79 ieee80211_key_free(key);
80 key = NULL;
81 } else {
82 /*
83 * Automatically frees any old key if present.
84 */
85 key = ieee80211_key_alloc(sdata, sta, alg, idx, key_len, _key);
86 if (!key) {
87 ret = -ENOMEM;
88 goto err_out;
89 }
90 }
91
92 if (set_tx_key || (!sta && !sdata->default_key && key))
93 ieee80211_set_default_key(sdata, idx);
94
95 ret = 0;
96 err_out:
97 if (sta)
98 sta_info_put(sta);
99 return ret;
100 }
101
102 static int ieee80211_ioctl_siwgenie(struct net_device *dev,
103 struct iw_request_info *info,
104 struct iw_point *data, char *extra)
105 {
106 struct ieee80211_sub_if_data *sdata;
107
108 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
109
110 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME)
111 return -EOPNOTSUPP;
112
113 if (sdata->type == IEEE80211_IF_TYPE_STA ||
114 sdata->type == IEEE80211_IF_TYPE_IBSS) {
115 int ret = ieee80211_sta_set_extra_ie(dev, extra, data->length);
116 if (ret)
117 return ret;
118 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
119 ieee80211_sta_req_auth(dev, &sdata->u.sta);
120 return 0;
121 }
122
123 return -EOPNOTSUPP;
124 }
125
126 static int ieee80211_ioctl_giwname(struct net_device *dev,
127 struct iw_request_info *info,
128 char *name, char *extra)
129 {
130 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
131
132 switch (local->hw.conf.phymode) {
133 case MODE_IEEE80211A:
134 strcpy(name, "IEEE 802.11a");
135 break;
136 case MODE_IEEE80211B:
137 strcpy(name, "IEEE 802.11b");
138 break;
139 case MODE_IEEE80211G:
140 strcpy(name, "IEEE 802.11g");
141 break;
142 default:
143 strcpy(name, "IEEE 802.11");
144 break;
145 }
146
147 return 0;
148 }
149
150
151 static int ieee80211_ioctl_giwrange(struct net_device *dev,
152 struct iw_request_info *info,
153 struct iw_point *data, char *extra)
154 {
155 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
156 struct iw_range *range = (struct iw_range *) extra;
157 struct ieee80211_hw_mode *mode = NULL;
158 int c = 0;
159
160 data->length = sizeof(struct iw_range);
161 memset(range, 0, sizeof(struct iw_range));
162
163 range->we_version_compiled = WIRELESS_EXT;
164 range->we_version_source = 21;
165 range->retry_capa = IW_RETRY_LIMIT;
166 range->retry_flags = IW_RETRY_LIMIT;
167 range->min_retry = 0;
168 range->max_retry = 255;
169 range->min_rts = 0;
170 range->max_rts = 2347;
171 range->min_frag = 256;
172 range->max_frag = 2346;
173
174 range->encoding_size[0] = 5;
175 range->encoding_size[1] = 13;
176 range->num_encoding_sizes = 2;
177 range->max_encoding_tokens = NUM_DEFAULT_KEYS;
178
179 range->max_qual.qual = local->hw.max_signal;
180 range->max_qual.level = local->hw.max_rssi;
181 range->max_qual.noise = local->hw.max_noise;
182 range->max_qual.updated = local->wstats_flags;
183
184 range->avg_qual.qual = local->hw.max_signal/2;
185 range->avg_qual.level = 0;
186 range->avg_qual.noise = 0;
187 range->avg_qual.updated = local->wstats_flags;
188
189 range->enc_capa = IW_ENC_CAPA_WPA | IW_ENC_CAPA_WPA2 |
190 IW_ENC_CAPA_CIPHER_TKIP | IW_ENC_CAPA_CIPHER_CCMP;
191
192 list_for_each_entry(mode, &local->modes_list, list) {
193 int i = 0;
194
195 if (!(local->enabled_modes & (1 << mode->mode)) ||
196 (local->hw_modes & local->enabled_modes &
197 (1 << MODE_IEEE80211G) && mode->mode == MODE_IEEE80211B))
198 continue;
199
200 while (i < mode->num_channels && c < IW_MAX_FREQUENCIES) {
201 struct ieee80211_channel *chan = &mode->channels[i];
202
203 if (chan->flag & IEEE80211_CHAN_W_SCAN) {
204 range->freq[c].i = chan->chan;
205 range->freq[c].m = chan->freq * 100000;
206 range->freq[c].e = 1;
207 c++;
208 }
209 i++;
210 }
211 }
212 range->num_channels = c;
213 range->num_frequency = c;
214
215 IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
216 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWTHRSPY);
217 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
218 IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
219
220 return 0;
221 }
222
223
224 static int ieee80211_ioctl_siwmode(struct net_device *dev,
225 struct iw_request_info *info,
226 __u32 *mode, char *extra)
227 {
228 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
229 int type;
230
231 if (sdata->type == IEEE80211_IF_TYPE_VLAN)
232 return -EOPNOTSUPP;
233
234 switch (*mode) {
235 case IW_MODE_INFRA:
236 type = IEEE80211_IF_TYPE_STA;
237 break;
238 case IW_MODE_ADHOC:
239 type = IEEE80211_IF_TYPE_IBSS;
240 break;
241 case IW_MODE_MONITOR:
242 type = IEEE80211_IF_TYPE_MNTR;
243 break;
244 default:
245 return -EINVAL;
246 }
247
248 if (type == sdata->type)
249 return 0;
250 if (netif_running(dev))
251 return -EBUSY;
252
253 ieee80211_if_reinit(dev);
254 ieee80211_if_set_type(dev, type);
255
256 return 0;
257 }
258
259
260 static int ieee80211_ioctl_giwmode(struct net_device *dev,
261 struct iw_request_info *info,
262 __u32 *mode, char *extra)
263 {
264 struct ieee80211_sub_if_data *sdata;
265
266 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
267 switch (sdata->type) {
268 case IEEE80211_IF_TYPE_AP:
269 *mode = IW_MODE_MASTER;
270 break;
271 case IEEE80211_IF_TYPE_STA:
272 *mode = IW_MODE_INFRA;
273 break;
274 case IEEE80211_IF_TYPE_IBSS:
275 *mode = IW_MODE_ADHOC;
276 break;
277 case IEEE80211_IF_TYPE_MNTR:
278 *mode = IW_MODE_MONITOR;
279 break;
280 case IEEE80211_IF_TYPE_WDS:
281 *mode = IW_MODE_REPEAT;
282 break;
283 case IEEE80211_IF_TYPE_VLAN:
284 *mode = IW_MODE_SECOND; /* FIXME */
285 break;
286 default:
287 *mode = IW_MODE_AUTO;
288 break;
289 }
290 return 0;
291 }
292
293 int ieee80211_set_channel(struct ieee80211_local *local, int channel, int freq)
294 {
295 struct ieee80211_hw_mode *mode;
296 int c, set = 0;
297 int ret = -EINVAL;
298
299 list_for_each_entry(mode, &local->modes_list, list) {
300 if (!(local->enabled_modes & (1 << mode->mode)))
301 continue;
302 for (c = 0; c < mode->num_channels; c++) {
303 struct ieee80211_channel *chan = &mode->channels[c];
304 if (chan->flag & IEEE80211_CHAN_W_SCAN &&
305 ((chan->chan == channel) || (chan->freq == freq))) {
306 local->oper_channel = chan;
307 local->oper_hw_mode = mode;
308 set++;
309 }
310 }
311 }
312
313 if (set) {
314 if (local->sta_scanning)
315 ret = 0;
316 else
317 ret = ieee80211_hw_config(local);
318
319 rate_control_clear(local);
320 }
321
322 return ret;
323 }
324
325 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
326 struct iw_request_info *info,
327 struct iw_freq *freq, char *extra)
328 {
329 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
330 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
331
332 if (sdata->type == IEEE80211_IF_TYPE_STA)
333 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
334
335 /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
336 if (freq->e == 0) {
337 if (freq->m < 0) {
338 if (sdata->type == IEEE80211_IF_TYPE_STA)
339 sdata->u.sta.flags |=
340 IEEE80211_STA_AUTO_CHANNEL_SEL;
341 return 0;
342 } else
343 return ieee80211_set_channel(local, freq->m, -1);
344 } else {
345 int i, div = 1000000;
346 for (i = 0; i < freq->e; i++)
347 div /= 10;
348 if (div > 0)
349 return ieee80211_set_channel(local, -1, freq->m / div);
350 else
351 return -EINVAL;
352 }
353 }
354
355
356 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
357 struct iw_request_info *info,
358 struct iw_freq *freq, char *extra)
359 {
360 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
361
362 /* TODO: in station mode (Managed/Ad-hoc) might need to poll low-level
363 * driver for the current channel with firmware-based management */
364
365 freq->m = local->hw.conf.freq;
366 freq->e = 6;
367
368 return 0;
369 }
370
371
372 static int ieee80211_ioctl_siwessid(struct net_device *dev,
373 struct iw_request_info *info,
374 struct iw_point *data, char *ssid)
375 {
376 struct ieee80211_sub_if_data *sdata;
377 size_t len = data->length;
378
379 /* iwconfig uses nul termination in SSID.. */
380 if (len > 0 && ssid[len - 1] == '\0')
381 len--;
382
383 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
384 if (sdata->type == IEEE80211_IF_TYPE_STA ||
385 sdata->type == IEEE80211_IF_TYPE_IBSS) {
386 int ret;
387 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
388 if (len > IEEE80211_MAX_SSID_LEN)
389 return -EINVAL;
390 memcpy(sdata->u.sta.ssid, ssid, len);
391 sdata->u.sta.ssid_len = len;
392 return 0;
393 }
394 if (data->flags)
395 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
396 else
397 sdata->u.sta.flags |= IEEE80211_STA_AUTO_SSID_SEL;
398 ret = ieee80211_sta_set_ssid(dev, ssid, len);
399 if (ret)
400 return ret;
401 ieee80211_sta_req_auth(dev, &sdata->u.sta);
402 return 0;
403 }
404
405 if (sdata->type == IEEE80211_IF_TYPE_AP) {
406 memcpy(sdata->u.ap.ssid, ssid, len);
407 memset(sdata->u.ap.ssid + len, 0,
408 IEEE80211_MAX_SSID_LEN - len);
409 sdata->u.ap.ssid_len = len;
410 return ieee80211_if_config(dev);
411 }
412 return -EOPNOTSUPP;
413 }
414
415
416 static int ieee80211_ioctl_giwessid(struct net_device *dev,
417 struct iw_request_info *info,
418 struct iw_point *data, char *ssid)
419 {
420 size_t len;
421
422 struct ieee80211_sub_if_data *sdata;
423 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
424 if (sdata->type == IEEE80211_IF_TYPE_STA ||
425 sdata->type == IEEE80211_IF_TYPE_IBSS) {
426 int res = ieee80211_sta_get_ssid(dev, ssid, &len);
427 if (res == 0) {
428 data->length = len;
429 data->flags = 1;
430 } else
431 data->flags = 0;
432 return res;
433 }
434
435 if (sdata->type == IEEE80211_IF_TYPE_AP) {
436 len = sdata->u.ap.ssid_len;
437 if (len > IW_ESSID_MAX_SIZE)
438 len = IW_ESSID_MAX_SIZE;
439 memcpy(ssid, sdata->u.ap.ssid, len);
440 data->length = len;
441 data->flags = 1;
442 return 0;
443 }
444 return -EOPNOTSUPP;
445 }
446
447
448 static int ieee80211_ioctl_siwap(struct net_device *dev,
449 struct iw_request_info *info,
450 struct sockaddr *ap_addr, char *extra)
451 {
452 struct ieee80211_sub_if_data *sdata;
453
454 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
455 if (sdata->type == IEEE80211_IF_TYPE_STA ||
456 sdata->type == IEEE80211_IF_TYPE_IBSS) {
457 int ret;
458 if (sdata->flags & IEEE80211_SDATA_USERSPACE_MLME) {
459 memcpy(sdata->u.sta.bssid, (u8 *) &ap_addr->sa_data,
460 ETH_ALEN);
461 return 0;
462 }
463 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
464 sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
465 IEEE80211_STA_AUTO_CHANNEL_SEL;
466 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
467 sdata->u.sta.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
468 else
469 sdata->u.sta.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
470 ret = ieee80211_sta_set_bssid(dev, (u8 *) &ap_addr->sa_data);
471 if (ret)
472 return ret;
473 ieee80211_sta_req_auth(dev, &sdata->u.sta);
474 return 0;
475 } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
476 if (memcmp(sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
477 ETH_ALEN) == 0)
478 return 0;
479 return ieee80211_if_update_wds(dev, (u8 *) &ap_addr->sa_data);
480 }
481
482 return -EOPNOTSUPP;
483 }
484
485
486 static int ieee80211_ioctl_giwap(struct net_device *dev,
487 struct iw_request_info *info,
488 struct sockaddr *ap_addr, char *extra)
489 {
490 struct ieee80211_sub_if_data *sdata;
491
492 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
493 if (sdata->type == IEEE80211_IF_TYPE_STA ||
494 sdata->type == IEEE80211_IF_TYPE_IBSS) {
495 ap_addr->sa_family = ARPHRD_ETHER;
496 memcpy(&ap_addr->sa_data, sdata->u.sta.bssid, ETH_ALEN);
497 return 0;
498 } else if (sdata->type == IEEE80211_IF_TYPE_WDS) {
499 ap_addr->sa_family = ARPHRD_ETHER;
500 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
501 return 0;
502 }
503
504 return -EOPNOTSUPP;
505 }
506
507
508 static int ieee80211_ioctl_siwscan(struct net_device *dev,
509 struct iw_request_info *info,
510 struct iw_point *data, char *extra)
511 {
512 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
513 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
514 u8 *ssid = NULL;
515 size_t ssid_len = 0;
516
517 if (!netif_running(dev))
518 return -ENETDOWN;
519
520 switch (sdata->type) {
521 case IEEE80211_IF_TYPE_STA:
522 case IEEE80211_IF_TYPE_IBSS:
523 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID) {
524 ssid = sdata->u.sta.ssid;
525 ssid_len = sdata->u.sta.ssid_len;
526 }
527 break;
528 case IEEE80211_IF_TYPE_AP:
529 if (local->scan_flags & IEEE80211_SCAN_MATCH_SSID) {
530 ssid = sdata->u.ap.ssid;
531 ssid_len = sdata->u.ap.ssid_len;
532 }
533 break;
534 default:
535 return -EOPNOTSUPP;
536 }
537
538 return ieee80211_sta_req_scan(dev, ssid, ssid_len);
539 }
540
541
542 static int ieee80211_ioctl_giwscan(struct net_device *dev,
543 struct iw_request_info *info,
544 struct iw_point *data, char *extra)
545 {
546 int res;
547 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
548 if (local->sta_scanning)
549 return -EAGAIN;
550 res = ieee80211_sta_scan_results(dev, extra, data->length);
551 if (res >= 0) {
552 data->length = res;
553 return 0;
554 }
555 data->length = 0;
556 return res;
557 }
558
559
560 static int ieee80211_ioctl_siwrate(struct net_device *dev,
561 struct iw_request_info *info,
562 struct iw_param *rate, char *extra)
563 {
564 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
565 struct ieee80211_hw_mode *mode;
566 int i;
567 u32 target_rate = rate->value / 100000;
568 struct ieee80211_sub_if_data *sdata;
569
570 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
571 if (!sdata->bss)
572 return -ENODEV;
573 mode = local->oper_hw_mode;
574 /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
575 * target_rate = X, rate->fixed = 1 means only rate X
576 * target_rate = X, rate->fixed = 0 means all rates <= X */
577 sdata->bss->max_ratectrl_rateidx = -1;
578 sdata->bss->force_unicast_rateidx = -1;
579 if (rate->value < 0)
580 return 0;
581 for (i=0; i< mode->num_rates; i++) {
582 struct ieee80211_rate *rates = &mode->rates[i];
583 int this_rate = rates->rate;
584
585 if (target_rate == this_rate) {
586 sdata->bss->max_ratectrl_rateidx = i;
587 if (rate->fixed)
588 sdata->bss->force_unicast_rateidx = i;
589 break;
590 }
591 }
592 return 0;
593 }
594
595 static int ieee80211_ioctl_giwrate(struct net_device *dev,
596 struct iw_request_info *info,
597 struct iw_param *rate, char *extra)
598 {
599 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
600 struct sta_info *sta;
601 struct ieee80211_sub_if_data *sdata;
602
603 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
604 if (sdata->type == IEEE80211_IF_TYPE_STA)
605 sta = sta_info_get(local, sdata->u.sta.bssid);
606 else
607 return -EOPNOTSUPP;
608 if (!sta)
609 return -ENODEV;
610 if (sta->txrate < local->oper_hw_mode->num_rates)
611 rate->value = local->oper_hw_mode->rates[sta->txrate].rate * 100000;
612 else
613 rate->value = 0;
614 sta_info_put(sta);
615 return 0;
616 }
617
618 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
619 struct iw_request_info *info,
620 union iwreq_data *data, char *extra)
621 {
622 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
623 bool need_reconfig = 0;
624
625 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
626 return -EINVAL;
627 if (data->txpower.flags & IW_TXPOW_RANGE)
628 return -EINVAL;
629 if (!data->txpower.fixed)
630 return -EINVAL;
631
632 if (local->hw.conf.power_level != data->txpower.value) {
633 local->hw.conf.power_level = data->txpower.value;
634 need_reconfig = 1;
635 }
636 if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
637 local->hw.conf.radio_enabled = !(data->txpower.disabled);
638 need_reconfig = 1;
639 }
640 if (need_reconfig) {
641 ieee80211_hw_config(local);
642 /* The return value of hw_config is not of big interest here,
643 * as it doesn't say that it failed because of _this_ config
644 * change or something else. Ignore it. */
645 }
646
647 return 0;
648 }
649
650 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
651 struct iw_request_info *info,
652 union iwreq_data *data, char *extra)
653 {
654 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
655
656 data->txpower.fixed = 1;
657 data->txpower.disabled = !(local->hw.conf.radio_enabled);
658 data->txpower.value = local->hw.conf.power_level;
659 data->txpower.flags = IW_TXPOW_DBM;
660
661 return 0;
662 }
663
664 static int ieee80211_ioctl_siwrts(struct net_device *dev,
665 struct iw_request_info *info,
666 struct iw_param *rts, char *extra)
667 {
668 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
669
670 if (rts->disabled)
671 local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
672 else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
673 return -EINVAL;
674 else
675 local->rts_threshold = rts->value;
676
677 /* If the wlan card performs RTS/CTS in hardware/firmware,
678 * configure it here */
679
680 if (local->ops->set_rts_threshold)
681 local->ops->set_rts_threshold(local_to_hw(local),
682 local->rts_threshold);
683
684 return 0;
685 }
686
687 static int ieee80211_ioctl_giwrts(struct net_device *dev,
688 struct iw_request_info *info,
689 struct iw_param *rts, char *extra)
690 {
691 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
692
693 rts->value = local->rts_threshold;
694 rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
695 rts->fixed = 1;
696
697 return 0;
698 }
699
700
701 static int ieee80211_ioctl_siwfrag(struct net_device *dev,
702 struct iw_request_info *info,
703 struct iw_param *frag, char *extra)
704 {
705 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
706
707 if (frag->disabled)
708 local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
709 else if (frag->value < 256 ||
710 frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
711 return -EINVAL;
712 else {
713 /* Fragment length must be even, so strip LSB. */
714 local->fragmentation_threshold = frag->value & ~0x1;
715 }
716
717 /* If the wlan card performs fragmentation in hardware/firmware,
718 * configure it here */
719
720 if (local->ops->set_frag_threshold)
721 local->ops->set_frag_threshold(
722 local_to_hw(local),
723 local->fragmentation_threshold);
724
725 return 0;
726 }
727
728 static int ieee80211_ioctl_giwfrag(struct net_device *dev,
729 struct iw_request_info *info,
730 struct iw_param *frag, char *extra)
731 {
732 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
733
734 frag->value = local->fragmentation_threshold;
735 frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
736 frag->fixed = 1;
737
738 return 0;
739 }
740
741
742 static int ieee80211_ioctl_siwretry(struct net_device *dev,
743 struct iw_request_info *info,
744 struct iw_param *retry, char *extra)
745 {
746 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
747
748 if (retry->disabled ||
749 (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
750 return -EINVAL;
751
752 if (retry->flags & IW_RETRY_MAX)
753 local->long_retry_limit = retry->value;
754 else if (retry->flags & IW_RETRY_MIN)
755 local->short_retry_limit = retry->value;
756 else {
757 local->long_retry_limit = retry->value;
758 local->short_retry_limit = retry->value;
759 }
760
761 if (local->ops->set_retry_limit) {
762 return local->ops->set_retry_limit(
763 local_to_hw(local),
764 local->short_retry_limit,
765 local->long_retry_limit);
766 }
767
768 return 0;
769 }
770
771
772 static int ieee80211_ioctl_giwretry(struct net_device *dev,
773 struct iw_request_info *info,
774 struct iw_param *retry, char *extra)
775 {
776 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
777
778 retry->disabled = 0;
779 if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
780 /* first return min value, iwconfig will ask max value
781 * later if needed */
782 retry->flags |= IW_RETRY_LIMIT;
783 retry->value = local->short_retry_limit;
784 if (local->long_retry_limit != local->short_retry_limit)
785 retry->flags |= IW_RETRY_MIN;
786 return 0;
787 }
788 if (retry->flags & IW_RETRY_MAX) {
789 retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
790 retry->value = local->long_retry_limit;
791 }
792
793 return 0;
794 }
795
796 static int ieee80211_ioctl_siwmlme(struct net_device *dev,
797 struct iw_request_info *info,
798 struct iw_point *data, char *extra)
799 {
800 struct ieee80211_sub_if_data *sdata;
801 struct iw_mlme *mlme = (struct iw_mlme *) extra;
802
803 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
804 if (sdata->type != IEEE80211_IF_TYPE_STA &&
805 sdata->type != IEEE80211_IF_TYPE_IBSS)
806 return -EINVAL;
807
808 switch (mlme->cmd) {
809 case IW_MLME_DEAUTH:
810 /* TODO: mlme->addr.sa_data */
811 return ieee80211_sta_deauthenticate(dev, mlme->reason_code);
812 case IW_MLME_DISASSOC:
813 /* TODO: mlme->addr.sa_data */
814 return ieee80211_sta_disassociate(dev, mlme->reason_code);
815 default:
816 return -EOPNOTSUPP;
817 }
818 }
819
820
821 static int ieee80211_ioctl_siwencode(struct net_device *dev,
822 struct iw_request_info *info,
823 struct iw_point *erq, char *keybuf)
824 {
825 struct ieee80211_sub_if_data *sdata;
826 int idx, i, alg = ALG_WEP;
827 u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
828 int remove = 0;
829
830 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
831
832 idx = erq->flags & IW_ENCODE_INDEX;
833 if (idx == 0) {
834 if (sdata->default_key)
835 for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
836 if (sdata->default_key == sdata->keys[i]) {
837 idx = i;
838 break;
839 }
840 }
841 } else if (idx < 1 || idx > 4)
842 return -EINVAL;
843 else
844 idx--;
845
846 if (erq->flags & IW_ENCODE_DISABLED)
847 remove = 1;
848 else if (erq->length == 0) {
849 /* No key data - just set the default TX key index */
850 ieee80211_set_default_key(sdata, idx);
851 return 0;
852 }
853
854 return ieee80211_set_encryption(
855 dev, bcaddr,
856 idx, alg, remove,
857 !sdata->default_key,
858 keybuf, erq->length);
859 }
860
861
862 static int ieee80211_ioctl_giwencode(struct net_device *dev,
863 struct iw_request_info *info,
864 struct iw_point *erq, char *key)
865 {
866 struct ieee80211_sub_if_data *sdata;
867 int idx, i;
868
869 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
870
871 idx = erq->flags & IW_ENCODE_INDEX;
872 if (idx < 1 || idx > 4) {
873 idx = -1;
874 if (!sdata->default_key)
875 idx = 0;
876 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
877 if (sdata->default_key == sdata->keys[i]) {
878 idx = i;
879 break;
880 }
881 }
882 if (idx < 0)
883 return -EINVAL;
884 } else
885 idx--;
886
887 erq->flags = idx + 1;
888
889 if (!sdata->keys[idx]) {
890 erq->length = 0;
891 erq->flags |= IW_ENCODE_DISABLED;
892 return 0;
893 }
894
895 memcpy(key, sdata->keys[idx]->conf.key,
896 min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
897 erq->length = sdata->keys[idx]->conf.keylen;
898 erq->flags |= IW_ENCODE_ENABLED;
899
900 return 0;
901 }
902
903 static int ieee80211_ioctl_siwauth(struct net_device *dev,
904 struct iw_request_info *info,
905 struct iw_param *data, char *extra)
906 {
907 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
908 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
909 int ret = 0;
910
911 switch (data->flags & IW_AUTH_INDEX) {
912 case IW_AUTH_WPA_VERSION:
913 case IW_AUTH_CIPHER_PAIRWISE:
914 case IW_AUTH_CIPHER_GROUP:
915 case IW_AUTH_WPA_ENABLED:
916 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
917 break;
918 case IW_AUTH_KEY_MGMT:
919 if (sdata->type != IEEE80211_IF_TYPE_STA)
920 ret = -EINVAL;
921 else {
922 /*
923 * Key management was set by wpa_supplicant,
924 * we only need this to associate to a network
925 * that has privacy enabled regardless of not
926 * having a key.
927 */
928 sdata->u.sta.key_management_enabled = !!data->value;
929 }
930 break;
931 case IW_AUTH_80211_AUTH_ALG:
932 if (sdata->type == IEEE80211_IF_TYPE_STA ||
933 sdata->type == IEEE80211_IF_TYPE_IBSS)
934 sdata->u.sta.auth_algs = data->value;
935 else
936 ret = -EOPNOTSUPP;
937 break;
938 case IW_AUTH_PRIVACY_INVOKED:
939 if (local->ops->set_privacy_invoked)
940 ret = local->ops->set_privacy_invoked(
941 local_to_hw(local), data->value);
942 break;
943 default:
944 ret = -EOPNOTSUPP;
945 break;
946 }
947 return ret;
948 }
949
950 /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
951 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
952 {
953 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
954 struct iw_statistics *wstats = &local->wstats;
955 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
956 struct sta_info *sta = NULL;
957
958 if (sdata->type == IEEE80211_IF_TYPE_STA ||
959 sdata->type == IEEE80211_IF_TYPE_IBSS)
960 sta = sta_info_get(local, sdata->u.sta.bssid);
961 if (!sta) {
962 wstats->discard.fragment = 0;
963 wstats->discard.misc = 0;
964 wstats->qual.qual = 0;
965 wstats->qual.level = 0;
966 wstats->qual.noise = 0;
967 wstats->qual.updated = IW_QUAL_ALL_INVALID;
968 } else {
969 wstats->qual.level = sta->last_rssi;
970 wstats->qual.qual = sta->last_signal;
971 wstats->qual.noise = sta->last_noise;
972 wstats->qual.updated = local->wstats_flags;
973 sta_info_put(sta);
974 }
975 return wstats;
976 }
977
978 static int ieee80211_ioctl_giwauth(struct net_device *dev,
979 struct iw_request_info *info,
980 struct iw_param *data, char *extra)
981 {
982 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
983 int ret = 0;
984
985 switch (data->flags & IW_AUTH_INDEX) {
986 case IW_AUTH_80211_AUTH_ALG:
987 if (sdata->type == IEEE80211_IF_TYPE_STA ||
988 sdata->type == IEEE80211_IF_TYPE_IBSS)
989 data->value = sdata->u.sta.auth_algs;
990 else
991 ret = -EOPNOTSUPP;
992 break;
993 default:
994 ret = -EOPNOTSUPP;
995 break;
996 }
997 return ret;
998 }
999
1000
1001 static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
1002 struct iw_request_info *info,
1003 struct iw_point *erq, char *extra)
1004 {
1005 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1006 struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
1007 int uninitialized_var(alg), idx, i, remove = 0;
1008
1009 switch (ext->alg) {
1010 case IW_ENCODE_ALG_NONE:
1011 remove = 1;
1012 break;
1013 case IW_ENCODE_ALG_WEP:
1014 alg = ALG_WEP;
1015 break;
1016 case IW_ENCODE_ALG_TKIP:
1017 alg = ALG_TKIP;
1018 break;
1019 case IW_ENCODE_ALG_CCMP:
1020 alg = ALG_CCMP;
1021 break;
1022 default:
1023 return -EOPNOTSUPP;
1024 }
1025
1026 if (erq->flags & IW_ENCODE_DISABLED)
1027 remove = 1;
1028
1029 idx = erq->flags & IW_ENCODE_INDEX;
1030 if (idx < 1 || idx > 4) {
1031 idx = -1;
1032 if (!sdata->default_key)
1033 idx = 0;
1034 else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
1035 if (sdata->default_key == sdata->keys[i]) {
1036 idx = i;
1037 break;
1038 }
1039 }
1040 if (idx < 0)
1041 return -EINVAL;
1042 } else
1043 idx--;
1044
1045 return ieee80211_set_encryption(dev, ext->addr.sa_data, idx, alg,
1046 remove,
1047 ext->ext_flags &
1048 IW_ENCODE_EXT_SET_TX_KEY,
1049 ext->key, ext->key_len);
1050 }
1051
1052
1053 /* Structures to export the Wireless Handlers */
1054
1055 static const iw_handler ieee80211_handler[] =
1056 {
1057 (iw_handler) NULL, /* SIOCSIWCOMMIT */
1058 (iw_handler) ieee80211_ioctl_giwname, /* SIOCGIWNAME */
1059 (iw_handler) NULL, /* SIOCSIWNWID */
1060 (iw_handler) NULL, /* SIOCGIWNWID */
1061 (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
1062 (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
1063 (iw_handler) ieee80211_ioctl_siwmode, /* SIOCSIWMODE */
1064 (iw_handler) ieee80211_ioctl_giwmode, /* SIOCGIWMODE */
1065 (iw_handler) NULL, /* SIOCSIWSENS */
1066 (iw_handler) NULL, /* SIOCGIWSENS */
1067 (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
1068 (iw_handler) ieee80211_ioctl_giwrange, /* SIOCGIWRANGE */
1069 (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
1070 (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
1071 (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
1072 (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
1073 (iw_handler) NULL, /* SIOCSIWSPY */
1074 (iw_handler) NULL, /* SIOCGIWSPY */
1075 (iw_handler) NULL, /* SIOCSIWTHRSPY */
1076 (iw_handler) NULL, /* SIOCGIWTHRSPY */
1077 (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
1078 (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
1079 (iw_handler) ieee80211_ioctl_siwmlme, /* SIOCSIWMLME */
1080 (iw_handler) NULL, /* SIOCGIWAPLIST */
1081 (iw_handler) ieee80211_ioctl_siwscan, /* SIOCSIWSCAN */
1082 (iw_handler) ieee80211_ioctl_giwscan, /* SIOCGIWSCAN */
1083 (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
1084 (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
1085 (iw_handler) NULL, /* SIOCSIWNICKN */
1086 (iw_handler) NULL, /* SIOCGIWNICKN */
1087 (iw_handler) NULL, /* -- hole -- */
1088 (iw_handler) NULL, /* -- hole -- */
1089 (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
1090 (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
1091 (iw_handler) ieee80211_ioctl_siwrts, /* SIOCSIWRTS */
1092 (iw_handler) ieee80211_ioctl_giwrts, /* SIOCGIWRTS */
1093 (iw_handler) ieee80211_ioctl_siwfrag, /* SIOCSIWFRAG */
1094 (iw_handler) ieee80211_ioctl_giwfrag, /* SIOCGIWFRAG */
1095 (iw_handler) ieee80211_ioctl_siwtxpower, /* SIOCSIWTXPOW */
1096 (iw_handler) ieee80211_ioctl_giwtxpower, /* SIOCGIWTXPOW */
1097 (iw_handler) ieee80211_ioctl_siwretry, /* SIOCSIWRETRY */
1098 (iw_handler) ieee80211_ioctl_giwretry, /* SIOCGIWRETRY */
1099 (iw_handler) ieee80211_ioctl_siwencode, /* SIOCSIWENCODE */
1100 (iw_handler) ieee80211_ioctl_giwencode, /* SIOCGIWENCODE */
1101 (iw_handler) NULL, /* SIOCSIWPOWER */
1102 (iw_handler) NULL, /* SIOCGIWPOWER */
1103 (iw_handler) NULL, /* -- hole -- */
1104 (iw_handler) NULL, /* -- hole -- */
1105 (iw_handler) ieee80211_ioctl_siwgenie, /* SIOCSIWGENIE */
1106 (iw_handler) NULL, /* SIOCGIWGENIE */
1107 (iw_handler) ieee80211_ioctl_siwauth, /* SIOCSIWAUTH */
1108 (iw_handler) ieee80211_ioctl_giwauth, /* SIOCGIWAUTH */
1109 (iw_handler) ieee80211_ioctl_siwencodeext, /* SIOCSIWENCODEEXT */
1110 (iw_handler) NULL, /* SIOCGIWENCODEEXT */
1111 (iw_handler) NULL, /* SIOCSIWPMKSA */
1112 (iw_handler) NULL, /* -- hole -- */
1113 };
1114
1115 const struct iw_handler_def ieee80211_iw_handler_def =
1116 {
1117 .num_standard = ARRAY_SIZE(ieee80211_handler),
1118 .standard = (iw_handler *) ieee80211_handler,
1119 .get_wireless_stats = ieee80211_get_wireless_stats,
1120 };