ieee80211_ioctl.c 29 KB

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