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.
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"
30 static int ieee80211_ioctl_siwgenie(struct net_device *dev,
31 struct iw_request_info *info,
32 struct iw_point *data, char *extra)
34 struct ieee80211_sub_if_data *sdata;
36 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
38 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
39 int ret = ieee80211_sta_set_extra_ie(sdata, extra, data->length);
40 if (ret && ret != -EALREADY)
42 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
43 sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
44 sdata->u.mgd.flags &= ~IEEE80211_STA_CONTROL_PORT;
46 ieee80211_sta_req_auth(sdata);
53 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
54 struct iw_request_info *info,
55 struct iw_freq *freq, char *extra)
57 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
59 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
60 return cfg80211_ibss_wext_siwfreq(dev, info, freq, extra);
61 else if (sdata->vif.type == NL80211_IFTYPE_STATION)
62 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
64 /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
67 if (sdata->vif.type == NL80211_IFTYPE_STATION)
69 IEEE80211_STA_AUTO_CHANNEL_SEL;
72 return ieee80211_set_freq(sdata,
73 ieee80211_channel_to_frequency(freq->m));
76 for (i = 0; i < freq->e; i++)
79 return ieee80211_set_freq(sdata, freq->m / div);
86 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
87 struct iw_request_info *info,
88 struct iw_freq *freq, char *extra)
90 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
91 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
93 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
94 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
96 freq->m = local->oper_channel->center_freq;
103 static int ieee80211_ioctl_siwessid(struct net_device *dev,
104 struct iw_request_info *info,
105 struct iw_point *data, char *ssid)
107 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
108 size_t len = data->length;
111 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
112 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
114 /* iwconfig uses nul termination in SSID.. */
115 if (len > 0 && ssid[len - 1] == '\0')
118 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
120 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
122 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_SSID_SEL;
124 ret = ieee80211_sta_set_ssid(sdata, ssid, len);
128 sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
129 sdata->u.mgd.flags &= ~IEEE80211_STA_CONTROL_PORT;
130 ieee80211_sta_req_auth(sdata);
138 static int ieee80211_ioctl_giwessid(struct net_device *dev,
139 struct iw_request_info *info,
140 struct iw_point *data, char *ssid)
143 struct ieee80211_sub_if_data *sdata;
145 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
147 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
148 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
150 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
151 int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
164 static int ieee80211_ioctl_siwap(struct net_device *dev,
165 struct iw_request_info *info,
166 struct sockaddr *ap_addr, char *extra)
168 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
170 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
171 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
173 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
176 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
177 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
178 IEEE80211_STA_AUTO_CHANNEL_SEL;
179 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
180 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
182 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
183 ret = ieee80211_sta_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
186 sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
187 sdata->u.mgd.flags &= ~IEEE80211_STA_CONTROL_PORT;
188 ieee80211_sta_req_auth(sdata);
190 } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
192 * If it is necessary to update the WDS peer address
193 * while the interface is running, then we need to do
194 * more work here, namely if it is running we need to
195 * add a new and remove the old STA entry, this is
196 * normally handled by _open() and _stop().
198 if (netif_running(dev))
201 memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
211 static int ieee80211_ioctl_giwap(struct net_device *dev,
212 struct iw_request_info *info,
213 struct sockaddr *ap_addr, char *extra)
215 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
217 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
218 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
220 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
221 if (sdata->u.mgd.state == IEEE80211_STA_MLME_ASSOCIATED) {
222 ap_addr->sa_family = ARPHRD_ETHER;
223 memcpy(&ap_addr->sa_data, sdata->u.mgd.bssid, ETH_ALEN);
225 memset(&ap_addr->sa_data, 0, ETH_ALEN);
227 } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
228 ap_addr->sa_family = ARPHRD_ETHER;
229 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
237 static int ieee80211_ioctl_siwrate(struct net_device *dev,
238 struct iw_request_info *info,
239 struct iw_param *rate, char *extra)
241 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
242 int i, err = -EINVAL;
243 u32 target_rate = rate->value / 100000;
244 struct ieee80211_sub_if_data *sdata;
245 struct ieee80211_supported_band *sband;
247 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
249 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
251 /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
252 * target_rate = X, rate->fixed = 1 means only rate X
253 * target_rate = X, rate->fixed = 0 means all rates <= X */
254 sdata->max_ratectrl_rateidx = -1;
255 sdata->force_unicast_rateidx = -1;
259 for (i=0; i< sband->n_bitrates; i++) {
260 struct ieee80211_rate *brate = &sband->bitrates[i];
261 int this_rate = brate->bitrate;
263 if (target_rate == this_rate) {
264 sdata->max_ratectrl_rateidx = i;
266 sdata->force_unicast_rateidx = i;
274 static int ieee80211_ioctl_giwrate(struct net_device *dev,
275 struct iw_request_info *info,
276 struct iw_param *rate, char *extra)
278 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
279 struct sta_info *sta;
280 struct ieee80211_sub_if_data *sdata;
281 struct ieee80211_supported_band *sband;
283 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
285 if (sdata->vif.type != NL80211_IFTYPE_STATION)
288 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
292 sta = sta_info_get(local, sdata->u.mgd.bssid);
294 if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
295 rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
304 rate->value *= 100000;
309 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
310 struct iw_request_info *info,
311 union iwreq_data *data, char *extra)
313 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
314 struct ieee80211_channel* chan = local->hw.conf.channel;
316 u32 reconf_flags = 0;
319 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
321 if (data->txpower.flags & IW_TXPOW_RANGE)
326 /* only change when not disabling */
327 if (!data->txpower.disabled) {
328 if (data->txpower.fixed) {
329 if (data->txpower.value < 0)
331 new_power_level = data->txpower.value;
333 * Debatable, but we cannot do a fixed power
334 * level above the regulatory constraint.
335 * Use "iwconfig wlan0 txpower 15dBm" instead.
337 if (new_power_level > chan->max_power)
341 * Automatic power level setting, max being the value
342 * passed in from userland.
344 if (data->txpower.value < 0)
345 new_power_level = -1;
347 new_power_level = data->txpower.value;
353 * ieee80211_hw_config() will limit to the channel's
354 * max power and possibly power constraint from AP.
356 local->user_power_level = new_power_level;
359 if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
360 local->hw.conf.radio_enabled = !(data->txpower.disabled);
361 reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
362 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
365 if (reconf || reconf_flags)
366 ieee80211_hw_config(local, reconf_flags);
371 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
372 struct iw_request_info *info,
373 union iwreq_data *data, char *extra)
375 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
377 data->txpower.fixed = 1;
378 data->txpower.disabled = !(local->hw.conf.radio_enabled);
379 data->txpower.value = local->hw.conf.power_level;
380 data->txpower.flags = IW_TXPOW_DBM;
385 static int ieee80211_ioctl_siwpower(struct net_device *dev,
386 struct iw_request_info *info,
387 struct iw_param *wrq,
390 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
391 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
392 struct ieee80211_conf *conf = &local->hw.conf;
396 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
399 if (sdata->vif.type != NL80211_IFTYPE_STATION)
408 switch (wrq->flags & IW_POWER_MODE) {
409 case IW_POWER_ON: /* If not specified */
410 case IW_POWER_MODE: /* If set all mask */
411 case IW_POWER_ALL_R: /* If explicitely state all */
414 default: /* Otherwise we ignore */
418 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
421 if (wrq->flags & IW_POWER_TIMEOUT)
422 timeout = wrq->value / 1000;
425 if (ps == sdata->u.mgd.powersave && timeout == conf->dynamic_ps_timeout)
428 sdata->u.mgd.powersave = ps;
429 conf->dynamic_ps_timeout = timeout;
431 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
432 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
434 ieee80211_recalc_ps(local, -1);
439 static int ieee80211_ioctl_giwpower(struct net_device *dev,
440 struct iw_request_info *info,
441 union iwreq_data *wrqu,
444 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
446 wrqu->power.disabled = !sdata->u.mgd.powersave;
451 static int ieee80211_ioctl_siwauth(struct net_device *dev,
452 struct iw_request_info *info,
453 struct iw_param *data, char *extra)
455 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
458 switch (data->flags & IW_AUTH_INDEX) {
459 case IW_AUTH_WPA_VERSION:
460 case IW_AUTH_CIPHER_GROUP:
461 case IW_AUTH_WPA_ENABLED:
462 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
463 case IW_AUTH_KEY_MGMT:
464 case IW_AUTH_CIPHER_GROUP_MGMT:
466 case IW_AUTH_CIPHER_PAIRWISE:
467 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
468 if (data->value & (IW_AUTH_CIPHER_WEP40 |
469 IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_TKIP))
470 sdata->u.mgd.flags |=
471 IEEE80211_STA_TKIP_WEP_USED;
473 sdata->u.mgd.flags &=
474 ~IEEE80211_STA_TKIP_WEP_USED;
477 case IW_AUTH_DROP_UNENCRYPTED:
478 sdata->drop_unencrypted = !!data->value;
480 case IW_AUTH_PRIVACY_INVOKED:
481 if (sdata->vif.type != NL80211_IFTYPE_STATION)
484 sdata->u.mgd.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
486 * Privacy invoked by wpa_supplicant, store the
487 * value and allow associating to a protected
488 * network without having a key up front.
491 sdata->u.mgd.flags |=
492 IEEE80211_STA_PRIVACY_INVOKED;
495 case IW_AUTH_80211_AUTH_ALG:
496 if (sdata->vif.type == NL80211_IFTYPE_STATION)
497 sdata->u.mgd.auth_algs = data->value;
502 if (!(sdata->local->hw.flags & IEEE80211_HW_MFP_CAPABLE)) {
506 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
507 switch (data->value) {
508 case IW_AUTH_MFP_DISABLED:
509 sdata->u.mgd.mfp = IEEE80211_MFP_DISABLED;
511 case IW_AUTH_MFP_OPTIONAL:
512 sdata->u.mgd.mfp = IEEE80211_MFP_OPTIONAL;
514 case IW_AUTH_MFP_REQUIRED:
515 sdata->u.mgd.mfp = IEEE80211_MFP_REQUIRED;
530 /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
531 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
533 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
534 struct iw_statistics *wstats = &local->wstats;
535 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
536 struct sta_info *sta = NULL;
540 if (sdata->vif.type == NL80211_IFTYPE_STATION)
541 sta = sta_info_get(local, sdata->u.mgd.bssid);
544 wstats->discard.fragment = 0;
545 wstats->discard.misc = 0;
546 wstats->qual.qual = 0;
547 wstats->qual.level = 0;
548 wstats->qual.noise = 0;
549 wstats->qual.updated = IW_QUAL_ALL_INVALID;
551 wstats->qual.updated = 0;
553 * mirror what cfg80211 does for iwrange/scan results,
554 * otherwise userspace gets confused.
556 if (local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
557 IEEE80211_HW_SIGNAL_DBM)) {
558 wstats->qual.updated |= IW_QUAL_LEVEL_UPDATED;
559 wstats->qual.updated |= IW_QUAL_QUAL_UPDATED;
561 wstats->qual.updated |= IW_QUAL_LEVEL_INVALID;
562 wstats->qual.updated |= IW_QUAL_QUAL_INVALID;
565 if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
566 wstats->qual.level = sta->last_signal;
567 wstats->qual.qual = sta->last_signal;
568 } else if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
569 int sig = sta->last_signal;
571 wstats->qual.updated |= IW_QUAL_DBM;
572 wstats->qual.level = sig;
577 wstats->qual.qual = sig + 110;
580 if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
582 * This assumes that if driver reports noise, it also
583 * reports signal in dBm.
585 wstats->qual.noise = sta->last_noise;
586 wstats->qual.updated |= IW_QUAL_NOISE_UPDATED;
588 wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
597 static int ieee80211_ioctl_giwauth(struct net_device *dev,
598 struct iw_request_info *info,
599 struct iw_param *data, char *extra)
601 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
604 switch (data->flags & IW_AUTH_INDEX) {
605 case IW_AUTH_80211_AUTH_ALG:
606 if (sdata->vif.type == NL80211_IFTYPE_STATION)
607 data->value = sdata->u.mgd.auth_algs;
619 /* Structures to export the Wireless Handlers */
621 static const iw_handler ieee80211_handler[] =
623 (iw_handler) NULL, /* SIOCSIWCOMMIT */
624 (iw_handler) cfg80211_wext_giwname, /* SIOCGIWNAME */
625 (iw_handler) NULL, /* SIOCSIWNWID */
626 (iw_handler) NULL, /* SIOCGIWNWID */
627 (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
628 (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
629 (iw_handler) cfg80211_wext_siwmode, /* SIOCSIWMODE */
630 (iw_handler) cfg80211_wext_giwmode, /* SIOCGIWMODE */
631 (iw_handler) NULL, /* SIOCSIWSENS */
632 (iw_handler) NULL, /* SIOCGIWSENS */
633 (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
634 (iw_handler) cfg80211_wext_giwrange, /* SIOCGIWRANGE */
635 (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
636 (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
637 (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
638 (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
639 (iw_handler) NULL, /* SIOCSIWSPY */
640 (iw_handler) NULL, /* SIOCGIWSPY */
641 (iw_handler) NULL, /* SIOCSIWTHRSPY */
642 (iw_handler) NULL, /* SIOCGIWTHRSPY */
643 (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
644 (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
645 (iw_handler) cfg80211_wext_siwmlme, /* SIOCSIWMLME */
646 (iw_handler) NULL, /* SIOCGIWAPLIST */
647 (iw_handler) cfg80211_wext_siwscan, /* SIOCSIWSCAN */
648 (iw_handler) cfg80211_wext_giwscan, /* SIOCGIWSCAN */
649 (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
650 (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
651 (iw_handler) NULL, /* SIOCSIWNICKN */
652 (iw_handler) NULL, /* SIOCGIWNICKN */
653 (iw_handler) NULL, /* -- hole -- */
654 (iw_handler) NULL, /* -- hole -- */
655 (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
656 (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
657 (iw_handler) cfg80211_wext_siwrts, /* SIOCSIWRTS */
658 (iw_handler) cfg80211_wext_giwrts, /* SIOCGIWRTS */
659 (iw_handler) cfg80211_wext_siwfrag, /* SIOCSIWFRAG */
660 (iw_handler) cfg80211_wext_giwfrag, /* SIOCGIWFRAG */
661 (iw_handler) ieee80211_ioctl_siwtxpower, /* SIOCSIWTXPOW */
662 (iw_handler) ieee80211_ioctl_giwtxpower, /* SIOCGIWTXPOW */
663 (iw_handler) cfg80211_wext_siwretry, /* SIOCSIWRETRY */
664 (iw_handler) cfg80211_wext_giwretry, /* SIOCGIWRETRY */
665 (iw_handler) cfg80211_wext_siwencode, /* SIOCSIWENCODE */
666 (iw_handler) cfg80211_wext_giwencode, /* SIOCGIWENCODE */
667 (iw_handler) ieee80211_ioctl_siwpower, /* SIOCSIWPOWER */
668 (iw_handler) ieee80211_ioctl_giwpower, /* SIOCGIWPOWER */
669 (iw_handler) NULL, /* -- hole -- */
670 (iw_handler) NULL, /* -- hole -- */
671 (iw_handler) ieee80211_ioctl_siwgenie, /* SIOCSIWGENIE */
672 (iw_handler) NULL, /* SIOCGIWGENIE */
673 (iw_handler) ieee80211_ioctl_siwauth, /* SIOCSIWAUTH */
674 (iw_handler) ieee80211_ioctl_giwauth, /* SIOCGIWAUTH */
675 (iw_handler) cfg80211_wext_siwencodeext, /* SIOCSIWENCODEEXT */
676 (iw_handler) NULL, /* SIOCGIWENCODEEXT */
677 (iw_handler) NULL, /* SIOCSIWPMKSA */
678 (iw_handler) NULL, /* -- hole -- */
681 const struct iw_handler_def ieee80211_iw_handler_def =
683 .num_standard = ARRAY_SIZE(ieee80211_handler),
684 .standard = (iw_handler *) ieee80211_handler,
685 .get_wireless_stats = ieee80211_get_wireless_stats,