ieee80211_ioctl.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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. sta = sta_info_get(local, sdata->u.sta.bssid);
  491. else
  492. return -EOPNOTSUPP;
  493. if (!sta)
  494. return -ENODEV;
  495. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  496. if (sta->txrate_idx < sband->n_bitrates)
  497. rate->value = sband->bitrates[sta->txrate_idx].bitrate;
  498. else
  499. rate->value = 0;
  500. rate->value *= 100000;
  501. return 0;
  502. }
  503. static int ieee80211_ioctl_siwtxpower(struct net_device *dev,
  504. struct iw_request_info *info,
  505. union iwreq_data *data, char *extra)
  506. {
  507. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  508. bool need_reconfig = 0;
  509. int new_power_level;
  510. if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
  511. return -EINVAL;
  512. if (data->txpower.flags & IW_TXPOW_RANGE)
  513. return -EINVAL;
  514. if (data->txpower.fixed) {
  515. new_power_level = data->txpower.value;
  516. } else {
  517. /*
  518. * Automatic power level. Use maximum power for the current
  519. * channel. Should be part of rate control.
  520. */
  521. struct ieee80211_channel* chan = local->hw.conf.channel;
  522. if (!chan)
  523. return -EINVAL;
  524. new_power_level = chan->max_power;
  525. }
  526. if (local->hw.conf.power_level != new_power_level) {
  527. local->hw.conf.power_level = new_power_level;
  528. need_reconfig = 1;
  529. }
  530. if (local->hw.conf.radio_enabled != !(data->txpower.disabled)) {
  531. local->hw.conf.radio_enabled = !(data->txpower.disabled);
  532. need_reconfig = 1;
  533. ieee80211_led_radio(local, local->hw.conf.radio_enabled);
  534. }
  535. if (need_reconfig) {
  536. ieee80211_hw_config(local);
  537. /* The return value of hw_config is not of big interest here,
  538. * as it doesn't say that it failed because of _this_ config
  539. * change or something else. Ignore it. */
  540. }
  541. return 0;
  542. }
  543. static int ieee80211_ioctl_giwtxpower(struct net_device *dev,
  544. struct iw_request_info *info,
  545. union iwreq_data *data, char *extra)
  546. {
  547. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  548. data->txpower.fixed = 1;
  549. data->txpower.disabled = !(local->hw.conf.radio_enabled);
  550. data->txpower.value = local->hw.conf.power_level;
  551. data->txpower.flags = IW_TXPOW_DBM;
  552. return 0;
  553. }
  554. static int ieee80211_ioctl_siwrts(struct net_device *dev,
  555. struct iw_request_info *info,
  556. struct iw_param *rts, char *extra)
  557. {
  558. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  559. if (rts->disabled)
  560. local->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
  561. else if (rts->value < 0 || rts->value > IEEE80211_MAX_RTS_THRESHOLD)
  562. return -EINVAL;
  563. else
  564. local->rts_threshold = rts->value;
  565. /* If the wlan card performs RTS/CTS in hardware/firmware,
  566. * configure it here */
  567. if (local->ops->set_rts_threshold)
  568. local->ops->set_rts_threshold(local_to_hw(local),
  569. local->rts_threshold);
  570. return 0;
  571. }
  572. static int ieee80211_ioctl_giwrts(struct net_device *dev,
  573. struct iw_request_info *info,
  574. struct iw_param *rts, char *extra)
  575. {
  576. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  577. rts->value = local->rts_threshold;
  578. rts->disabled = (rts->value >= IEEE80211_MAX_RTS_THRESHOLD);
  579. rts->fixed = 1;
  580. return 0;
  581. }
  582. static int ieee80211_ioctl_siwfrag(struct net_device *dev,
  583. struct iw_request_info *info,
  584. struct iw_param *frag, char *extra)
  585. {
  586. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  587. if (frag->disabled)
  588. local->fragmentation_threshold = IEEE80211_MAX_FRAG_THRESHOLD;
  589. else if (frag->value < 256 ||
  590. frag->value > IEEE80211_MAX_FRAG_THRESHOLD)
  591. return -EINVAL;
  592. else {
  593. /* Fragment length must be even, so strip LSB. */
  594. local->fragmentation_threshold = frag->value & ~0x1;
  595. }
  596. /* If the wlan card performs fragmentation in hardware/firmware,
  597. * configure it here */
  598. if (local->ops->set_frag_threshold)
  599. local->ops->set_frag_threshold(
  600. local_to_hw(local),
  601. local->fragmentation_threshold);
  602. return 0;
  603. }
  604. static int ieee80211_ioctl_giwfrag(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. frag->value = local->fragmentation_threshold;
  610. frag->disabled = (frag->value >= IEEE80211_MAX_RTS_THRESHOLD);
  611. frag->fixed = 1;
  612. return 0;
  613. }
  614. static int ieee80211_ioctl_siwretry(struct net_device *dev,
  615. struct iw_request_info *info,
  616. struct iw_param *retry, char *extra)
  617. {
  618. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  619. if (retry->disabled ||
  620. (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
  621. return -EINVAL;
  622. if (retry->flags & IW_RETRY_MAX)
  623. local->long_retry_limit = retry->value;
  624. else if (retry->flags & IW_RETRY_MIN)
  625. local->short_retry_limit = retry->value;
  626. else {
  627. local->long_retry_limit = retry->value;
  628. local->short_retry_limit = retry->value;
  629. }
  630. if (local->ops->set_retry_limit) {
  631. return local->ops->set_retry_limit(
  632. local_to_hw(local),
  633. local->short_retry_limit,
  634. local->long_retry_limit);
  635. }
  636. return 0;
  637. }
  638. static int ieee80211_ioctl_giwretry(struct net_device *dev,
  639. struct iw_request_info *info,
  640. struct iw_param *retry, char *extra)
  641. {
  642. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  643. retry->disabled = 0;
  644. if (retry->flags == 0 || retry->flags & IW_RETRY_MIN) {
  645. /* first return min value, iwconfig will ask max value
  646. * later if needed */
  647. retry->flags |= IW_RETRY_LIMIT;
  648. retry->value = local->short_retry_limit;
  649. if (local->long_retry_limit != local->short_retry_limit)
  650. retry->flags |= IW_RETRY_MIN;
  651. return 0;
  652. }
  653. if (retry->flags & IW_RETRY_MAX) {
  654. retry->flags = IW_RETRY_LIMIT | IW_RETRY_MAX;
  655. retry->value = local->long_retry_limit;
  656. }
  657. return 0;
  658. }
  659. static int ieee80211_ioctl_siwmlme(struct net_device *dev,
  660. struct iw_request_info *info,
  661. struct iw_point *data, char *extra)
  662. {
  663. struct ieee80211_sub_if_data *sdata;
  664. struct iw_mlme *mlme = (struct iw_mlme *) extra;
  665. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  666. if (sdata->vif.type != IEEE80211_IF_TYPE_STA &&
  667. sdata->vif.type != IEEE80211_IF_TYPE_IBSS)
  668. return -EINVAL;
  669. switch (mlme->cmd) {
  670. case IW_MLME_DEAUTH:
  671. /* TODO: mlme->addr.sa_data */
  672. return ieee80211_sta_deauthenticate(dev, mlme->reason_code);
  673. case IW_MLME_DISASSOC:
  674. /* TODO: mlme->addr.sa_data */
  675. return ieee80211_sta_disassociate(dev, mlme->reason_code);
  676. default:
  677. return -EOPNOTSUPP;
  678. }
  679. }
  680. static int ieee80211_ioctl_siwencode(struct net_device *dev,
  681. struct iw_request_info *info,
  682. struct iw_point *erq, char *keybuf)
  683. {
  684. struct ieee80211_sub_if_data *sdata;
  685. int idx, i, alg = ALG_WEP;
  686. u8 bcaddr[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  687. int remove = 0;
  688. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  689. idx = erq->flags & IW_ENCODE_INDEX;
  690. if (idx == 0) {
  691. if (sdata->default_key)
  692. for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  693. if (sdata->default_key == sdata->keys[i]) {
  694. idx = i;
  695. break;
  696. }
  697. }
  698. } else if (idx < 1 || idx > 4)
  699. return -EINVAL;
  700. else
  701. idx--;
  702. if (erq->flags & IW_ENCODE_DISABLED)
  703. remove = 1;
  704. else if (erq->length == 0) {
  705. /* No key data - just set the default TX key index */
  706. ieee80211_set_default_key(sdata, idx);
  707. return 0;
  708. }
  709. return ieee80211_set_encryption(
  710. dev, bcaddr,
  711. idx, alg, remove,
  712. !sdata->default_key,
  713. keybuf, erq->length);
  714. }
  715. static int ieee80211_ioctl_giwencode(struct net_device *dev,
  716. struct iw_request_info *info,
  717. struct iw_point *erq, char *key)
  718. {
  719. struct ieee80211_sub_if_data *sdata;
  720. int idx, i;
  721. sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  722. idx = erq->flags & IW_ENCODE_INDEX;
  723. if (idx < 1 || idx > 4) {
  724. idx = -1;
  725. if (!sdata->default_key)
  726. idx = 0;
  727. else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  728. if (sdata->default_key == sdata->keys[i]) {
  729. idx = i;
  730. break;
  731. }
  732. }
  733. if (idx < 0)
  734. return -EINVAL;
  735. } else
  736. idx--;
  737. erq->flags = idx + 1;
  738. if (!sdata->keys[idx]) {
  739. erq->length = 0;
  740. erq->flags |= IW_ENCODE_DISABLED;
  741. return 0;
  742. }
  743. memcpy(key, sdata->keys[idx]->conf.key,
  744. min_t(int, erq->length, sdata->keys[idx]->conf.keylen));
  745. erq->length = sdata->keys[idx]->conf.keylen;
  746. erq->flags |= IW_ENCODE_ENABLED;
  747. return 0;
  748. }
  749. static int ieee80211_ioctl_siwauth(struct net_device *dev,
  750. struct iw_request_info *info,
  751. struct iw_param *data, char *extra)
  752. {
  753. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  754. int ret = 0;
  755. switch (data->flags & IW_AUTH_INDEX) {
  756. case IW_AUTH_WPA_VERSION:
  757. case IW_AUTH_CIPHER_PAIRWISE:
  758. case IW_AUTH_CIPHER_GROUP:
  759. case IW_AUTH_WPA_ENABLED:
  760. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  761. case IW_AUTH_KEY_MGMT:
  762. break;
  763. case IW_AUTH_DROP_UNENCRYPTED:
  764. sdata->drop_unencrypted = !!data->value;
  765. break;
  766. case IW_AUTH_PRIVACY_INVOKED:
  767. if (sdata->vif.type != IEEE80211_IF_TYPE_STA)
  768. ret = -EINVAL;
  769. else {
  770. sdata->u.sta.flags &= ~IEEE80211_STA_PRIVACY_INVOKED;
  771. /*
  772. * Privacy invoked by wpa_supplicant, store the
  773. * value and allow associating to a protected
  774. * network without having a key up front.
  775. */
  776. if (data->value)
  777. sdata->u.sta.flags |=
  778. IEEE80211_STA_PRIVACY_INVOKED;
  779. }
  780. break;
  781. case IW_AUTH_80211_AUTH_ALG:
  782. if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  783. sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
  784. sdata->u.sta.auth_algs = data->value;
  785. else
  786. ret = -EOPNOTSUPP;
  787. break;
  788. default:
  789. ret = -EOPNOTSUPP;
  790. break;
  791. }
  792. return ret;
  793. }
  794. /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
  795. static struct iw_statistics *ieee80211_get_wireless_stats(struct net_device *dev)
  796. {
  797. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  798. struct iw_statistics *wstats = &local->wstats;
  799. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  800. struct sta_info *sta = NULL;
  801. if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  802. sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
  803. sta = sta_info_get(local, sdata->u.sta.bssid);
  804. if (!sta) {
  805. wstats->discard.fragment = 0;
  806. wstats->discard.misc = 0;
  807. wstats->qual.qual = 0;
  808. wstats->qual.level = 0;
  809. wstats->qual.noise = 0;
  810. wstats->qual.updated = IW_QUAL_ALL_INVALID;
  811. } else {
  812. wstats->qual.level = sta->last_rssi;
  813. wstats->qual.qual = sta->last_signal;
  814. wstats->qual.noise = sta->last_noise;
  815. wstats->qual.updated = local->wstats_flags;
  816. }
  817. return wstats;
  818. }
  819. static int ieee80211_ioctl_giwauth(struct net_device *dev,
  820. struct iw_request_info *info,
  821. struct iw_param *data, char *extra)
  822. {
  823. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  824. int ret = 0;
  825. switch (data->flags & IW_AUTH_INDEX) {
  826. case IW_AUTH_80211_AUTH_ALG:
  827. if (sdata->vif.type == IEEE80211_IF_TYPE_STA ||
  828. sdata->vif.type == IEEE80211_IF_TYPE_IBSS)
  829. data->value = sdata->u.sta.auth_algs;
  830. else
  831. ret = -EOPNOTSUPP;
  832. break;
  833. default:
  834. ret = -EOPNOTSUPP;
  835. break;
  836. }
  837. return ret;
  838. }
  839. static int ieee80211_ioctl_siwencodeext(struct net_device *dev,
  840. struct iw_request_info *info,
  841. struct iw_point *erq, char *extra)
  842. {
  843. struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
  844. struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
  845. int uninitialized_var(alg), idx, i, remove = 0;
  846. switch (ext->alg) {
  847. case IW_ENCODE_ALG_NONE:
  848. remove = 1;
  849. break;
  850. case IW_ENCODE_ALG_WEP:
  851. alg = ALG_WEP;
  852. break;
  853. case IW_ENCODE_ALG_TKIP:
  854. alg = ALG_TKIP;
  855. break;
  856. case IW_ENCODE_ALG_CCMP:
  857. alg = ALG_CCMP;
  858. break;
  859. default:
  860. return -EOPNOTSUPP;
  861. }
  862. if (erq->flags & IW_ENCODE_DISABLED)
  863. remove = 1;
  864. idx = erq->flags & IW_ENCODE_INDEX;
  865. if (idx < 1 || idx > 4) {
  866. idx = -1;
  867. if (!sdata->default_key)
  868. idx = 0;
  869. else for (i = 0; i < NUM_DEFAULT_KEYS; i++) {
  870. if (sdata->default_key == sdata->keys[i]) {
  871. idx = i;
  872. break;
  873. }
  874. }
  875. if (idx < 0)
  876. return -EINVAL;
  877. } else
  878. idx--;
  879. return ieee80211_set_encryption(dev, ext->addr.sa_data, idx, alg,
  880. remove,
  881. ext->ext_flags &
  882. IW_ENCODE_EXT_SET_TX_KEY,
  883. ext->key, ext->key_len);
  884. }
  885. /* Structures to export the Wireless Handlers */
  886. static const iw_handler ieee80211_handler[] =
  887. {
  888. (iw_handler) NULL, /* SIOCSIWCOMMIT */
  889. (iw_handler) ieee80211_ioctl_giwname, /* SIOCGIWNAME */
  890. (iw_handler) NULL, /* SIOCSIWNWID */
  891. (iw_handler) NULL, /* SIOCGIWNWID */
  892. (iw_handler) ieee80211_ioctl_siwfreq, /* SIOCSIWFREQ */
  893. (iw_handler) ieee80211_ioctl_giwfreq, /* SIOCGIWFREQ */
  894. (iw_handler) ieee80211_ioctl_siwmode, /* SIOCSIWMODE */
  895. (iw_handler) ieee80211_ioctl_giwmode, /* SIOCGIWMODE */
  896. (iw_handler) NULL, /* SIOCSIWSENS */
  897. (iw_handler) NULL, /* SIOCGIWSENS */
  898. (iw_handler) NULL /* not used */, /* SIOCSIWRANGE */
  899. (iw_handler) ieee80211_ioctl_giwrange, /* SIOCGIWRANGE */
  900. (iw_handler) NULL /* not used */, /* SIOCSIWPRIV */
  901. (iw_handler) NULL /* kernel code */, /* SIOCGIWPRIV */
  902. (iw_handler) NULL /* not used */, /* SIOCSIWSTATS */
  903. (iw_handler) NULL /* kernel code */, /* SIOCGIWSTATS */
  904. (iw_handler) NULL, /* SIOCSIWSPY */
  905. (iw_handler) NULL, /* SIOCGIWSPY */
  906. (iw_handler) NULL, /* SIOCSIWTHRSPY */
  907. (iw_handler) NULL, /* SIOCGIWTHRSPY */
  908. (iw_handler) ieee80211_ioctl_siwap, /* SIOCSIWAP */
  909. (iw_handler) ieee80211_ioctl_giwap, /* SIOCGIWAP */
  910. (iw_handler) ieee80211_ioctl_siwmlme, /* SIOCSIWMLME */
  911. (iw_handler) NULL, /* SIOCGIWAPLIST */
  912. (iw_handler) ieee80211_ioctl_siwscan, /* SIOCSIWSCAN */
  913. (iw_handler) ieee80211_ioctl_giwscan, /* SIOCGIWSCAN */
  914. (iw_handler) ieee80211_ioctl_siwessid, /* SIOCSIWESSID */
  915. (iw_handler) ieee80211_ioctl_giwessid, /* SIOCGIWESSID */
  916. (iw_handler) NULL, /* SIOCSIWNICKN */
  917. (iw_handler) NULL, /* SIOCGIWNICKN */
  918. (iw_handler) NULL, /* -- hole -- */
  919. (iw_handler) NULL, /* -- hole -- */
  920. (iw_handler) ieee80211_ioctl_siwrate, /* SIOCSIWRATE */
  921. (iw_handler) ieee80211_ioctl_giwrate, /* SIOCGIWRATE */
  922. (iw_handler) ieee80211_ioctl_siwrts, /* SIOCSIWRTS */
  923. (iw_handler) ieee80211_ioctl_giwrts, /* SIOCGIWRTS */
  924. (iw_handler) ieee80211_ioctl_siwfrag, /* SIOCSIWFRAG */
  925. (iw_handler) ieee80211_ioctl_giwfrag, /* SIOCGIWFRAG */
  926. (iw_handler) ieee80211_ioctl_siwtxpower, /* SIOCSIWTXPOW */
  927. (iw_handler) ieee80211_ioctl_giwtxpower, /* SIOCGIWTXPOW */
  928. (iw_handler) ieee80211_ioctl_siwretry, /* SIOCSIWRETRY */
  929. (iw_handler) ieee80211_ioctl_giwretry, /* SIOCGIWRETRY */
  930. (iw_handler) ieee80211_ioctl_siwencode, /* SIOCSIWENCODE */
  931. (iw_handler) ieee80211_ioctl_giwencode, /* SIOCGIWENCODE */
  932. (iw_handler) NULL, /* SIOCSIWPOWER */
  933. (iw_handler) NULL, /* SIOCGIWPOWER */
  934. (iw_handler) NULL, /* -- hole -- */
  935. (iw_handler) NULL, /* -- hole -- */
  936. (iw_handler) ieee80211_ioctl_siwgenie, /* SIOCSIWGENIE */
  937. (iw_handler) NULL, /* SIOCGIWGENIE */
  938. (iw_handler) ieee80211_ioctl_siwauth, /* SIOCSIWAUTH */
  939. (iw_handler) ieee80211_ioctl_giwauth, /* SIOCGIWAUTH */
  940. (iw_handler) ieee80211_ioctl_siwencodeext, /* SIOCSIWENCODEEXT */
  941. (iw_handler) NULL, /* SIOCGIWENCODEEXT */
  942. (iw_handler) NULL, /* SIOCSIWPMKSA */
  943. (iw_handler) NULL, /* -- hole -- */
  944. };
  945. const struct iw_handler_def ieee80211_iw_handler_def =
  946. {
  947. .num_standard = ARRAY_SIZE(ieee80211_handler),
  948. .standard = (iw_handler *) ieee80211_handler,
  949. .get_wireless_stats = ieee80211_get_wireless_stats,
  950. };