wext.c 30 KB

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