ieee80211_ioctl.c 29 KB

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