wext.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/types.h>
  13. #include <linux/slab.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/wireless.h>
  18. #include <net/iw_handler.h>
  19. #include <asm/uaccess.h>
  20. #include <net/mac80211.h>
  21. #include "ieee80211_i.h"
  22. #include "led.h"
  23. #include "rate.h"
  24. #include "wpa.h"
  25. #include "aes_ccm.h"
  26. static int ieee80211_ioctl_siwfreq(struct net_device *dev,
  27. struct iw_request_info *info,
  28. struct iw_freq *freq, char *extra)
  29. {
  30. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  31. struct ieee80211_local *local = sdata->local;
  32. struct ieee80211_channel *chan;
  33. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  34. return cfg80211_ibss_wext_siwfreq(dev, info, freq, extra);
  35. else if (sdata->vif.type == NL80211_IFTYPE_STATION)
  36. return cfg80211_mgd_wext_siwfreq(dev, info, freq, extra);
  37. /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
  38. if (freq->e == 0) {
  39. if (freq->m < 0)
  40. return -EINVAL;
  41. else
  42. chan = ieee80211_get_channel(local->hw.wiphy,
  43. ieee80211_channel_to_frequency(freq->m));
  44. } else {
  45. int i, div = 1000000;
  46. for (i = 0; i < freq->e; i++)
  47. div /= 10;
  48. if (div <= 0)
  49. return -EINVAL;
  50. chan = ieee80211_get_channel(local->hw.wiphy, freq->m / div);
  51. }
  52. if (!chan)
  53. return -EINVAL;
  54. if (chan->flags & IEEE80211_CHAN_DISABLED)
  55. return -EINVAL;
  56. /*
  57. * no change except maybe auto -> fixed, ignore the HT
  58. * setting so you can fix a channel you're on already
  59. */
  60. if (local->oper_channel == chan)
  61. return 0;
  62. local->oper_channel = chan;
  63. local->oper_channel_type = NL80211_CHAN_NO_HT;
  64. ieee80211_hw_config(local, 0);
  65. return 0;
  66. }
  67. static int ieee80211_ioctl_giwfreq(struct net_device *dev,
  68. struct iw_request_info *info,
  69. struct iw_freq *freq, char *extra)
  70. {
  71. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  72. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  73. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  74. return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
  75. else if (sdata->vif.type == NL80211_IFTYPE_STATION)
  76. return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
  77. freq->m = local->oper_channel->center_freq;
  78. freq->e = 6;
  79. return 0;
  80. }
  81. static int ieee80211_ioctl_siwessid(struct net_device *dev,
  82. struct iw_request_info *info,
  83. struct iw_point *data, char *ssid)
  84. {
  85. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  86. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  87. return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
  88. else if (sdata->vif.type == NL80211_IFTYPE_STATION)
  89. return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
  90. return -EOPNOTSUPP;
  91. }
  92. static int ieee80211_ioctl_giwessid(struct net_device *dev,
  93. struct iw_request_info *info,
  94. struct iw_point *data, char *ssid)
  95. {
  96. struct ieee80211_sub_if_data *sdata;
  97. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  98. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  99. return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
  100. else if (sdata->vif.type == NL80211_IFTYPE_STATION)
  101. return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
  102. return -EOPNOTSUPP;
  103. }
  104. static int ieee80211_ioctl_siwap(struct net_device *dev,
  105. struct iw_request_info *info,
  106. struct sockaddr *ap_addr, char *extra)
  107. {
  108. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  109. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  110. return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
  111. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  112. return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
  113. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  114. /*
  115. * If it is necessary to update the WDS peer address
  116. * while the interface is running, then we need to do
  117. * more work here, namely if it is running we need to
  118. * add a new and remove the old STA entry, this is
  119. * normally handled by _open() and _stop().
  120. */
  121. if (netif_running(dev))
  122. return -EBUSY;
  123. memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
  124. ETH_ALEN);
  125. return 0;
  126. }
  127. return -EOPNOTSUPP;
  128. }
  129. static int ieee80211_ioctl_giwap(struct net_device *dev,
  130. struct iw_request_info *info,
  131. struct sockaddr *ap_addr, char *extra)
  132. {
  133. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  134. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  135. return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
  136. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  137. return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
  138. if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  139. ap_addr->sa_family = ARPHRD_ETHER;
  140. memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
  141. return 0;
  142. }
  143. return -EOPNOTSUPP;
  144. }
  145. static int ieee80211_ioctl_siwrate(struct net_device *dev,
  146. struct iw_request_info *info,
  147. struct iw_param *rate, char *extra)
  148. {
  149. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  150. int i, err = -EINVAL;
  151. u32 target_rate = rate->value / 100000;
  152. struct ieee80211_sub_if_data *sdata;
  153. struct ieee80211_supported_band *sband;
  154. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  155. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  156. /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
  157. * target_rate = X, rate->fixed = 1 means only rate X
  158. * target_rate = X, rate->fixed = 0 means all rates <= X */
  159. sdata->max_ratectrl_rateidx = -1;
  160. sdata->force_unicast_rateidx = -1;
  161. if (rate->value < 0)
  162. return 0;
  163. for (i=0; i< sband->n_bitrates; i++) {
  164. struct ieee80211_rate *brate = &sband->bitrates[i];
  165. int this_rate = brate->bitrate;
  166. if (target_rate == this_rate) {
  167. sdata->max_ratectrl_rateidx = i;
  168. if (rate->fixed)
  169. sdata->force_unicast_rateidx = i;
  170. err = 0;
  171. break;
  172. }
  173. }
  174. return err;
  175. }
  176. static int ieee80211_ioctl_giwrate(struct net_device *dev,
  177. struct iw_request_info *info,
  178. struct iw_param *rate, char *extra)
  179. {
  180. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  181. struct sta_info *sta;
  182. struct ieee80211_sub_if_data *sdata;
  183. struct ieee80211_supported_band *sband;
  184. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  185. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  186. return -EOPNOTSUPP;
  187. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  188. rcu_read_lock();
  189. sta = sta_info_get(local, sdata->u.mgd.bssid);
  190. if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
  191. rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
  192. else
  193. rate->value = 0;
  194. rcu_read_unlock();
  195. if (!sta)
  196. return -ENODEV;
  197. rate->value *= 100000;
  198. return 0;
  199. }
  200. static int ieee80211_ioctl_siwpower(struct net_device *dev,
  201. struct iw_request_info *info,
  202. struct iw_param *wrq,
  203. char *extra)
  204. {
  205. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  206. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  207. struct ieee80211_conf *conf = &local->hw.conf;
  208. int timeout = 0;
  209. bool ps;
  210. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
  211. return -EOPNOTSUPP;
  212. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  213. return -EINVAL;
  214. if (wrq->disabled) {
  215. ps = false;
  216. timeout = 0;
  217. goto set;
  218. }
  219. switch (wrq->flags & IW_POWER_MODE) {
  220. case IW_POWER_ON: /* If not specified */
  221. case IW_POWER_MODE: /* If set all mask */
  222. case IW_POWER_ALL_R: /* If explicitely state all */
  223. ps = true;
  224. break;
  225. default: /* Otherwise we ignore */
  226. return -EINVAL;
  227. }
  228. if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
  229. return -EINVAL;
  230. if (wrq->flags & IW_POWER_TIMEOUT)
  231. timeout = wrq->value / 1000;
  232. set:
  233. if (ps == sdata->u.mgd.powersave && timeout == conf->dynamic_ps_timeout)
  234. return 0;
  235. sdata->u.mgd.powersave = ps;
  236. conf->dynamic_ps_timeout = timeout;
  237. if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
  238. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  239. ieee80211_recalc_ps(local, -1);
  240. return 0;
  241. }
  242. static int ieee80211_ioctl_giwpower(struct net_device *dev,
  243. struct iw_request_info *info,
  244. union iwreq_data *wrqu,
  245. char *extra)
  246. {
  247. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  248. wrqu->power.disabled = !sdata->u.mgd.powersave;
  249. return 0;
  250. }
  251. /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
  252. static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
  253. {
  254. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  255. struct iw_statistics *wstats = &local->wstats;
  256. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  257. struct sta_info *sta = NULL;
  258. rcu_read_lock();
  259. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  260. sta = sta_info_get(local, sdata->u.mgd.bssid);
  261. if (!sta) {
  262. wstats->discard.fragment = 0;
  263. wstats->discard.misc = 0;
  264. wstats->qual.qual = 0;
  265. wstats->qual.level = 0;
  266. wstats->qual.noise = 0;
  267. wstats->qual.updated = IW_QUAL_ALL_INVALID;
  268. } else {
  269. wstats->qual.updated = 0;
  270. /*
  271. * mirror what cfg80211 does for iwrange/scan results,
  272. * otherwise userspace gets confused.
  273. */
  274. if (local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
  275. IEEE80211_HW_SIGNAL_DBM)) {
  276. wstats->qual.updated |= IW_QUAL_LEVEL_UPDATED;
  277. wstats->qual.updated |= IW_QUAL_QUAL_UPDATED;
  278. } else {
  279. wstats->qual.updated |= IW_QUAL_LEVEL_INVALID;
  280. wstats->qual.updated |= IW_QUAL_QUAL_INVALID;
  281. }
  282. if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
  283. wstats->qual.level = sta->last_signal;
  284. wstats->qual.qual = sta->last_signal;
  285. } else if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
  286. int sig = sta->last_signal;
  287. wstats->qual.updated |= IW_QUAL_DBM;
  288. wstats->qual.level = sig;
  289. if (sig < -110)
  290. sig = -110;
  291. else if (sig > -40)
  292. sig = -40;
  293. wstats->qual.qual = sig + 110;
  294. }
  295. if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
  296. /*
  297. * This assumes that if driver reports noise, it also
  298. * reports signal in dBm.
  299. */
  300. wstats->qual.noise = sta->last_noise;
  301. wstats->qual.updated |= IW_QUAL_NOISE_UPDATED;
  302. } else {
  303. wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
  304. }
  305. }
  306. rcu_read_unlock();
  307. return wstats;
  308. }
  309. /* Structures to export the Wireless Handlers */
  310. static const iw_handler ieee80211_handler[] =
  311. {
  312. (iw_handler) NULL, /* SIOCSIWCOMMIT */
  313. (iw_handler) cfg80211_wext_giwname, /* SIOCGIWNAME */
  314. (iw_handler) NULL, /* SIOCSIWNWID */
  315. (iw_handler) NULL, /* SIOCGIWNWID */
  316. (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
  317. (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
  318. (iw_handler) cfg80211_wext_siwmode, /* SIOCSIWMODE */
  319. (iw_handler) cfg80211_wext_giwmode, /* SIOCGIWMODE */
  320. (iw_handler) NULL, /* SIOCSIWSENS */
  321. (iw_handler) NULL, /* SIOCGIWSENS */
  322. (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
  323. (iw_handler) cfg80211_wext_giwrange, /* SIOCGIWRANGE */
  324. (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
  325. (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
  326. (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
  327. (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
  328. (iw_handler) NULL, /* SIOCSIWSPY */
  329. (iw_handler) NULL, /* SIOCGIWSPY */
  330. (iw_handler) NULL, /* SIOCSIWTHRSPY */
  331. (iw_handler) NULL, /* SIOCGIWTHRSPY */
  332. (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
  333. (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
  334. (iw_handler) cfg80211_wext_siwmlme, /* SIOCSIWMLME */
  335. (iw_handler) NULL, /* SIOCGIWAPLIST */
  336. (iw_handler) cfg80211_wext_siwscan, /* SIOCSIWSCAN */
  337. (iw_handler) cfg80211_wext_giwscan, /* SIOCGIWSCAN */
  338. (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
  339. (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
  340. (iw_handler) NULL, /* SIOCSIWNICKN */
  341. (iw_handler) NULL, /* SIOCGIWNICKN */
  342. (iw_handler) NULL, /* -- hole -- */
  343. (iw_handler) NULL, /* -- hole -- */
  344. (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
  345. (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
  346. (iw_handler) cfg80211_wext_siwrts, /* SIOCSIWRTS */
  347. (iw_handler) cfg80211_wext_giwrts, /* SIOCGIWRTS */
  348. (iw_handler) cfg80211_wext_siwfrag, /* SIOCSIWFRAG */
  349. (iw_handler) cfg80211_wext_giwfrag, /* SIOCGIWFRAG */
  350. (iw_handler) cfg80211_wext_siwtxpower, /* SIOCSIWTXPOW */
  351. (iw_handler) cfg80211_wext_giwtxpower, /* SIOCGIWTXPOW */
  352. (iw_handler) cfg80211_wext_siwretry, /* SIOCSIWRETRY */
  353. (iw_handler) cfg80211_wext_giwretry, /* SIOCGIWRETRY */
  354. (iw_handler) cfg80211_wext_siwencode, /* SIOCSIWENCODE */
  355. (iw_handler) cfg80211_wext_giwencode, /* SIOCGIWENCODE */
  356. (iw_handler) ieee80211_ioctl_siwpower, /* SIOCSIWPOWER */
  357. (iw_handler) ieee80211_ioctl_giwpower, /* SIOCGIWPOWER */
  358. (iw_handler) NULL, /* -- hole -- */
  359. (iw_handler) NULL, /* -- hole -- */
  360. (iw_handler) cfg80211_wext_siwgenie, /* SIOCSIWGENIE */
  361. (iw_handler) NULL, /* SIOCGIWGENIE */
  362. (iw_handler) cfg80211_wext_siwauth, /* SIOCSIWAUTH */
  363. (iw_handler) cfg80211_wext_giwauth, /* SIOCGIWAUTH */
  364. (iw_handler) cfg80211_wext_siwencodeext, /* SIOCSIWENCODEEXT */
  365. (iw_handler) NULL, /* SIOCGIWENCODEEXT */
  366. (iw_handler) NULL, /* SIOCSIWPMKSA */
  367. (iw_handler) NULL, /* -- hole -- */
  368. };
  369. const struct iw_handler_def ieee80211_iw_handler_def =
  370. {
  371. .num_standard = ARRAY_SIZE(ieee80211_handler),
  372. .standard = (iw_handler *) ieee80211_handler,
  373. .get_wireless_stats = ieee80211_get_wireless_stats,
  374. };