wext-compat.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. /*
  2. * cfg80211 - wext compat code
  3. *
  4. * This is temporary code until all wireless functionality is migrated
  5. * into cfg80211, when that happens all the exports here go away and
  6. * we directly assign the wireless handlers of wireless interfaces.
  7. *
  8. * Copyright 2008-2009 Johannes Berg <johannes@sipsolutions.net>
  9. */
  10. #include <linux/wireless.h>
  11. #include <linux/nl80211.h>
  12. #include <linux/if_arp.h>
  13. #include <linux/etherdevice.h>
  14. #include <net/iw_handler.h>
  15. #include <net/cfg80211.h>
  16. #include "core.h"
  17. int cfg80211_wext_giwname(struct net_device *dev,
  18. struct iw_request_info *info,
  19. char *name, char *extra)
  20. {
  21. struct wireless_dev *wdev = dev->ieee80211_ptr;
  22. struct ieee80211_supported_band *sband;
  23. bool is_ht = false, is_a = false, is_b = false, is_g = false;
  24. if (!wdev)
  25. return -EOPNOTSUPP;
  26. sband = wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
  27. if (sband) {
  28. is_a = true;
  29. is_ht |= sband->ht_cap.ht_supported;
  30. }
  31. sband = wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
  32. if (sband) {
  33. int i;
  34. /* Check for mandatory rates */
  35. for (i = 0; i < sband->n_bitrates; i++) {
  36. if (sband->bitrates[i].bitrate == 10)
  37. is_b = true;
  38. if (sband->bitrates[i].bitrate == 60)
  39. is_g = true;
  40. }
  41. is_ht |= sband->ht_cap.ht_supported;
  42. }
  43. strcpy(name, "IEEE 802.11");
  44. if (is_a)
  45. strcat(name, "a");
  46. if (is_b)
  47. strcat(name, "b");
  48. if (is_g)
  49. strcat(name, "g");
  50. if (is_ht)
  51. strcat(name, "n");
  52. return 0;
  53. }
  54. EXPORT_SYMBOL_GPL(cfg80211_wext_giwname);
  55. int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
  56. u32 *mode, char *extra)
  57. {
  58. struct wireless_dev *wdev = dev->ieee80211_ptr;
  59. struct cfg80211_registered_device *rdev;
  60. struct vif_params vifparams;
  61. enum nl80211_iftype type;
  62. int ret;
  63. if (!wdev)
  64. return -EOPNOTSUPP;
  65. rdev = wiphy_to_dev(wdev->wiphy);
  66. if (!rdev->ops->change_virtual_intf)
  67. return -EOPNOTSUPP;
  68. /* don't support changing VLANs, you just re-create them */
  69. if (wdev->iftype == NL80211_IFTYPE_AP_VLAN)
  70. return -EOPNOTSUPP;
  71. switch (*mode) {
  72. case IW_MODE_INFRA:
  73. type = NL80211_IFTYPE_STATION;
  74. break;
  75. case IW_MODE_ADHOC:
  76. type = NL80211_IFTYPE_ADHOC;
  77. break;
  78. case IW_MODE_REPEAT:
  79. type = NL80211_IFTYPE_WDS;
  80. break;
  81. case IW_MODE_MONITOR:
  82. type = NL80211_IFTYPE_MONITOR;
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. if (type == wdev->iftype)
  88. return 0;
  89. memset(&vifparams, 0, sizeof(vifparams));
  90. ret = rdev->ops->change_virtual_intf(wdev->wiphy, dev, type,
  91. NULL, &vifparams);
  92. WARN_ON(!ret && wdev->iftype != type);
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(cfg80211_wext_siwmode);
  96. int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
  97. u32 *mode, char *extra)
  98. {
  99. struct wireless_dev *wdev = dev->ieee80211_ptr;
  100. if (!wdev)
  101. return -EOPNOTSUPP;
  102. switch (wdev->iftype) {
  103. case NL80211_IFTYPE_AP:
  104. *mode = IW_MODE_MASTER;
  105. break;
  106. case NL80211_IFTYPE_STATION:
  107. *mode = IW_MODE_INFRA;
  108. break;
  109. case NL80211_IFTYPE_ADHOC:
  110. *mode = IW_MODE_ADHOC;
  111. break;
  112. case NL80211_IFTYPE_MONITOR:
  113. *mode = IW_MODE_MONITOR;
  114. break;
  115. case NL80211_IFTYPE_WDS:
  116. *mode = IW_MODE_REPEAT;
  117. break;
  118. case NL80211_IFTYPE_AP_VLAN:
  119. *mode = IW_MODE_SECOND; /* FIXME */
  120. break;
  121. default:
  122. *mode = IW_MODE_AUTO;
  123. break;
  124. }
  125. return 0;
  126. }
  127. EXPORT_SYMBOL_GPL(cfg80211_wext_giwmode);
  128. int cfg80211_wext_giwrange(struct net_device *dev,
  129. struct iw_request_info *info,
  130. struct iw_point *data, char *extra)
  131. {
  132. struct wireless_dev *wdev = dev->ieee80211_ptr;
  133. struct iw_range *range = (struct iw_range *) extra;
  134. enum ieee80211_band band;
  135. int i, c = 0;
  136. if (!wdev)
  137. return -EOPNOTSUPP;
  138. data->length = sizeof(struct iw_range);
  139. memset(range, 0, sizeof(struct iw_range));
  140. range->we_version_compiled = WIRELESS_EXT;
  141. range->we_version_source = 21;
  142. range->retry_capa = IW_RETRY_LIMIT;
  143. range->retry_flags = IW_RETRY_LIMIT;
  144. range->min_retry = 0;
  145. range->max_retry = 255;
  146. range->min_rts = 0;
  147. range->max_rts = 2347;
  148. range->min_frag = 256;
  149. range->max_frag = 2346;
  150. range->max_encoding_tokens = 4;
  151. range->max_qual.updated = IW_QUAL_NOISE_INVALID;
  152. switch (wdev->wiphy->signal_type) {
  153. case CFG80211_SIGNAL_TYPE_NONE:
  154. break;
  155. case CFG80211_SIGNAL_TYPE_MBM:
  156. range->max_qual.level = -110;
  157. range->max_qual.qual = 70;
  158. range->avg_qual.qual = 35;
  159. range->max_qual.updated |= IW_QUAL_DBM;
  160. range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
  161. range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
  162. break;
  163. case CFG80211_SIGNAL_TYPE_UNSPEC:
  164. range->max_qual.level = 100;
  165. range->max_qual.qual = 100;
  166. range->avg_qual.qual = 50;
  167. range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
  168. range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
  169. break;
  170. }
  171. range->avg_qual.level = range->max_qual.level / 2;
  172. range->avg_qual.noise = range->max_qual.noise / 2;
  173. range->avg_qual.updated = range->max_qual.updated;
  174. for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
  175. switch (wdev->wiphy->cipher_suites[i]) {
  176. case WLAN_CIPHER_SUITE_TKIP:
  177. range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
  178. IW_ENC_CAPA_WPA);
  179. break;
  180. case WLAN_CIPHER_SUITE_CCMP:
  181. range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
  182. IW_ENC_CAPA_WPA2);
  183. break;
  184. case WLAN_CIPHER_SUITE_WEP40:
  185. range->encoding_size[range->num_encoding_sizes++] =
  186. WLAN_KEY_LEN_WEP40;
  187. break;
  188. case WLAN_CIPHER_SUITE_WEP104:
  189. range->encoding_size[range->num_encoding_sizes++] =
  190. WLAN_KEY_LEN_WEP104;
  191. break;
  192. }
  193. }
  194. for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
  195. struct ieee80211_supported_band *sband;
  196. sband = wdev->wiphy->bands[band];
  197. if (!sband)
  198. continue;
  199. for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
  200. struct ieee80211_channel *chan = &sband->channels[i];
  201. if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
  202. range->freq[c].i =
  203. ieee80211_frequency_to_channel(
  204. chan->center_freq);
  205. range->freq[c].m = chan->center_freq;
  206. range->freq[c].e = 6;
  207. c++;
  208. }
  209. }
  210. }
  211. range->num_channels = c;
  212. range->num_frequency = c;
  213. IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
  214. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
  215. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
  216. if (wdev->wiphy->max_scan_ssids > 0)
  217. range->scan_capa |= IW_SCAN_CAPA_ESSID;
  218. return 0;
  219. }
  220. EXPORT_SYMBOL_GPL(cfg80211_wext_giwrange);
  221. /**
  222. * cfg80211_wext_freq - get wext frequency for non-"auto"
  223. * @wiphy: the wiphy
  224. * @freq: the wext freq encoding
  225. *
  226. * Returns a channel, %NULL for auto, or an ERR_PTR for errors!
  227. */
  228. struct ieee80211_channel *cfg80211_wext_freq(struct wiphy *wiphy,
  229. struct iw_freq *freq)
  230. {
  231. struct ieee80211_channel *chan;
  232. int f;
  233. /*
  234. * Parse frequency - return NULL for auto and
  235. * -EINVAL for impossible things.
  236. */
  237. if (freq->e == 0) {
  238. if (freq->m < 0)
  239. return NULL;
  240. f = ieee80211_channel_to_frequency(freq->m);
  241. } else {
  242. int i, div = 1000000;
  243. for (i = 0; i < freq->e; i++)
  244. div /= 10;
  245. if (div <= 0)
  246. return ERR_PTR(-EINVAL);
  247. f = freq->m / div;
  248. }
  249. /*
  250. * Look up channel struct and return -EINVAL when
  251. * it cannot be found.
  252. */
  253. chan = ieee80211_get_channel(wiphy, f);
  254. if (!chan)
  255. return ERR_PTR(-EINVAL);
  256. return chan;
  257. }
  258. EXPORT_SYMBOL_GPL(cfg80211_wext_freq);
  259. int cfg80211_wext_siwrts(struct net_device *dev,
  260. struct iw_request_info *info,
  261. struct iw_param *rts, char *extra)
  262. {
  263. struct wireless_dev *wdev = dev->ieee80211_ptr;
  264. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  265. u32 orts = wdev->wiphy->rts_threshold;
  266. int err;
  267. if (rts->disabled || !rts->fixed)
  268. wdev->wiphy->rts_threshold = (u32) -1;
  269. else if (rts->value < 0)
  270. return -EINVAL;
  271. else
  272. wdev->wiphy->rts_threshold = rts->value;
  273. err = rdev->ops->set_wiphy_params(wdev->wiphy,
  274. WIPHY_PARAM_RTS_THRESHOLD);
  275. if (err)
  276. wdev->wiphy->rts_threshold = orts;
  277. return err;
  278. }
  279. EXPORT_SYMBOL_GPL(cfg80211_wext_siwrts);
  280. int cfg80211_wext_giwrts(struct net_device *dev,
  281. struct iw_request_info *info,
  282. struct iw_param *rts, char *extra)
  283. {
  284. struct wireless_dev *wdev = dev->ieee80211_ptr;
  285. rts->value = wdev->wiphy->rts_threshold;
  286. rts->disabled = rts->value == (u32) -1;
  287. rts->fixed = 1;
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(cfg80211_wext_giwrts);
  291. int cfg80211_wext_siwfrag(struct net_device *dev,
  292. struct iw_request_info *info,
  293. struct iw_param *frag, char *extra)
  294. {
  295. struct wireless_dev *wdev = dev->ieee80211_ptr;
  296. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  297. u32 ofrag = wdev->wiphy->frag_threshold;
  298. int err;
  299. if (frag->disabled || !frag->fixed)
  300. wdev->wiphy->frag_threshold = (u32) -1;
  301. else if (frag->value < 256)
  302. return -EINVAL;
  303. else {
  304. /* Fragment length must be even, so strip LSB. */
  305. wdev->wiphy->frag_threshold = frag->value & ~0x1;
  306. }
  307. err = rdev->ops->set_wiphy_params(wdev->wiphy,
  308. WIPHY_PARAM_FRAG_THRESHOLD);
  309. if (err)
  310. wdev->wiphy->frag_threshold = ofrag;
  311. return err;
  312. }
  313. EXPORT_SYMBOL_GPL(cfg80211_wext_siwfrag);
  314. int cfg80211_wext_giwfrag(struct net_device *dev,
  315. struct iw_request_info *info,
  316. struct iw_param *frag, char *extra)
  317. {
  318. struct wireless_dev *wdev = dev->ieee80211_ptr;
  319. frag->value = wdev->wiphy->frag_threshold;
  320. frag->disabled = frag->value == (u32) -1;
  321. frag->fixed = 1;
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(cfg80211_wext_giwfrag);
  325. int cfg80211_wext_siwretry(struct net_device *dev,
  326. struct iw_request_info *info,
  327. struct iw_param *retry, char *extra)
  328. {
  329. struct wireless_dev *wdev = dev->ieee80211_ptr;
  330. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  331. u32 changed = 0;
  332. u8 olong = wdev->wiphy->retry_long;
  333. u8 oshort = wdev->wiphy->retry_short;
  334. int err;
  335. if (retry->disabled ||
  336. (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
  337. return -EINVAL;
  338. if (retry->flags & IW_RETRY_LONG) {
  339. wdev->wiphy->retry_long = retry->value;
  340. changed |= WIPHY_PARAM_RETRY_LONG;
  341. } else if (retry->flags & IW_RETRY_SHORT) {
  342. wdev->wiphy->retry_short = retry->value;
  343. changed |= WIPHY_PARAM_RETRY_SHORT;
  344. } else {
  345. wdev->wiphy->retry_short = retry->value;
  346. wdev->wiphy->retry_long = retry->value;
  347. changed |= WIPHY_PARAM_RETRY_LONG;
  348. changed |= WIPHY_PARAM_RETRY_SHORT;
  349. }
  350. if (!changed)
  351. return 0;
  352. err = rdev->ops->set_wiphy_params(wdev->wiphy, changed);
  353. if (err) {
  354. wdev->wiphy->retry_short = oshort;
  355. wdev->wiphy->retry_long = olong;
  356. }
  357. return err;
  358. }
  359. EXPORT_SYMBOL_GPL(cfg80211_wext_siwretry);
  360. int cfg80211_wext_giwretry(struct net_device *dev,
  361. struct iw_request_info *info,
  362. struct iw_param *retry, char *extra)
  363. {
  364. struct wireless_dev *wdev = dev->ieee80211_ptr;
  365. retry->disabled = 0;
  366. if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
  367. /*
  368. * First return short value, iwconfig will ask long value
  369. * later if needed
  370. */
  371. retry->flags |= IW_RETRY_LIMIT;
  372. retry->value = wdev->wiphy->retry_short;
  373. if (wdev->wiphy->retry_long != wdev->wiphy->retry_short)
  374. retry->flags |= IW_RETRY_LONG;
  375. return 0;
  376. }
  377. if (retry->flags & IW_RETRY_LONG) {
  378. retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
  379. retry->value = wdev->wiphy->retry_long;
  380. }
  381. return 0;
  382. }
  383. EXPORT_SYMBOL_GPL(cfg80211_wext_giwretry);
  384. static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
  385. struct net_device *dev, const u8 *addr,
  386. bool remove, bool tx_key, int idx,
  387. struct key_params *params)
  388. {
  389. struct wireless_dev *wdev = dev->ieee80211_ptr;
  390. int err;
  391. if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  392. if (!rdev->ops->set_default_mgmt_key)
  393. return -EOPNOTSUPP;
  394. if (idx < 4 || idx > 5)
  395. return -EINVAL;
  396. } else if (idx < 0 || idx > 3)
  397. return -EINVAL;
  398. if (remove) {
  399. err = rdev->ops->del_key(&rdev->wiphy, dev, idx, addr);
  400. if (!err) {
  401. if (idx == wdev->wext.default_key)
  402. wdev->wext.default_key = -1;
  403. else if (idx == wdev->wext.default_mgmt_key)
  404. wdev->wext.default_mgmt_key = -1;
  405. }
  406. /*
  407. * Applications using wireless extensions expect to be
  408. * able to delete keys that don't exist, so allow that.
  409. */
  410. if (err == -ENOENT)
  411. return 0;
  412. return err;
  413. } else {
  414. if (addr)
  415. tx_key = false;
  416. if (cfg80211_validate_key_settings(params, idx, addr))
  417. return -EINVAL;
  418. err = rdev->ops->add_key(&rdev->wiphy, dev, idx, addr, params);
  419. if (err)
  420. return err;
  421. if (tx_key || (!addr && wdev->wext.default_key == -1)) {
  422. err = rdev->ops->set_default_key(&rdev->wiphy,
  423. dev, idx);
  424. if (!err)
  425. wdev->wext.default_key = idx;
  426. return err;
  427. }
  428. if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
  429. (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
  430. err = rdev->ops->set_default_mgmt_key(&rdev->wiphy,
  431. dev, idx);
  432. if (!err)
  433. wdev->wext.default_mgmt_key = idx;
  434. return err;
  435. }
  436. return 0;
  437. }
  438. }
  439. int cfg80211_wext_siwencode(struct net_device *dev,
  440. struct iw_request_info *info,
  441. struct iw_point *erq, char *keybuf)
  442. {
  443. struct wireless_dev *wdev = dev->ieee80211_ptr;
  444. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  445. int idx, err;
  446. bool remove = false;
  447. struct key_params params;
  448. /* no use -- only MFP (set_default_mgmt_key) is optional */
  449. if (!rdev->ops->del_key ||
  450. !rdev->ops->add_key ||
  451. !rdev->ops->set_default_key)
  452. return -EOPNOTSUPP;
  453. idx = erq->flags & IW_ENCODE_INDEX;
  454. if (idx == 0) {
  455. idx = wdev->wext.default_key;
  456. if (idx < 0)
  457. idx = 0;
  458. } else if (idx < 1 || idx > 4)
  459. return -EINVAL;
  460. else
  461. idx--;
  462. if (erq->flags & IW_ENCODE_DISABLED)
  463. remove = true;
  464. else if (erq->length == 0) {
  465. /* No key data - just set the default TX key index */
  466. err = rdev->ops->set_default_key(&rdev->wiphy, dev, idx);
  467. if (!err)
  468. wdev->wext.default_key = idx;
  469. return err;
  470. }
  471. memset(&params, 0, sizeof(params));
  472. params.key = keybuf;
  473. params.key_len = erq->length;
  474. if (erq->length == 5)
  475. params.cipher = WLAN_CIPHER_SUITE_WEP40;
  476. else if (erq->length == 13)
  477. params.cipher = WLAN_CIPHER_SUITE_WEP104;
  478. else if (!remove)
  479. return -EINVAL;
  480. return cfg80211_set_encryption(rdev, dev, NULL, remove,
  481. wdev->wext.default_key == -1,
  482. idx, &params);
  483. }
  484. EXPORT_SYMBOL_GPL(cfg80211_wext_siwencode);
  485. int cfg80211_wext_siwencodeext(struct net_device *dev,
  486. struct iw_request_info *info,
  487. struct iw_point *erq, char *extra)
  488. {
  489. struct wireless_dev *wdev = dev->ieee80211_ptr;
  490. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  491. struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
  492. const u8 *addr;
  493. int idx;
  494. bool remove = false;
  495. struct key_params params;
  496. u32 cipher;
  497. /* no use -- only MFP (set_default_mgmt_key) is optional */
  498. if (!rdev->ops->del_key ||
  499. !rdev->ops->add_key ||
  500. !rdev->ops->set_default_key)
  501. return -EOPNOTSUPP;
  502. switch (ext->alg) {
  503. case IW_ENCODE_ALG_NONE:
  504. remove = true;
  505. cipher = 0;
  506. break;
  507. case IW_ENCODE_ALG_WEP:
  508. if (ext->key_len == 5)
  509. cipher = WLAN_CIPHER_SUITE_WEP40;
  510. else if (ext->key_len == 13)
  511. cipher = WLAN_CIPHER_SUITE_WEP104;
  512. else
  513. return -EINVAL;
  514. break;
  515. case IW_ENCODE_ALG_TKIP:
  516. cipher = WLAN_CIPHER_SUITE_TKIP;
  517. break;
  518. case IW_ENCODE_ALG_CCMP:
  519. cipher = WLAN_CIPHER_SUITE_CCMP;
  520. break;
  521. case IW_ENCODE_ALG_AES_CMAC:
  522. cipher = WLAN_CIPHER_SUITE_AES_CMAC;
  523. break;
  524. default:
  525. return -EOPNOTSUPP;
  526. }
  527. if (erq->flags & IW_ENCODE_DISABLED)
  528. remove = true;
  529. idx = erq->flags & IW_ENCODE_INDEX;
  530. if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  531. if (idx < 4 || idx > 5) {
  532. idx = wdev->wext.default_mgmt_key;
  533. if (idx < 0)
  534. return -EINVAL;
  535. } else
  536. idx--;
  537. } else {
  538. if (idx < 1 || idx > 4) {
  539. idx = wdev->wext.default_key;
  540. if (idx < 0)
  541. return -EINVAL;
  542. } else
  543. idx--;
  544. }
  545. addr = ext->addr.sa_data;
  546. if (is_broadcast_ether_addr(addr))
  547. addr = NULL;
  548. memset(&params, 0, sizeof(params));
  549. params.key = ext->key;
  550. params.key_len = ext->key_len;
  551. params.cipher = cipher;
  552. if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
  553. params.seq = ext->rx_seq;
  554. params.seq_len = 6;
  555. }
  556. return cfg80211_set_encryption(
  557. rdev, dev, addr, remove,
  558. ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
  559. idx, &params);
  560. }
  561. EXPORT_SYMBOL_GPL(cfg80211_wext_siwencodeext);
  562. struct giwencode_cookie {
  563. size_t buflen;
  564. char *keybuf;
  565. };
  566. static void giwencode_get_key_cb(void *cookie, struct key_params *params)
  567. {
  568. struct giwencode_cookie *data = cookie;
  569. if (!params->key) {
  570. data->buflen = 0;
  571. return;
  572. }
  573. data->buflen = min_t(size_t, data->buflen, params->key_len);
  574. memcpy(data->keybuf, params->key, data->buflen);
  575. }
  576. int cfg80211_wext_giwencode(struct net_device *dev,
  577. struct iw_request_info *info,
  578. struct iw_point *erq, char *keybuf)
  579. {
  580. struct wireless_dev *wdev = dev->ieee80211_ptr;
  581. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  582. int idx, err;
  583. struct giwencode_cookie data = {
  584. .keybuf = keybuf,
  585. .buflen = erq->length,
  586. };
  587. if (!rdev->ops->get_key)
  588. return -EOPNOTSUPP;
  589. idx = erq->flags & IW_ENCODE_INDEX;
  590. if (idx == 0) {
  591. idx = wdev->wext.default_key;
  592. if (idx < 0)
  593. idx = 0;
  594. } else if (idx < 1 || idx > 4)
  595. return -EINVAL;
  596. else
  597. idx--;
  598. erq->flags = idx + 1;
  599. err = rdev->ops->get_key(&rdev->wiphy, dev, idx, NULL, &data,
  600. giwencode_get_key_cb);
  601. if (!err) {
  602. erq->length = data.buflen;
  603. erq->flags |= IW_ENCODE_ENABLED;
  604. return 0;
  605. }
  606. if (err == -ENOENT) {
  607. erq->flags |= IW_ENCODE_DISABLED;
  608. erq->length = 0;
  609. return 0;
  610. }
  611. return err;
  612. }
  613. EXPORT_SYMBOL_GPL(cfg80211_wext_giwencode);
  614. int cfg80211_wext_siwtxpower(struct net_device *dev,
  615. struct iw_request_info *info,
  616. union iwreq_data *data, char *extra)
  617. {
  618. struct wireless_dev *wdev = dev->ieee80211_ptr;
  619. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  620. enum tx_power_setting type;
  621. int dbm = 0;
  622. if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
  623. return -EINVAL;
  624. if (data->txpower.flags & IW_TXPOW_RANGE)
  625. return -EINVAL;
  626. if (!rdev->ops->set_tx_power)
  627. return -EOPNOTSUPP;
  628. /* only change when not disabling */
  629. if (!data->txpower.disabled) {
  630. rfkill_set_sw_state(rdev->rfkill, false);
  631. if (data->txpower.fixed) {
  632. /*
  633. * wext doesn't support negative values, see
  634. * below where it's for automatic
  635. */
  636. if (data->txpower.value < 0)
  637. return -EINVAL;
  638. dbm = data->txpower.value;
  639. type = TX_POWER_FIXED;
  640. /* TODO: do regulatory check! */
  641. } else {
  642. /*
  643. * Automatic power level setting, max being the value
  644. * passed in from userland.
  645. */
  646. if (data->txpower.value < 0) {
  647. type = TX_POWER_AUTOMATIC;
  648. } else {
  649. dbm = data->txpower.value;
  650. type = TX_POWER_LIMITED;
  651. }
  652. }
  653. } else {
  654. rfkill_set_sw_state(rdev->rfkill, true);
  655. schedule_work(&rdev->rfkill_sync);
  656. return 0;
  657. }
  658. return rdev->ops->set_tx_power(wdev->wiphy, type, dbm);;
  659. }
  660. EXPORT_SYMBOL_GPL(cfg80211_wext_siwtxpower);
  661. int cfg80211_wext_giwtxpower(struct net_device *dev,
  662. struct iw_request_info *info,
  663. union iwreq_data *data, char *extra)
  664. {
  665. struct wireless_dev *wdev = dev->ieee80211_ptr;
  666. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  667. int err, val;
  668. if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
  669. return -EINVAL;
  670. if (data->txpower.flags & IW_TXPOW_RANGE)
  671. return -EINVAL;
  672. if (!rdev->ops->get_tx_power)
  673. return -EOPNOTSUPP;
  674. err = rdev->ops->get_tx_power(wdev->wiphy, &val);
  675. if (err)
  676. return err;
  677. /* well... oh well */
  678. data->txpower.fixed = 1;
  679. data->txpower.disabled = rfkill_blocked(rdev->rfkill);
  680. data->txpower.value = val;
  681. data->txpower.flags = IW_TXPOW_DBM;
  682. return 0;
  683. }
  684. EXPORT_SYMBOL_GPL(cfg80211_wext_giwtxpower);
  685. static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
  686. s32 auth_alg)
  687. {
  688. int nr_alg = 0;
  689. if (!auth_alg)
  690. return -EINVAL;
  691. if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
  692. IW_AUTH_ALG_SHARED_KEY |
  693. IW_AUTH_ALG_LEAP))
  694. return -EINVAL;
  695. if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
  696. nr_alg++;
  697. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  698. }
  699. if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
  700. nr_alg++;
  701. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  702. }
  703. if (auth_alg & IW_AUTH_ALG_LEAP) {
  704. nr_alg++;
  705. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
  706. }
  707. if (nr_alg > 1)
  708. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  709. return 0;
  710. }
  711. static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
  712. {
  713. wdev->wext.connect.crypto.wpa_versions = 0;
  714. if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
  715. IW_AUTH_WPA_VERSION_WPA2))
  716. return -EINVAL;
  717. if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
  718. wdev->wext.connect.crypto.wpa_versions |=
  719. NL80211_WPA_VERSION_1;
  720. if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
  721. wdev->wext.connect.crypto.wpa_versions |=
  722. NL80211_WPA_VERSION_2;
  723. return 0;
  724. }
  725. int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
  726. {
  727. wdev->wext.connect.crypto.cipher_group = 0;
  728. if (cipher & IW_AUTH_CIPHER_WEP40)
  729. wdev->wext.connect.crypto.cipher_group =
  730. WLAN_CIPHER_SUITE_WEP40;
  731. else if (cipher & IW_AUTH_CIPHER_WEP104)
  732. wdev->wext.connect.crypto.cipher_group =
  733. WLAN_CIPHER_SUITE_WEP104;
  734. else if (cipher & IW_AUTH_CIPHER_TKIP)
  735. wdev->wext.connect.crypto.cipher_group =
  736. WLAN_CIPHER_SUITE_TKIP;
  737. else if (cipher & IW_AUTH_CIPHER_CCMP)
  738. wdev->wext.connect.crypto.cipher_group =
  739. WLAN_CIPHER_SUITE_CCMP;
  740. else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
  741. wdev->wext.connect.crypto.cipher_group =
  742. WLAN_CIPHER_SUITE_AES_CMAC;
  743. else
  744. return -EINVAL;
  745. return 0;
  746. }
  747. int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
  748. {
  749. int nr_ciphers = 0;
  750. u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
  751. if (cipher & IW_AUTH_CIPHER_WEP40) {
  752. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
  753. nr_ciphers++;
  754. }
  755. if (cipher & IW_AUTH_CIPHER_WEP104) {
  756. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
  757. nr_ciphers++;
  758. }
  759. if (cipher & IW_AUTH_CIPHER_TKIP) {
  760. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
  761. nr_ciphers++;
  762. }
  763. if (cipher & IW_AUTH_CIPHER_CCMP) {
  764. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
  765. nr_ciphers++;
  766. }
  767. if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
  768. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
  769. nr_ciphers++;
  770. }
  771. BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
  772. wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
  773. return 0;
  774. }
  775. int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
  776. {
  777. int nr_akm_suites = 0;
  778. if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
  779. IW_AUTH_KEY_MGMT_PSK))
  780. return -EINVAL;
  781. if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
  782. wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
  783. WLAN_AKM_SUITE_8021X;
  784. nr_akm_suites++;
  785. }
  786. if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
  787. wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
  788. WLAN_AKM_SUITE_PSK;
  789. nr_akm_suites++;
  790. }
  791. wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
  792. return 0;
  793. }
  794. int cfg80211_wext_siwauth(struct net_device *dev,
  795. struct iw_request_info *info,
  796. struct iw_param *data, char *extra)
  797. {
  798. struct wireless_dev *wdev = dev->ieee80211_ptr;
  799. if (wdev->iftype != NL80211_IFTYPE_STATION)
  800. return -EOPNOTSUPP;
  801. switch (data->flags & IW_AUTH_INDEX) {
  802. case IW_AUTH_PRIVACY_INVOKED:
  803. wdev->wext.connect.privacy = data->value;
  804. return 0;
  805. case IW_AUTH_WPA_VERSION:
  806. return cfg80211_set_wpa_version(wdev, data->value);
  807. case IW_AUTH_CIPHER_GROUP:
  808. return cfg80211_set_cipher_group(wdev, data->value);
  809. case IW_AUTH_KEY_MGMT:
  810. return cfg80211_set_key_mgt(wdev, data->value);
  811. case IW_AUTH_CIPHER_PAIRWISE:
  812. return cfg80211_set_cipher_pairwise(wdev, data->value);
  813. case IW_AUTH_80211_AUTH_ALG:
  814. return cfg80211_set_auth_alg(wdev, data->value);
  815. case IW_AUTH_WPA_ENABLED:
  816. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  817. case IW_AUTH_DROP_UNENCRYPTED:
  818. case IW_AUTH_MFP:
  819. return 0;
  820. default:
  821. return -EOPNOTSUPP;
  822. }
  823. }
  824. EXPORT_SYMBOL_GPL(cfg80211_wext_siwauth);
  825. int cfg80211_wext_giwauth(struct net_device *dev,
  826. struct iw_request_info *info,
  827. struct iw_param *data, char *extra)
  828. {
  829. /* XXX: what do we need? */
  830. return -EOPNOTSUPP;
  831. }
  832. EXPORT_SYMBOL_GPL(cfg80211_wext_giwauth);
  833. int cfg80211_wext_siwpower(struct net_device *dev,
  834. struct iw_request_info *info,
  835. struct iw_param *wrq, char *extra)
  836. {
  837. struct wireless_dev *wdev = dev->ieee80211_ptr;
  838. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  839. bool ps = wdev->wext.ps;
  840. int timeout = wdev->wext.ps_timeout;
  841. int err;
  842. if (wdev->iftype != NL80211_IFTYPE_STATION)
  843. return -EINVAL;
  844. if (!rdev->ops->set_power_mgmt)
  845. return -EOPNOTSUPP;
  846. if (wrq->disabled) {
  847. ps = false;
  848. } else {
  849. switch (wrq->flags & IW_POWER_MODE) {
  850. case IW_POWER_ON: /* If not specified */
  851. case IW_POWER_MODE: /* If set all mask */
  852. case IW_POWER_ALL_R: /* If explicitely state all */
  853. ps = true;
  854. break;
  855. default: /* Otherwise we ignore */
  856. return -EINVAL;
  857. }
  858. if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
  859. return -EINVAL;
  860. if (wrq->flags & IW_POWER_TIMEOUT)
  861. timeout = wrq->value / 1000;
  862. }
  863. err = rdev->ops->set_power_mgmt(wdev->wiphy, dev, ps, timeout);
  864. if (err)
  865. return err;
  866. wdev->wext.ps = ps;
  867. wdev->wext.ps_timeout = timeout;
  868. return 0;
  869. }
  870. EXPORT_SYMBOL_GPL(cfg80211_wext_siwpower);
  871. int cfg80211_wext_giwpower(struct net_device *dev,
  872. struct iw_request_info *info,
  873. struct iw_param *wrq, char *extra)
  874. {
  875. struct wireless_dev *wdev = dev->ieee80211_ptr;
  876. wrq->disabled = !wdev->wext.ps;
  877. return 0;
  878. }
  879. EXPORT_SYMBOL_GPL(cfg80211_wext_giwpower);
  880. int cfg80211_wds_wext_siwap(struct net_device *dev,
  881. struct iw_request_info *info,
  882. struct sockaddr *addr, char *extra)
  883. {
  884. struct wireless_dev *wdev = dev->ieee80211_ptr;
  885. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  886. int err;
  887. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
  888. return -EINVAL;
  889. if (addr->sa_family != ARPHRD_ETHER)
  890. return -EINVAL;
  891. if (netif_running(dev))
  892. return -EBUSY;
  893. if (!rdev->ops->set_wds_peer)
  894. return -EOPNOTSUPP;
  895. err = rdev->ops->set_wds_peer(wdev->wiphy, dev, (u8 *) &addr->sa_data);
  896. if (err)
  897. return err;
  898. memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
  899. return 0;
  900. }
  901. EXPORT_SYMBOL_GPL(cfg80211_wds_wext_siwap);
  902. int cfg80211_wds_wext_giwap(struct net_device *dev,
  903. struct iw_request_info *info,
  904. struct sockaddr *addr, char *extra)
  905. {
  906. struct wireless_dev *wdev = dev->ieee80211_ptr;
  907. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
  908. return -EINVAL;
  909. addr->sa_family = ARPHRD_ETHER;
  910. memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
  911. return 0;
  912. }
  913. EXPORT_SYMBOL_GPL(cfg80211_wds_wext_giwap);
  914. int cfg80211_wext_siwrate(struct net_device *dev,
  915. struct iw_request_info *info,
  916. struct iw_param *rate, char *extra)
  917. {
  918. struct wireless_dev *wdev = dev->ieee80211_ptr;
  919. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  920. struct cfg80211_bitrate_mask mask;
  921. if (!rdev->ops->set_bitrate_mask)
  922. return -EOPNOTSUPP;
  923. mask.fixed = 0;
  924. mask.maxrate = 0;
  925. if (rate->value < 0) {
  926. /* nothing */
  927. } else if (rate->fixed) {
  928. mask.fixed = rate->value / 1000; /* kbps */
  929. } else {
  930. mask.maxrate = rate->value / 1000; /* kbps */
  931. }
  932. return rdev->ops->set_bitrate_mask(wdev->wiphy, dev, NULL, &mask);
  933. }
  934. EXPORT_SYMBOL_GPL(cfg80211_wext_siwrate);
  935. int cfg80211_wext_giwrate(struct net_device *dev,
  936. struct iw_request_info *info,
  937. struct iw_param *rate, char *extra)
  938. {
  939. struct wireless_dev *wdev = dev->ieee80211_ptr;
  940. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  941. /* we are under RTNL - globally locked - so can use a static struct */
  942. static struct station_info sinfo;
  943. u8 *addr;
  944. int err;
  945. if (wdev->iftype != NL80211_IFTYPE_STATION)
  946. return -EOPNOTSUPP;
  947. if (!rdev->ops->get_station)
  948. return -EOPNOTSUPP;
  949. addr = wdev->wext.connect.bssid;
  950. if (!addr)
  951. return -EOPNOTSUPP;
  952. err = rdev->ops->get_station(&rdev->wiphy, dev, addr, &sinfo);
  953. if (err)
  954. return err;
  955. if (!(sinfo.filled & STATION_INFO_TX_BITRATE))
  956. return -EOPNOTSUPP;
  957. rate->value = 0;
  958. if (!(sinfo.txrate.flags & RATE_INFO_FLAGS_MCS))
  959. rate->value = 100000 * sinfo.txrate.legacy;
  960. return 0;
  961. }
  962. EXPORT_SYMBOL_GPL(cfg80211_wext_giwrate);
  963. /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
  964. struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
  965. {
  966. struct wireless_dev *wdev = dev->ieee80211_ptr;
  967. struct cfg80211_registered_device *rdev = wiphy_to_dev(wdev->wiphy);
  968. /* we are under RTNL - globally locked - so can use static structs */
  969. static struct iw_statistics wstats;
  970. static struct station_info sinfo;
  971. u8 *addr;
  972. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
  973. return NULL;
  974. if (!rdev->ops->get_station)
  975. return NULL;
  976. addr = wdev->wext.connect.bssid;
  977. if (!addr)
  978. return NULL;
  979. if (rdev->ops->get_station(&rdev->wiphy, dev, addr, &sinfo))
  980. return NULL;
  981. memset(&wstats, 0, sizeof(wstats));
  982. switch (rdev->wiphy.signal_type) {
  983. case CFG80211_SIGNAL_TYPE_MBM:
  984. if (sinfo.filled & STATION_INFO_SIGNAL) {
  985. int sig = sinfo.signal;
  986. wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
  987. wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
  988. wstats.qual.updated |= IW_QUAL_DBM;
  989. wstats.qual.level = sig;
  990. if (sig < -110)
  991. sig = -110;
  992. else if (sig > -40)
  993. sig = -40;
  994. wstats.qual.qual = sig + 110;
  995. break;
  996. }
  997. case CFG80211_SIGNAL_TYPE_UNSPEC:
  998. if (sinfo.filled & STATION_INFO_SIGNAL) {
  999. wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
  1000. wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
  1001. wstats.qual.level = sinfo.signal;
  1002. wstats.qual.qual = sinfo.signal;
  1003. break;
  1004. }
  1005. default:
  1006. wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
  1007. wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
  1008. }
  1009. wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
  1010. return &wstats;
  1011. }
  1012. EXPORT_SYMBOL_GPL(cfg80211_wireless_stats);