ieee80211_ioctl.c 29 KB

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