wext.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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_set_encryption(struct ieee80211_sub_if_data *sdata, u8 *sta_addr,
  27. int idx, int alg, int remove,
  28. int set_tx_key, const u8 *_key,
  29. size_t key_len)
  30. {
  31. struct ieee80211_local *local = sdata->local;
  32. struct sta_info *sta;
  33. struct ieee80211_key *key;
  34. int err;
  35. if (alg == ALG_AES_CMAC) {
  36. if (idx < NUM_DEFAULT_KEYS ||
  37. idx >= NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) {
  38. printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d "
  39. "(BIP)\n", sdata->dev->name, idx);
  40. return -EINVAL;
  41. }
  42. } else if (idx < 0 || idx >= NUM_DEFAULT_KEYS) {
  43. printk(KERN_DEBUG "%s: set_encrypt - invalid idx=%d\n",
  44. sdata->dev->name, idx);
  45. return -EINVAL;
  46. }
  47. if (remove) {
  48. rcu_read_lock();
  49. err = 0;
  50. if (is_broadcast_ether_addr(sta_addr)) {
  51. key = sdata->keys[idx];
  52. } else {
  53. sta = sta_info_get(local, sta_addr);
  54. if (!sta) {
  55. err = -ENOENT;
  56. goto out_unlock;
  57. }
  58. key = sta->key;
  59. }
  60. ieee80211_key_free(key);
  61. } else {
  62. key = ieee80211_key_alloc(alg, idx, key_len, _key);
  63. if (!key)
  64. return -ENOMEM;
  65. sta = NULL;
  66. err = 0;
  67. rcu_read_lock();
  68. if (!is_broadcast_ether_addr(sta_addr)) {
  69. set_tx_key = 0;
  70. /*
  71. * According to the standard, the key index of a
  72. * pairwise key must be zero. However, some AP are
  73. * broken when it comes to WEP key indices, so we
  74. * work around this.
  75. */
  76. if (idx != 0 && alg != ALG_WEP) {
  77. ieee80211_key_free(key);
  78. err = -EINVAL;
  79. goto out_unlock;
  80. }
  81. sta = sta_info_get(local, sta_addr);
  82. if (!sta) {
  83. ieee80211_key_free(key);
  84. err = -ENOENT;
  85. goto out_unlock;
  86. }
  87. }
  88. if (alg == ALG_WEP &&
  89. key_len != LEN_WEP40 && key_len != LEN_WEP104) {
  90. ieee80211_key_free(key);
  91. err = -EINVAL;
  92. goto out_unlock;
  93. }
  94. ieee80211_key_link(key, sdata, sta);
  95. if (set_tx_key || (!sta && !sdata->default_key && key))
  96. ieee80211_set_default_key(sdata, idx);
  97. if (alg == ALG_AES_CMAC &&
  98. (set_tx_key || (!sta && !sdata->default_mgmt_key && key)))
  99. ieee80211_set_default_mgmt_key(sdata, idx);
  100. }
  101. out_unlock:
  102. rcu_read_unlock();
  103. return err;
  104. }
  105. static int ieee80211_ioctl_siwgenie(struct net_device *dev,
  106. struct iw_request_info *info,
  107. struct iw_point *data, char *extra)
  108. {
  109. struct ieee80211_sub_if_data *sdata;
  110. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  111. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  112. int ret = ieee80211_sta_set_extra_ie(sdata, extra, data->length);
  113. if (ret)
  114. return ret;
  115. sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
  116. sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
  117. ieee80211_sta_req_auth(sdata);
  118. return 0;
  119. }
  120. return -EOPNOTSUPP;
  121. }
  122. static int ieee80211_ioctl_siwfreq(struct net_device *dev,
  123. struct iw_request_info *info,
  124. struct iw_freq *freq, char *extra)
  125. {
  126. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  127. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  128. return cfg80211_ibss_wext_siwfreq(dev, info, freq, extra);
  129. else if (sdata->vif.type == NL80211_IFTYPE_STATION)
  130. sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_CHANNEL_SEL;
  131. /* freq->e == 0: freq->m = channel; otherwise freq = m * 10^e */
  132. if (freq->e == 0) {
  133. if (freq->m < 0) {
  134. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  135. sdata->u.mgd.flags |=
  136. IEEE80211_STA_AUTO_CHANNEL_SEL;
  137. return 0;
  138. } else
  139. return ieee80211_set_freq(sdata,
  140. ieee80211_channel_to_frequency(freq->m));
  141. } else {
  142. int i, div = 1000000;
  143. for (i = 0; i < freq->e; i++)
  144. div /= 10;
  145. if (div > 0)
  146. return ieee80211_set_freq(sdata, freq->m / div);
  147. else
  148. return -EINVAL;
  149. }
  150. }
  151. static int ieee80211_ioctl_giwfreq(struct net_device *dev,
  152. struct iw_request_info *info,
  153. struct iw_freq *freq, char *extra)
  154. {
  155. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  156. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  157. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  158. return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
  159. freq->m = local->hw.conf.channel->center_freq;
  160. freq->e = 6;
  161. return 0;
  162. }
  163. static int ieee80211_ioctl_siwessid(struct net_device *dev,
  164. struct iw_request_info *info,
  165. struct iw_point *data, char *ssid)
  166. {
  167. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  168. size_t len = data->length;
  169. int ret;
  170. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  171. return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
  172. /* iwconfig uses nul termination in SSID.. */
  173. if (len > 0 && ssid[len - 1] == '\0')
  174. len--;
  175. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  176. if (data->flags)
  177. sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_SSID_SEL;
  178. else
  179. sdata->u.mgd.flags |= IEEE80211_STA_AUTO_SSID_SEL;
  180. ret = ieee80211_sta_set_ssid(sdata, ssid, len);
  181. if (ret)
  182. return ret;
  183. sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
  184. ieee80211_sta_req_auth(sdata);
  185. return 0;
  186. }
  187. return -EOPNOTSUPP;
  188. }
  189. static int ieee80211_ioctl_giwessid(struct net_device *dev,
  190. struct iw_request_info *info,
  191. struct iw_point *data, char *ssid)
  192. {
  193. size_t len;
  194. struct ieee80211_sub_if_data *sdata;
  195. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  196. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  197. return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
  198. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  199. int res = ieee80211_sta_get_ssid(sdata, ssid, &len);
  200. if (res == 0) {
  201. data->length = len;
  202. data->flags = 1;
  203. } else
  204. data->flags = 0;
  205. return res;
  206. }
  207. return -EOPNOTSUPP;
  208. }
  209. static int ieee80211_ioctl_siwap(struct net_device *dev,
  210. struct iw_request_info *info,
  211. struct sockaddr *ap_addr, char *extra)
  212. {
  213. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  214. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  215. return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
  216. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  217. int ret;
  218. if (is_zero_ether_addr((u8 *) &ap_addr->sa_data))
  219. sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL |
  220. IEEE80211_STA_AUTO_CHANNEL_SEL;
  221. else if (is_broadcast_ether_addr((u8 *) &ap_addr->sa_data))
  222. sdata->u.mgd.flags |= IEEE80211_STA_AUTO_BSSID_SEL;
  223. else
  224. sdata->u.mgd.flags &= ~IEEE80211_STA_AUTO_BSSID_SEL;
  225. ret = ieee80211_sta_set_bssid(sdata, (u8 *) &ap_addr->sa_data);
  226. if (ret)
  227. return ret;
  228. sdata->u.mgd.flags &= ~IEEE80211_STA_EXT_SME;
  229. ieee80211_sta_req_auth(sdata);
  230. return 0;
  231. } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  232. /*
  233. * If it is necessary to update the WDS peer address
  234. * while the interface is running, then we need to do
  235. * more work here, namely if it is running we need to
  236. * add a new and remove the old STA entry, this is
  237. * normally handled by _open() and _stop().
  238. */
  239. if (netif_running(dev))
  240. return -EBUSY;
  241. memcpy(&sdata->u.wds.remote_addr, (u8 *) &ap_addr->sa_data,
  242. ETH_ALEN);
  243. return 0;
  244. }
  245. return -EOPNOTSUPP;
  246. }
  247. static int ieee80211_ioctl_giwap(struct net_device *dev,
  248. struct iw_request_info *info,
  249. struct sockaddr *ap_addr, char *extra)
  250. {
  251. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  252. if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
  253. return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
  254. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  255. if (sdata->u.mgd.state == IEEE80211_STA_MLME_ASSOCIATED) {
  256. ap_addr->sa_family = ARPHRD_ETHER;
  257. memcpy(&ap_addr->sa_data, sdata->u.mgd.bssid, ETH_ALEN);
  258. } else
  259. memset(&ap_addr->sa_data, 0, ETH_ALEN);
  260. return 0;
  261. } else if (sdata->vif.type == NL80211_IFTYPE_WDS) {
  262. ap_addr->sa_family = ARPHRD_ETHER;
  263. memcpy(&ap_addr->sa_data, sdata->u.wds.remote_addr, ETH_ALEN);
  264. return 0;
  265. }
  266. return -EOPNOTSUPP;
  267. }
  268. static int ieee80211_ioctl_siwrate(struct net_device *dev,
  269. struct iw_request_info *info,
  270. struct iw_param *rate, char *extra)
  271. {
  272. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  273. int i, err = -EINVAL;
  274. u32 target_rate = rate->value / 100000;
  275. struct ieee80211_sub_if_data *sdata;
  276. struct ieee80211_supported_band *sband;
  277. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  278. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  279. /* target_rate = -1, rate->fixed = 0 means auto only, so use all rates
  280. * target_rate = X, rate->fixed = 1 means only rate X
  281. * target_rate = X, rate->fixed = 0 means all rates <= X */
  282. sdata->max_ratectrl_rateidx = -1;
  283. sdata->force_unicast_rateidx = -1;
  284. if (rate->value < 0)
  285. return 0;
  286. for (i=0; i< sband->n_bitrates; i++) {
  287. struct ieee80211_rate *brate = &sband->bitrates[i];
  288. int this_rate = brate->bitrate;
  289. if (target_rate == this_rate) {
  290. sdata->max_ratectrl_rateidx = i;
  291. if (rate->fixed)
  292. sdata->force_unicast_rateidx = i;
  293. err = 0;
  294. break;
  295. }
  296. }
  297. return err;
  298. }
  299. static int ieee80211_ioctl_giwrate(struct net_device *dev,
  300. struct iw_request_info *info,
  301. struct iw_param *rate, char *extra)
  302. {
  303. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  304. struct sta_info *sta;
  305. struct ieee80211_sub_if_data *sdata;
  306. struct ieee80211_supported_band *sband;
  307. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  308. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  309. return -EOPNOTSUPP;
  310. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  311. rcu_read_lock();
  312. sta = sta_info_get(local, sdata->u.mgd.bssid);
  313. if (sta && !(sta->last_tx_rate.flags & IEEE80211_TX_RC_MCS))
  314. rate->value = sband->bitrates[sta->last_tx_rate.idx].bitrate;
  315. else
  316. rate->value = 0;
  317. rcu_read_unlock();
  318. if (!sta)
  319. return -ENODEV;
  320. rate->value *= 100000;
  321. return 0;
  322. }
  323. static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
  324. struct iw_request_info *info,
  325. union iwreq_data *data, char *extra)
  326. {
  327. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  328. struct ieee80211_channel* chan = local->hw.conf.channel;
  329. bool reconf = false;
  330. u32 reconf_flags = 0;
  331. int new_power_level;
  332. if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
  333. return -EINVAL;
  334. if (data->txpower.flags & IW_TXPOW_RANGE)
  335. return -EINVAL;
  336. if (!chan)
  337. return -EINVAL;
  338. /* only change when not disabling */
  339. if (!data->txpower.disabled) {
  340. if (data->txpower.fixed) {
  341. if (data->txpower.value < 0)
  342. return -EINVAL;
  343. new_power_level = data->txpower.value;
  344. /*
  345. * Debatable, but we cannot do a fixed power
  346. * level above the regulatory constraint.
  347. * Use "iwconfig wlan0 txpower 15dBm" instead.
  348. */
  349. if (new_power_level > chan->max_power)
  350. return -EINVAL;
  351. } else {
  352. /*
  353. * Automatic power level setting, max being the value
  354. * passed in from userland.
  355. */
  356. if (data->txpower.value < 0)
  357. new_power_level = -1;
  358. else
  359. new_power_level = data->txpower.value;
  360. }
  361. reconf = true;
  362. /*
  363. * ieee80211_hw_config() will limit to the channel's
  364. * max power and possibly power constraint from AP.
  365. */
  366. local->user_power_level = new_power_level;
  367. }
  368. if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
  369. local->hw.conf.radio_enabled = !(data->txpower.disabled);
  370. reconf_flags |= IEEE80211_CONF_CHANGE_RADIO_ENABLED;
  371. ieee80211_led_radio(local, local->hw.conf.radio_enabled);
  372. }
  373. if (reconf || reconf_flags)
  374. ieee80211_hw_config(local, reconf_flags);
  375. return 0;
  376. }
  377. static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
  378. struct iw_request_info *info,
  379. union iwreq_data *data, char *extra)
  380. {
  381. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  382. data->txpower.fixed = 1;
  383. data->txpower.disabled = !(local->hw.conf.radio_enabled);
  384. data->txpower.value = local->hw.conf.power_level;
  385. data->txpower.flags = IW_TXPOW_DBM;
  386. return 0;
  387. }
  388. static int ieee80211_ioctl_siwrts(struct net_device *dev,
  389. struct iw_request_info *info,
  390. struct iw_param *rts, char *extra)
  391. {
  392. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  393. if (rts->disabled)
  394. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  395. else if (!rts->fixed)
  396. /* if the rts value is not fixed, then take default */
  397. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  398. else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
  399. return -EINVAL;
  400. else
  401. local->rts_threshold = rts->value;
  402. /* If the wlan card performs RTS/CTS in hardware/firmware,
  403. * configure it here */
  404. if (local->ops->set_rts_threshold)
  405. local->ops->set_rts_threshold(local_to_hw(local),
  406. local->rts_threshold);
  407. return 0;
  408. }
  409. static int ieee80211_ioctl_giwrts(struct net_device *dev,
  410. struct iw_request_info *info,
  411. struct iw_param *rts, char *extra)
  412. {
  413. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  414. rts->value = local->rts_threshold;
  415. rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
  416. rts->fixed = 1;
  417. return 0;
  418. }
  419. static int ieee80211_ioctl_siwfrag(struct net_device *dev,
  420. struct iw_request_info *info,
  421. struct iw_param *frag, char *extra)
  422. {
  423. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  424. if (frag->disabled)
  425. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  426. else if (!frag->fixed)
  427. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  428. else if (frag->value < 256 ||
  429. frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
  430. return -EINVAL;
  431. else {
  432. /* Fragment length must be even, so strip LSB. */
  433. local->fragmentation_threshold = frag->value & ~0x1;
  434. }
  435. return 0;
  436. }
  437. static int ieee80211_ioctl_giwfrag(struct net_device *dev,
  438. struct iw_request_info *info,
  439. struct iw_param *frag, char *extra)
  440. {
  441. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  442. frag->value = local->fragmentation_threshold;
  443. frag->disabled = (frag->value >= IEEE80211_MAX_FRAG_THRESHOLD);
  444. frag->fixed = 1;
  445. return 0;
  446. }
  447. static int ieee80211_ioctl_siwretry(struct net_device *dev,
  448. struct iw_request_info *info,
  449. struct iw_param *retry, char *extra)
  450. {
  451. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  452. if (retry->disabled ||
  453. (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
  454. return -EINVAL;
  455. if (retry->flags & IW_RETRY_MAX) {
  456. local->hw.conf.long_frame_max_tx_count = retry->value;
  457. } else if (retry->flags & IW_RETRY_MIN) {
  458. local->hw.conf.short_frame_max_tx_count = retry->value;
  459. } else {
  460. local->hw.conf.long_frame_max_tx_count = retry->value;
  461. local->hw.conf.short_frame_max_tx_count = retry->value;
  462. }
  463. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_RETRY_LIMITS);
  464. return 0;
  465. }
  466. static int ieee80211_ioctl_giwretry(struct net_device *dev,
  467. struct iw_request_info *info,
  468. struct iw_param *retry, char *extra)
  469. {
  470. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  471. retry->disabled = 0;
  472. if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
  473. /* first return min value, iwconfig will ask max value
  474. * later if needed */
  475. retry->flags |= IW_RETRY_LIMIT;
  476. retry->value = local->hw.conf.short_frame_max_tx_count;
  477. if (local->hw.conf.long_frame_max_tx_count !=
  478. local->hw.conf.short_frame_max_tx_count)
  479. retry->flags |= IW_RETRY_MIN;
  480. return 0;
  481. }
  482. if (retry->flags & IW_RETRY_MAX) {
  483. retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
  484. retry->value = local->hw.conf.long_frame_max_tx_count;
  485. }
  486. return 0;
  487. }
  488. static int ieee80211_ioctl_siwencode(struct net_device *dev,
  489. struct iw_request_info *info,
  490. struct iw_point *erq, char *keybuf)
  491. {
  492. struct ieee80211_sub_if_data *sdata;
  493. int idx, i, alg = ALG_WEP;
  494. u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  495. int remove = 0, ret;
  496. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  497. idx = erq->flags & IW_ENCODE_INDEX;
  498. if (idx == 0) {
  499. if (sdata->default_key)
  500. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  501. if (sdata->default_key == sdata->keys[i]) {
  502. idx = i;
  503. break;
  504. }
  505. }
  506. } else if (idx < 1 || idx > 4)
  507. return -EINVAL;
  508. else
  509. idx--;
  510. if (erq->flags & IW_ENCODE_DISABLED)
  511. remove = 1;
  512. else if (erq->length == 0) {
  513. /* No key data - just set the default TX key index */
  514. ieee80211_set_default_key(sdata, idx);
  515. return 0;
  516. }
  517. ret = ieee80211_set_encryption(
  518. sdata, bcaddr,
  519. idx, alg, remove,
  520. !sdata->default_key,
  521. keybuf, erq->length);
  522. if (!ret && sdata->vif.type == NL80211_IFTYPE_STATION) {
  523. if (remove)
  524. sdata->u.mgd.flags &= ~IEEE80211_STA_TKIP_WEP_USED;
  525. else
  526. sdata->u.mgd.flags |= IEEE80211_STA_TKIP_WEP_USED;
  527. }
  528. return ret;
  529. }
  530. static int ieee80211_ioctl_giwencode(struct net_device *dev,
  531. struct iw_request_info *info,
  532. struct iw_point *erq, char *key)
  533. {
  534. struct ieee80211_sub_if_data *sdata;
  535. int idx, i;
  536. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  537. idx = erq->flags & IW_ENCODE_INDEX;
  538. if (idx < 1 || idx > 4) {
  539. idx = -1;
  540. if (!sdata->default_key)
  541. idx = 0;
  542. else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  543. if (sdata->default_key == sdata->keys[i]) {
  544. idx = i;
  545. break;
  546. }
  547. }
  548. if (idx < 0)
  549. return -EINVAL;
  550. } else
  551. idx--;
  552. erq->flags = idx + 1;
  553. if (!sdata->keys[idx]) {
  554. erq->length = 0;
  555. erq->flags |= IW_ENCODE_DISABLED;
  556. return 0;
  557. }
  558. memcpy(key, sdata->keys[idx]->conf.key,
  559. min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
  560. erq->length = sdata->keys[idx]->conf.keylen;
  561. erq->flags |= IW_ENCODE_ENABLED;
  562. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  563. switch (sdata->u.mgd.auth_alg) {
  564. case WLAN_AUTH_OPEN:
  565. case WLAN_AUTH_LEAP:
  566. erq->flags |= IW_ENCODE_OPEN;
  567. break;
  568. case WLAN_AUTH_SHARED_KEY:
  569. erq->flags |= IW_ENCODE_RESTRICTED;
  570. break;
  571. }
  572. }
  573. return 0;
  574. }
  575. static int ieee80211_ioctl_siwpower(struct net_device *dev,
  576. struct iw_request_info *info,
  577. struct iw_param *wrq,
  578. char *extra)
  579. {
  580. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  581. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  582. struct ieee80211_conf *conf = &local->hw.conf;
  583. int timeout = 0;
  584. bool ps;
  585. if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
  586. return -EOPNOTSUPP;
  587. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  588. return -EINVAL;
  589. if (wrq->disabled) {
  590. ps = false;
  591. timeout = 0;
  592. goto set;
  593. }
  594. switch (wrq->flags & IW_POWER_MODE) {
  595. case IW_POWER_ON: /* If not specified */
  596. case IW_POWER_MODE: /* If set all mask */
  597. case IW_POWER_ALL_R: /* If explicitely state all */
  598. ps = true;
  599. break;
  600. default: /* Otherwise we ignore */
  601. return -EINVAL;
  602. }
  603. if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
  604. return -EINVAL;
  605. if (wrq->flags & IW_POWER_TIMEOUT)
  606. timeout = wrq->value / 1000;
  607. set:
  608. if (ps == sdata->u.mgd.powersave && timeout == conf->dynamic_ps_timeout)
  609. return 0;
  610. sdata->u.mgd.powersave = ps;
  611. conf->dynamic_ps_timeout = timeout;
  612. if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
  613. ieee80211_hw_config(local,
  614. IEEE80211_CONF_CHANGE_DYNPS_TIMEOUT);
  615. ieee80211_recalc_ps(local, -1);
  616. return 0;
  617. }
  618. static int ieee80211_ioctl_giwpower(struct net_device *dev,
  619. struct iw_request_info *info,
  620. union iwreq_data *wrqu,
  621. char *extra)
  622. {
  623. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  624. wrqu->power.disabled = !sdata->u.mgd.powersave;
  625. return 0;
  626. }
  627. static int ieee80211_ioctl_siwauth(struct net_device *dev,
  628. struct iw_request_info *info,
  629. struct iw_param *data, char *extra)
  630. {
  631. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  632. int ret = 0;
  633. switch (data->flags & IW_AUTH_INDEX) {
  634. case IW_AUTH_WPA_VERSION:
  635. case IW_AUTH_CIPHER_GROUP:
  636. case IW_AUTH_WPA_ENABLED:
  637. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  638. case IW_AUTH_KEY_MGMT:
  639. case IW_AUTH_CIPHER_GROUP_MGMT:
  640. break;
  641. case IW_AUTH_CIPHER_PAIRWISE:
  642. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  643. if (data->value & (IW_AUTH_CIPHER_WEP40 |
  644. IW_AUTH_CIPHER_WEP104 | IW_AUTH_CIPHER_TKIP))
  645. sdata->u.mgd.flags |=
  646. IEEE80211_STA_TKIP_WEP_USED;
  647. else
  648. sdata->u.mgd.flags &=
  649. ~IEEE80211_STA_TKIP_WEP_USED;
  650. }
  651. break;
  652. case IW_AUTH_DROP_UNENCRYPTED:
  653. sdata->drop_unencrypted = !!data->value;
  654. break;
  655. case IW_AUTH_PRIVACY_INVOKED:
  656. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  657. ret = -EINVAL;
  658. else {
  659. sdata->u.mgd.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
  660. /*
  661. * Privacy invoked by wpa_supplicant, store the
  662. * value and allow associating to a protected
  663. * network without having a key up front.
  664. */
  665. if (data->value)
  666. sdata->u.mgd.flags |=
  667. IEEE80211_STA_PRIVACY_INVOKED;
  668. }
  669. break;
  670. case IW_AUTH_80211_AUTH_ALG:
  671. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  672. sdata->u.mgd.auth_algs = data->value;
  673. else
  674. ret = -EOPNOTSUPP;
  675. break;
  676. case IW_AUTH_MFP:
  677. if (!(sdata->local->hw.flags & IEEE80211_HW_MFP_CAPABLE)) {
  678. ret = -EOPNOTSUPP;
  679. break;
  680. }
  681. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  682. switch (data->value) {
  683. case IW_AUTH_MFP_DISABLED:
  684. sdata->u.mgd.mfp = IEEE80211_MFP_DISABLED;
  685. break;
  686. case IW_AUTH_MFP_OPTIONAL:
  687. sdata->u.mgd.mfp = IEEE80211_MFP_OPTIONAL;
  688. break;
  689. case IW_AUTH_MFP_REQUIRED:
  690. sdata->u.mgd.mfp = IEEE80211_MFP_REQUIRED;
  691. break;
  692. default:
  693. ret = -EINVAL;
  694. }
  695. } else
  696. ret = -EOPNOTSUPP;
  697. break;
  698. default:
  699. ret = -EOPNOTSUPP;
  700. break;
  701. }
  702. return ret;
  703. }
  704. /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
  705. static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
  706. {
  707. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  708. struct iw_statistics *wstats = &local->wstats;
  709. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  710. struct sta_info *sta = NULL;
  711. rcu_read_lock();
  712. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  713. sta = sta_info_get(local, sdata->u.mgd.bssid);
  714. if (!sta) {
  715. wstats->discard.fragment = 0;
  716. wstats->discard.misc = 0;
  717. wstats->qual.qual = 0;
  718. wstats->qual.level = 0;
  719. wstats->qual.noise = 0;
  720. wstats->qual.updated = IW_QUAL_ALL_INVALID;
  721. } else {
  722. wstats->qual.updated = 0;
  723. /*
  724. * mirror what cfg80211 does for iwrange/scan results,
  725. * otherwise userspace gets confused.
  726. */
  727. if (local->hw.flags & (IEEE80211_HW_SIGNAL_UNSPEC |
  728. IEEE80211_HW_SIGNAL_DBM)) {
  729. wstats->qual.updated |= IW_QUAL_LEVEL_UPDATED;
  730. wstats->qual.updated |= IW_QUAL_QUAL_UPDATED;
  731. } else {
  732. wstats->qual.updated |= IW_QUAL_LEVEL_INVALID;
  733. wstats->qual.updated |= IW_QUAL_QUAL_INVALID;
  734. }
  735. if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
  736. wstats->qual.level = sta->last_signal;
  737. wstats->qual.qual = sta->last_signal;
  738. } else if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
  739. int sig = sta->last_signal;
  740. wstats->qual.updated |= IW_QUAL_DBM;
  741. wstats->qual.level = sig;
  742. if (sig < -110)
  743. sig = -110;
  744. else if (sig > -40)
  745. sig = -40;
  746. wstats->qual.qual = sig + 110;
  747. }
  748. if (local->hw.flags & IEEE80211_HW_NOISE_DBM) {
  749. /*
  750. * This assumes that if driver reports noise, it also
  751. * reports signal in dBm.
  752. */
  753. wstats->qual.noise = sta->last_noise;
  754. wstats->qual.updated |= IW_QUAL_NOISE_UPDATED;
  755. } else {
  756. wstats->qual.updated |= IW_QUAL_NOISE_INVALID;
  757. }
  758. }
  759. rcu_read_unlock();
  760. return wstats;
  761. }
  762. static int ieee80211_ioctl_giwauth(struct net_device *dev,
  763. struct iw_request_info *info,
  764. struct iw_param *data, char *extra)
  765. {
  766. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  767. int ret = 0;
  768. switch (data->flags & IW_AUTH_INDEX) {
  769. case IW_AUTH_80211_AUTH_ALG:
  770. if (sdata->vif.type == NL80211_IFTYPE_STATION)
  771. data->value = sdata->u.mgd.auth_algs;
  772. else
  773. ret = -EOPNOTSUPP;
  774. break;
  775. default:
  776. ret = -EOPNOTSUPP;
  777. break;
  778. }
  779. return ret;
  780. }
  781. static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
  782. struct iw_request_info *info,
  783. struct iw_point *erq, char *extra)
  784. {
  785. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  786. struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
  787. int uninitialized_var(alg), idx, i, remove = 0;
  788. switch (ext->alg) {
  789. case IW_ENCODE_ALG_NONE:
  790. remove = 1;
  791. break;
  792. case IW_ENCODE_ALG_WEP:
  793. alg = ALG_WEP;
  794. break;
  795. case IW_ENCODE_ALG_TKIP:
  796. alg = ALG_TKIP;
  797. break;
  798. case IW_ENCODE_ALG_CCMP:
  799. alg = ALG_CCMP;
  800. break;
  801. case IW_ENCODE_ALG_AES_CMAC:
  802. alg = ALG_AES_CMAC;
  803. break;
  804. default:
  805. return -EOPNOTSUPP;
  806. }
  807. if (erq->flags & IW_ENCODE_DISABLED)
  808. remove = 1;
  809. idx = erq->flags & IW_ENCODE_INDEX;
  810. if (alg == ALG_AES_CMAC) {
  811. if (idx < NUM_DEFAULT_KEYS + 1 ||
  812. idx > NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS) {
  813. idx = -1;
  814. if (!sdata->default_mgmt_key)
  815. idx = 0;
  816. else for (i = NUM_DEFAULT_KEYS;
  817. i < NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS;
  818. i++) {
  819. if (sdata->default_mgmt_key == sdata->keys[i])
  820. {
  821. idx = i;
  822. break;
  823. }
  824. }
  825. if (idx < 0)
  826. return -EINVAL;
  827. } else
  828. idx--;
  829. } else {
  830. if (idx < 1 || idx > 4) {
  831. idx = -1;
  832. if (!sdata->default_key)
  833. idx = 0;
  834. else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  835. if (sdata->default_key == sdata->keys[i]) {
  836. idx = i;
  837. break;
  838. }
  839. }
  840. if (idx < 0)
  841. return -EINVAL;
  842. } else
  843. idx--;
  844. }
  845. return ieee80211_set_encryption(sdata, ext->addr.sa_data, idx, alg,
  846. remove,
  847. ext->ext_flags &
  848. IW_ENCODE_EXT_SET_TX_KEY,
  849. ext->key, ext->key_len);
  850. }
  851. /* Structures to export the Wireless Handlers */
  852. static const iw_handler ieee80211_handler[] =
  853. {
  854. (iw_handler) NULL, /* SIOCSIWCOMMIT */
  855. (iw_handler) cfg80211_wext_giwname, /* SIOCGIWNAME */
  856. (iw_handler) NULL, /* SIOCSIWNWID */
  857. (iw_handler) NULL, /* SIOCGIWNWID */
  858. (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
  859. (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
  860. (iw_handler) cfg80211_wext_siwmode, /* SIOCSIWMODE */
  861. (iw_handler) cfg80211_wext_giwmode, /* SIOCGIWMODE */
  862. (iw_handler) NULL, /* SIOCSIWSENS */
  863. (iw_handler) NULL, /* SIOCGIWSENS */
  864. (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
  865. (iw_handler) cfg80211_wext_giwrange, /* SIOCGIWRANGE */
  866. (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
  867. (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
  868. (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
  869. (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
  870. (iw_handler) NULL, /* SIOCSIWSPY */
  871. (iw_handler) NULL, /* SIOCGIWSPY */
  872. (iw_handler) NULL, /* SIOCSIWTHRSPY */
  873. (iw_handler) NULL, /* SIOCGIWTHRSPY */
  874. (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
  875. (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
  876. (iw_handler) cfg80211_wext_siwmlme, /* SIOCSIWMLME */
  877. (iw_handler) NULL, /* SIOCGIWAPLIST */
  878. (iw_handler) cfg80211_wext_siwscan, /* SIOCSIWSCAN */
  879. (iw_handler) cfg80211_wext_giwscan, /* SIOCGIWSCAN */
  880. (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
  881. (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
  882. (iw_handler) NULL, /* SIOCSIWNICKN */
  883. (iw_handler) NULL, /* SIOCGIWNICKN */
  884. (iw_handler) NULL, /* -- hole -- */
  885. (iw_handler) NULL, /* -- hole -- */
  886. (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
  887. (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
  888. (iw_handler) ieee80211_ioctl_siwrts, /* SIOCSIWRTS */
  889. (iw_handler) ieee80211_ioctl_giwrts, /* SIOCGIWRTS */
  890. (iw_handler) ieee80211_ioctl_siwfrag, /* SIOCSIWFRAG */
  891. (iw_handler) ieee80211_ioctl_giwfrag, /* SIOCGIWFRAG */
  892. (iw_handler) ieee80211_ioctl_siwtxpower, /* SIOCSIWTXPOW */
  893. (iw_handler) ieee80211_ioctl_giwtxpower, /* SIOCGIWTXPOW */
  894. (iw_handler) ieee80211_ioctl_siwretry, /* SIOCSIWRETRY */
  895. (iw_handler) ieee80211_ioctl_giwretry, /* SIOCGIWRETRY */
  896. (iw_handler) ieee80211_ioctl_siwencode, /* SIOCSIWENCODE */
  897. (iw_handler) ieee80211_ioctl_giwencode, /* SIOCGIWENCODE */
  898. (iw_handler) ieee80211_ioctl_siwpower, /* SIOCSIWPOWER */
  899. (iw_handler) ieee80211_ioctl_giwpower, /* SIOCGIWPOWER */
  900. (iw_handler) NULL, /* -- hole -- */
  901. (iw_handler) NULL, /* -- hole -- */
  902. (iw_handler) ieee80211_ioctl_siwgenie, /* SIOCSIWGENIE */
  903. (iw_handler) NULL, /* SIOCGIWGENIE */
  904. (iw_handler) ieee80211_ioctl_siwauth, /* SIOCSIWAUTH */
  905. (iw_handler) ieee80211_ioctl_giwauth, /* SIOCGIWAUTH */
  906. (iw_handler) ieee80211_ioctl_siwencodeext, /* SIOCSIWENCODEEXT */
  907. (iw_handler) NULL, /* SIOCGIWENCODEEXT */
  908. (iw_handler) NULL, /* SIOCSIWPMKSA */
  909. (iw_handler) NULL, /* -- hole -- */
  910. };
  911. const struct iw_handler_def ieee80211_iw_handler_def =
  912. {
  913. .num_standard = ARRAY_SIZE(ieee80211_handler),
  914. .standard = (iw_handler *) ieee80211_handler,
  915. .get_wireless_stats = ieee80211_get_wireless_stats,
  916. };