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