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);
42 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
43 sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
44 ieee80211_sta_req_auth(sdata);
51 static int ieee80211_ioctl_siwfreq(struct net_device *dev,
52 struct iw_request_info *info,
53 struct iw_freq *freq, char *extra)
55 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
57 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
58 return cfg80211_ibss_wext_siwfreq(dev, info, freq, extra);
59 else if (sdata->vif.type == NL80211_IFTYPE_STATION)
60 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
62 /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
65 if (sdata->vif.type == NL80211_IFTYPE_STATION)
67 IEEE80211_STA_AUTO_CHANNEL_SEL;
70 return ieee80211_set_freq(sdata,
71 ieee80211_channel_to_frequency(freq->m));
74 for (i = 0; i < freq->e; i++)
77 return ieee80211_set_freq(sdata, freq->m / div);
84 static int ieee80211_ioctl_giwfreq(struct net_device *dev,
85 struct iw_request_info *info,
86 struct iw_freq *freq, char *extra)
88 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
89 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
91 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
92 return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
94 freq->m = local->oper_channel->center_freq;
101 static int ieee80211_ioctl_siwessid(struct net_device *dev,
102 struct iw_request_info *info,
103 struct iw_point *data, char *ssid)
105 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
106 size_t len = data->length;
109 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
110 return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
112 /* iwconfig uses nul termination in SSID.. */
113 if (len > 0 && ssid[len - 1] == '\0')
116 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
118 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
120 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_SSID_SEL;
122 ret = ieee80211_sta_set_ssid(sdata, ssid, len);
126 sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
127 ieee80211_sta_req_auth(sdata);
135 static int ieee80211_ioctl_giwessid(struct net_device *dev,
136 struct iw_request_info *info,
137 struct iw_point *data, char *ssid)
140 struct ieee80211_sub_if_data *sdata;
142 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
144 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
145 return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
147 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
148 int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
161 static int ieee80211_ioctl_siwap(struct net_device *dev,
162 struct iw_request_info *info,
163 struct sockaddr *ap_addr, char *extra)
165 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
167 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
168 return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
170 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
173 if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
174 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
175 IEEE80211_STA_AUTO_CHANNEL_SEL;
176 else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
177 sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
179 sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
180 ret = ieee80211_sta_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
183 sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
184 ieee80211_sta_req_auth(sdata);
186 } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
188 * If it is necessary to update the WDS peer address
189 * while the interface is running, then we need to do
190 * more work here, namely if it is running we need to
191 * add a new and remove the old STA entry, this is
192 * normally handled by _open() and _stop().
194 if (netif_running(dev))
197 memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
207 static int ieee80211_ioctl_giwap(struct net_device *dev,
208 struct iw_request_info *info,
209 struct sockaddr *ap_addr, char *extra)
211 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
213 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
214 return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
216 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
217 if (sdata->u.mgd.state == IEEE80211_STA_MLME_ASSOCIATED) {
218 ap_addr->sa_family = ARPHRD_ETHER;
219 memcpy(&ap_addr->sa_data, sdata->u.mgd.bssid, ETH_ALEN);
221 memset(&ap_addr->sa_data, 0, ETH_ALEN);
223 } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
224 ap_addr->sa_family = ARPHRD_ETHER;
225 memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
233 static int ieee80211_ioctl_siwrate(struct net_device *dev,
234 struct iw_request_info *info,
235 struct iw_param *rate, char *extra)
237 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
238 int i, err = -EINVAL;
239 u32 target_rate = rate->value / 100000;
240 struct ieee80211_sub_if_data *sdata;
241 struct ieee80211_supported_band *sband;
243 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
245 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
247 /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
248 * target_rate = X, rate->fixed = 1 means only rate X
249 * target_rate = X, rate->fixed = 0 means all rates <= X */
250 sdata->max_ratectrl_rateidx = -1;
251 sdata->force_unicast_rateidx = -1;
255 for (i=0; i< sband->n_bitrates; i++) {
256 struct ieee80211_rate *brate = &sband->bitrates[i];
257 int this_rate = brate->bitrate;
259 if (target_rate == this_rate) {
260 sdata->max_ratectrl_rateidx = i;
262 sdata->force_unicast_rateidx = i;
270 static int ieee80211_ioctl_giwrate(struct net_device *dev,
271 struct iw_request_info *info,
272 struct iw_param *rate, char *extra)
274 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
275 struct sta_info *sta;
276 struct ieee80211_sub_if_data *sdata;
277 struct ieee80211_supported_band *sband;
279 sdata = IEEE80211_DEV_TO_SUB_IF(dev);
281 if (sdata->vif.type != NL80211_IFTYPE_STATION)
284 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
288 sta = sta_info_get(local, sdata->u.mgd.bssid);
290 if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
291 rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
300 rate->value *= 100000;
305 static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
306 struct iw_request_info *info,
307 union iwreq_data *data, char *extra)
309 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
310 struct ieee80211_channel* chan = local->hw.conf.channel;
312 u32 reconf_flags = 0;
315 if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
317 if (data->txpower.flags & IW_TXPOW_RANGE)
322 /* only change when not disabling */
323 if (!data->txpower.disabled) {
324 if (data->txpower.fixed) {
325 if (data->txpower.value < 0)
327 new_power_level = data->txpower.value;
329 * Debatable, but we cannot do a fixed power
330 * level above the regulatory constraint.
331 * Use "iwconfig wlan0 txpower 15dBm" instead.
333 if (new_power_level > chan->max_power)
337 * Automatic power level setting, max being the value
338 * passed in from userland.
340 if (data->txpower.value < 0)
341 new_power_level = -1;
343 new_power_level = data->txpower.value;
349 * ieee80211_hw_config() will limit to the channel's
350 * max power and possibly power constraint from AP.
352 local->user_power_level = new_power_level;
355 if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
356 local->hw.conf.radio_enabled = !(data->txpower.disabled);
357 reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
358 ieee80211_led_radio(local, local->hw.conf.radio_enabled);
361 if (reconf || reconf_flags)
362 ieee80211_hw_config(local, reconf_flags);
367 static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
368 struct iw_request_info *info,
369 union iwreq_data *data, char *extra)
371 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
373 data->txpower.fixed = 1;
374 data->txpower.disabled = !(local->hw.conf.radio_enabled);
375 data->txpower.value = local->hw.conf.power_level;
376 data->txpower.flags = IW_TXPOW_DBM;
381 static int ieee80211_ioctl_siwpower(struct net_device *dev,
382 struct iw_request_info *info,
383 struct iw_param *wrq,
386 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
387 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
388 struct ieee80211_conf *conf = &local->hw.conf;
392 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
395 if (sdata->vif.type != NL80211_IFTYPE_STATION)
404 switch (wrq->flags & IW_POWER_MODE) {
405 case IW_POWER_ON: /* If not specified */
406 case IW_POWER_MODE: /* If set all mask */
407 case IW_POWER_ALL_R: /* If explicitely state all */
410 default: /* Otherwise we ignore */
414 if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
417 if (wrq->flags & IW_POWER_TIMEOUT)
418 timeout = wrq->value / 1000;
421 if (ps == sdata->u.mgd.powersave && timeout == conf->dynamic_ps_timeout)
424 sdata->u.mgd.powersave = ps;
425 conf->dynamic_ps_timeout = timeout;
427 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
428 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
430 ieee80211_recalc_ps(local, -1);
435 static int ieee80211_ioctl_giwpower(struct net_device *dev,
436 struct iw_request_info *info,
437 union iwreq_data *wrqu,
440 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
442 wrqu->power.disabled = !sdata->u.mgd.powersave;
447 static int ieee80211_ioctl_siwauth(struct net_device *dev,
448 struct iw_request_info *info,
449 struct iw_param *data, char *extra)
451 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
454 switch (data->flags & IW_AUTH_INDEX) {
455 case IW_AUTH_WPA_VERSION:
456 case IW_AUTH_CIPHER_GROUP:
457 case IW_AUTH_WPA_ENABLED:
458 case IW_AUTH_RX_UNENCRYPTED_EAPOL:
459 case IW_AUTH_KEY_MGMT:
460 case IW_AUTH_CIPHER_GROUP_MGMT:
462 case IW_AUTH_CIPHER_PAIRWISE:
463 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
464 if (data->value & (IW_AUTH_CIPHER_WEP40 |
465 IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_TKIP))
466 sdata->u.mgd.flags |=
467 IEEE80211_STA_TKIP_WEP_USED;
469 sdata->u.mgd.flags &=
470 ~IEEE80211_STA_TKIP_WEP_USED;
473 case IW_AUTH_DROP_UNENCRYPTED:
474 sdata->drop_unencrypted = !!data->value;
476 case IW_AUTH_PRIVACY_INVOKED:
477 if (sdata->vif.type != NL80211_IFTYPE_STATION)
480 sdata->u.mgd.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
482 * Privacy invoked by wpa_supplicant, store the
483 * value and allow associating to a protected
484 * network without having a key up front.
487 sdata->u.mgd.flags |=
488 IEEE80211_STA_PRIVACY_INVOKED;
491 case IW_AUTH_80211_AUTH_ALG:
492 if (sdata->vif.type == NL80211_IFTYPE_STATION)
493 sdata->u.mgd.auth_algs = data->value;
498 if (!(sdata->local->hw.flags & IEEE80211_HW_MFP_CAPABLE)) {
502 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
503 switch (data->value) {
504 case IW_AUTH_MFP_DISABLED:
505 sdata->u.mgd.mfp = IEEE80211_MFP_DISABLED;
507 case IW_AUTH_MFP_OPTIONAL:
508 sdata->u.mgd.mfp = IEEE80211_MFP_OPTIONAL;
510 case IW_AUTH_MFP_REQUIRED:
511 sdata->u.mgd.mfp = IEEE80211_MFP_REQUIRED;
526 /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
527 static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
529 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
530 struct iw_statistics *wstats = &local->wstats;
531 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
532 struct sta_info *sta = NULL;
536 if (sdata->vif.type == NL80211_IFTYPE_STATION)
537 sta = sta_info_get(local, sdata->u.mgd.bssid);
540 wstats->discard.fragment = 0;
541 wstats->discard.misc = 0;
542 wstats->qual.qual = 0;
543 wstats->qual.level = 0;
544 wstats->qual.noise = 0;
545 wstats->qual.updated = IW_QUAL_ALL_INVALID;
547 wstats->qual.updated = 0;
549 * mirror what cfg80211 does for iwrange/scan results,
550 * otherwise userspace gets confused.
552 if (local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
553 IEEE80211_HW_SIGNAL_DBM)) {
554 wstats->qual.updated |= IW_QUAL_LEVEL_UPDATED;
555 wstats->qual.updated |= IW_QUAL_QUAL_UPDATED;
557 wstats->qual.updated |= IW_QUAL_LEVEL_INVALID;
558 wstats->qual.updated |= IW_QUAL_QUAL_INVALID;
561 if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
562 wstats->qual.level = sta->last_signal;
563 wstats->qual.qual = sta->last_signal;
564 } else if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
565 int sig = sta->last_signal;
567 wstats->qual.updated |= IW_QUAL_DBM;
568 wstats->qual.level = sig;
573 wstats->qual.qual = sig + 110;
576 if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
578 * This assumes that if driver reports noise, it also
579 * reports signal in dBm.
581 wstats->qual.noise = sta->last_noise;
582 wstats->qual.updated |= IW_QUAL_NOISE_UPDATED;
584 wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
593 static int ieee80211_ioctl_giwauth(struct net_device *dev,
594 struct iw_request_info *info,
595 struct iw_param *data, char *extra)
597 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
600 switch (data->flags & IW_AUTH_INDEX) {
601 case IW_AUTH_80211_AUTH_ALG:
602 if (sdata->vif.type == NL80211_IFTYPE_STATION)
603 data->value = sdata->u.mgd.auth_algs;
615 /* Structures to export the Wireless Handlers */
617 static const iw_handler ieee80211_handler[] =
619 (iw_handler) NULL, /* SIOCSIWCOMMIT */
620 (iw_handler) cfg80211_wext_giwname, /* SIOCGIWNAME */
621 (iw_handler) NULL, /* SIOCSIWNWID */
622 (iw_handler) NULL, /* SIOCGIWNWID */
623 (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
624 (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
625 (iw_handler) cfg80211_wext_siwmode, /* SIOCSIWMODE */
626 (iw_handler) cfg80211_wext_giwmode, /* SIOCGIWMODE */
627 (iw_handler) NULL, /* SIOCSIWSENS */
628 (iw_handler) NULL, /* SIOCGIWSENS */
629 (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
630 (iw_handler) cfg80211_wext_giwrange, /* SIOCGIWRANGE */
631 (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
632 (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
633 (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
634 (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
635 (iw_handler) NULL, /* SIOCSIWSPY */
636 (iw_handler) NULL, /* SIOCGIWSPY */
637 (iw_handler) NULL, /* SIOCSIWTHRSPY */
638 (iw_handler) NULL, /* SIOCGIWTHRSPY */
639 (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
640 (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
641 (iw_handler) cfg80211_wext_siwmlme, /* SIOCSIWMLME */
642 (iw_handler) NULL, /* SIOCGIWAPLIST */
643 (iw_handler) cfg80211_wext_siwscan, /* SIOCSIWSCAN */
644 (iw_handler) cfg80211_wext_giwscan, /* SIOCGIWSCAN */
645 (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
646 (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
647 (iw_handler) NULL, /* SIOCSIWNICKN */
648 (iw_handler) NULL, /* SIOCGIWNICKN */
649 (iw_handler) NULL, /* -- hole -- */
650 (iw_handler) NULL, /* -- hole -- */
651 (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
652 (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
653 (iw_handler) cfg80211_wext_siwrts, /* SIOCSIWRTS */
654 (iw_handler) cfg80211_wext_giwrts, /* SIOCGIWRTS */
655 (iw_handler) cfg80211_wext_siwfrag, /* SIOCSIWFRAG */
656 (iw_handler) cfg80211_wext_giwfrag, /* SIOCGIWFRAG */
657 (iw_handler) ieee80211_ioctl_siwtxpower, /* SIOCSIWTXPOW */
658 (iw_handler) ieee80211_ioctl_giwtxpower, /* SIOCGIWTXPOW */
659 (iw_handler) cfg80211_wext_siwretry, /* SIOCSIWRETRY */
660 (iw_handler) cfg80211_wext_giwretry, /* SIOCGIWRETRY */
661 (iw_handler) cfg80211_wext_siwencode, /* SIOCSIWENCODE */
662 (iw_handler) cfg80211_wext_giwencode, /* SIOCGIWENCODE */
663 (iw_handler) ieee80211_ioctl_siwpower, /* SIOCSIWPOWER */
664 (iw_handler) ieee80211_ioctl_giwpower, /* SIOCGIWPOWER */
665 (iw_handler) NULL, /* -- hole -- */
666 (iw_handler) NULL, /* -- hole -- */
667 (iw_handler) ieee80211_ioctl_siwgenie, /* SIOCSIWGENIE */
668 (iw_handler) NULL, /* SIOCGIWGENIE */
669 (iw_handler) ieee80211_ioctl_siwauth, /* SIOCSIWAUTH */
670 (iw_handler) ieee80211_ioctl_giwauth, /* SIOCGIWAUTH */
671 (iw_handler) cfg80211_wext_siwencodeext, /* SIOCSIWENCODEEXT */
672 (iw_handler) NULL, /* SIOCGIWENCODEEXT */
673 (iw_handler) NULL, /* SIOCSIWPMKSA */
674 (iw_handler) NULL, /* -- hole -- */
677 const struct iw_handler_def ieee80211_iw_handler_def =
679 .num_standard = ARRAY_SIZE(ieee80211_handler),
680 .standard = (iw_handler *) ieee80211_handler,
681 .get_wireless_stats = ieee80211_get_wireless_stats,