ieee80211_ioctl.c 29 KB

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