wext-compat.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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/export.h>
  11. #include <linux/wireless.h>
  12. #include <linux/nl80211.h>
  13. #include <linux/if_arp.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/slab.h>
  16. #include <net/iw_handler.h>
  17. #include <net/cfg80211.h>
  18. #include <net/cfg80211-wext.h>
  19. #include "wext-compat.h"
  20. #include "core.h"
  21. #include "rdev-ops.h"
  22. int cfg80211_wext_giwname(struct net_device *dev,
  23. struct iw_request_info *info,
  24. char *name, char *extra)
  25. {
  26. struct wireless_dev *wdev = dev->ieee80211_ptr;
  27. struct ieee80211_supported_band *sband;
  28. bool is_ht = false, is_a = false, is_b = false, is_g = false;
  29. if (!wdev)
  30. return -EOPNOTSUPP;
  31. sband = wdev->wiphy->bands[IEEE80211_BAND_5GHZ];
  32. if (sband) {
  33. is_a = true;
  34. is_ht |= sband->ht_cap.ht_supported;
  35. }
  36. sband = wdev->wiphy->bands[IEEE80211_BAND_2GHZ];
  37. if (sband) {
  38. int i;
  39. /* Check for mandatory rates */
  40. for (i = 0; i < sband->n_bitrates; i++) {
  41. if (sband->bitrates[i].bitrate == 10)
  42. is_b = true;
  43. if (sband->bitrates[i].bitrate == 60)
  44. is_g = true;
  45. }
  46. is_ht |= sband->ht_cap.ht_supported;
  47. }
  48. strcpy(name, "IEEE 802.11");
  49. if (is_a)
  50. strcat(name, "a");
  51. if (is_b)
  52. strcat(name, "b");
  53. if (is_g)
  54. strcat(name, "g");
  55. if (is_ht)
  56. strcat(name, "n");
  57. return 0;
  58. }
  59. EXPORT_WEXT_HANDLER(cfg80211_wext_giwname);
  60. int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
  61. u32 *mode, char *extra)
  62. {
  63. struct wireless_dev *wdev = dev->ieee80211_ptr;
  64. struct cfg80211_registered_device *rdev;
  65. struct vif_params vifparams;
  66. enum nl80211_iftype type;
  67. rdev = wiphy_to_rdev(wdev->wiphy);
  68. switch (*mode) {
  69. case IW_MODE_INFRA:
  70. type = NL80211_IFTYPE_STATION;
  71. break;
  72. case IW_MODE_ADHOC:
  73. type = NL80211_IFTYPE_ADHOC;
  74. break;
  75. case IW_MODE_REPEAT:
  76. type = NL80211_IFTYPE_WDS;
  77. break;
  78. case IW_MODE_MONITOR:
  79. type = NL80211_IFTYPE_MONITOR;
  80. break;
  81. default:
  82. return -EINVAL;
  83. }
  84. if (type == wdev->iftype)
  85. return 0;
  86. memset(&vifparams, 0, sizeof(vifparams));
  87. return cfg80211_change_iface(rdev, dev, type, NULL, &vifparams);
  88. }
  89. EXPORT_WEXT_HANDLER(cfg80211_wext_siwmode);
  90. int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
  91. u32 *mode, char *extra)
  92. {
  93. struct wireless_dev *wdev = dev->ieee80211_ptr;
  94. if (!wdev)
  95. return -EOPNOTSUPP;
  96. switch (wdev->iftype) {
  97. case NL80211_IFTYPE_AP:
  98. *mode = IW_MODE_MASTER;
  99. break;
  100. case NL80211_IFTYPE_STATION:
  101. *mode = IW_MODE_INFRA;
  102. break;
  103. case NL80211_IFTYPE_ADHOC:
  104. *mode = IW_MODE_ADHOC;
  105. break;
  106. case NL80211_IFTYPE_MONITOR:
  107. *mode = IW_MODE_MONITOR;
  108. break;
  109. case NL80211_IFTYPE_WDS:
  110. *mode = IW_MODE_REPEAT;
  111. break;
  112. case NL80211_IFTYPE_AP_VLAN:
  113. *mode = IW_MODE_SECOND; /* FIXME */
  114. break;
  115. default:
  116. *mode = IW_MODE_AUTO;
  117. break;
  118. }
  119. return 0;
  120. }
  121. EXPORT_WEXT_HANDLER(cfg80211_wext_giwmode);
  122. int cfg80211_wext_giwrange(struct net_device *dev,
  123. struct iw_request_info *info,
  124. struct iw_point *data, char *extra)
  125. {
  126. struct wireless_dev *wdev = dev->ieee80211_ptr;
  127. struct iw_range *range = (struct iw_range *) extra;
  128. enum ieee80211_band band;
  129. int i, c = 0;
  130. if (!wdev)
  131. return -EOPNOTSUPP;
  132. data->length = sizeof(struct iw_range);
  133. memset(range, 0, sizeof(struct iw_range));
  134. range->we_version_compiled = WIRELESS_EXT;
  135. range->we_version_source = 21;
  136. range->retry_capa = IW_RETRY_LIMIT;
  137. range->retry_flags = IW_RETRY_LIMIT;
  138. range->min_retry = 0;
  139. range->max_retry = 255;
  140. range->min_rts = 0;
  141. range->max_rts = 2347;
  142. range->min_frag = 256;
  143. range->max_frag = 2346;
  144. range->max_encoding_tokens = 4;
  145. range->max_qual.updated = IW_QUAL_NOISE_INVALID;
  146. switch (wdev->wiphy->signal_type) {
  147. case CFG80211_SIGNAL_TYPE_NONE:
  148. break;
  149. case CFG80211_SIGNAL_TYPE_MBM:
  150. range->max_qual.level = (u8)-110;
  151. range->max_qual.qual = 70;
  152. range->avg_qual.qual = 35;
  153. range->max_qual.updated |= IW_QUAL_DBM;
  154. range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
  155. range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
  156. break;
  157. case CFG80211_SIGNAL_TYPE_UNSPEC:
  158. range->max_qual.level = 100;
  159. range->max_qual.qual = 100;
  160. range->avg_qual.qual = 50;
  161. range->max_qual.updated |= IW_QUAL_QUAL_UPDATED;
  162. range->max_qual.updated |= IW_QUAL_LEVEL_UPDATED;
  163. break;
  164. }
  165. range->avg_qual.level = range->max_qual.level / 2;
  166. range->avg_qual.noise = range->max_qual.noise / 2;
  167. range->avg_qual.updated = range->max_qual.updated;
  168. for (i = 0; i < wdev->wiphy->n_cipher_suites; i++) {
  169. switch (wdev->wiphy->cipher_suites[i]) {
  170. case WLAN_CIPHER_SUITE_TKIP:
  171. range->enc_capa |= (IW_ENC_CAPA_CIPHER_TKIP |
  172. IW_ENC_CAPA_WPA);
  173. break;
  174. case WLAN_CIPHER_SUITE_CCMP:
  175. range->enc_capa |= (IW_ENC_CAPA_CIPHER_CCMP |
  176. IW_ENC_CAPA_WPA2);
  177. break;
  178. case WLAN_CIPHER_SUITE_WEP40:
  179. range->encoding_size[range->num_encoding_sizes++] =
  180. WLAN_KEY_LEN_WEP40;
  181. break;
  182. case WLAN_CIPHER_SUITE_WEP104:
  183. range->encoding_size[range->num_encoding_sizes++] =
  184. WLAN_KEY_LEN_WEP104;
  185. break;
  186. }
  187. }
  188. for (band = 0; band < IEEE80211_NUM_BANDS; band ++) {
  189. struct ieee80211_supported_band *sband;
  190. sband = wdev->wiphy->bands[band];
  191. if (!sband)
  192. continue;
  193. for (i = 0; i < sband->n_channels && c < IW_MAX_FREQUENCIES; i++) {
  194. struct ieee80211_channel *chan = &sband->channels[i];
  195. if (!(chan->flags & IEEE80211_CHAN_DISABLED)) {
  196. range->freq[c].i =
  197. ieee80211_frequency_to_channel(
  198. chan->center_freq);
  199. range->freq[c].m = chan->center_freq;
  200. range->freq[c].e = 6;
  201. c++;
  202. }
  203. }
  204. }
  205. range->num_channels = c;
  206. range->num_frequency = c;
  207. IW_EVENT_CAPA_SET_KERNEL(range->event_capa);
  208. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWAP);
  209. IW_EVENT_CAPA_SET(range->event_capa, SIOCGIWSCAN);
  210. if (wdev->wiphy->max_scan_ssids > 0)
  211. range->scan_capa |= IW_SCAN_CAPA_ESSID;
  212. return 0;
  213. }
  214. EXPORT_WEXT_HANDLER(cfg80211_wext_giwrange);
  215. /**
  216. * cfg80211_wext_freq - get wext frequency for non-"auto"
  217. * @dev: the net device
  218. * @freq: the wext freq encoding
  219. *
  220. * Returns a frequency, or a negative error code, or 0 for auto.
  221. */
  222. int cfg80211_wext_freq(struct iw_freq *freq)
  223. {
  224. /*
  225. * Parse frequency - return 0 for auto and
  226. * -EINVAL for impossible things.
  227. */
  228. if (freq->e == 0) {
  229. enum ieee80211_band band = IEEE80211_BAND_2GHZ;
  230. if (freq->m < 0)
  231. return 0;
  232. if (freq->m > 14)
  233. band = IEEE80211_BAND_5GHZ;
  234. return ieee80211_channel_to_frequency(freq->m, band);
  235. } else {
  236. int i, div = 1000000;
  237. for (i = 0; i < freq->e; i++)
  238. div /= 10;
  239. if (div <= 0)
  240. return -EINVAL;
  241. return freq->m / div;
  242. }
  243. }
  244. int cfg80211_wext_siwrts(struct net_device *dev,
  245. struct iw_request_info *info,
  246. struct iw_param *rts, char *extra)
  247. {
  248. struct wireless_dev *wdev = dev->ieee80211_ptr;
  249. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  250. u32 orts = wdev->wiphy->rts_threshold;
  251. int err;
  252. if (rts->disabled || !rts->fixed)
  253. wdev->wiphy->rts_threshold = (u32) -1;
  254. else if (rts->value < 0)
  255. return -EINVAL;
  256. else
  257. wdev->wiphy->rts_threshold = rts->value;
  258. err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_RTS_THRESHOLD);
  259. if (err)
  260. wdev->wiphy->rts_threshold = orts;
  261. return err;
  262. }
  263. EXPORT_WEXT_HANDLER(cfg80211_wext_siwrts);
  264. int cfg80211_wext_giwrts(struct net_device *dev,
  265. struct iw_request_info *info,
  266. struct iw_param *rts, char *extra)
  267. {
  268. struct wireless_dev *wdev = dev->ieee80211_ptr;
  269. rts->value = wdev->wiphy->rts_threshold;
  270. rts->disabled = rts->value == (u32) -1;
  271. rts->fixed = 1;
  272. return 0;
  273. }
  274. EXPORT_WEXT_HANDLER(cfg80211_wext_giwrts);
  275. int cfg80211_wext_siwfrag(struct net_device *dev,
  276. struct iw_request_info *info,
  277. struct iw_param *frag, char *extra)
  278. {
  279. struct wireless_dev *wdev = dev->ieee80211_ptr;
  280. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  281. u32 ofrag = wdev->wiphy->frag_threshold;
  282. int err;
  283. if (frag->disabled || !frag->fixed)
  284. wdev->wiphy->frag_threshold = (u32) -1;
  285. else if (frag->value < 256)
  286. return -EINVAL;
  287. else {
  288. /* Fragment length must be even, so strip LSB. */
  289. wdev->wiphy->frag_threshold = frag->value & ~0x1;
  290. }
  291. err = rdev_set_wiphy_params(rdev, WIPHY_PARAM_FRAG_THRESHOLD);
  292. if (err)
  293. wdev->wiphy->frag_threshold = ofrag;
  294. return err;
  295. }
  296. EXPORT_WEXT_HANDLER(cfg80211_wext_siwfrag);
  297. int cfg80211_wext_giwfrag(struct net_device *dev,
  298. struct iw_request_info *info,
  299. struct iw_param *frag, char *extra)
  300. {
  301. struct wireless_dev *wdev = dev->ieee80211_ptr;
  302. frag->value = wdev->wiphy->frag_threshold;
  303. frag->disabled = frag->value == (u32) -1;
  304. frag->fixed = 1;
  305. return 0;
  306. }
  307. EXPORT_WEXT_HANDLER(cfg80211_wext_giwfrag);
  308. static int cfg80211_wext_siwretry(struct net_device *dev,
  309. struct iw_request_info *info,
  310. struct iw_param *retry, char *extra)
  311. {
  312. struct wireless_dev *wdev = dev->ieee80211_ptr;
  313. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  314. u32 changed = 0;
  315. u8 olong = wdev->wiphy->retry_long;
  316. u8 oshort = wdev->wiphy->retry_short;
  317. int err;
  318. if (retry->disabled || retry->value < 1 || retry->value > 255 ||
  319. (retry->flags & IW_RETRY_TYPE) != IW_RETRY_LIMIT)
  320. return -EINVAL;
  321. if (retry->flags & IW_RETRY_LONG) {
  322. wdev->wiphy->retry_long = retry->value;
  323. changed |= WIPHY_PARAM_RETRY_LONG;
  324. } else if (retry->flags & IW_RETRY_SHORT) {
  325. wdev->wiphy->retry_short = retry->value;
  326. changed |= WIPHY_PARAM_RETRY_SHORT;
  327. } else {
  328. wdev->wiphy->retry_short = retry->value;
  329. wdev->wiphy->retry_long = retry->value;
  330. changed |= WIPHY_PARAM_RETRY_LONG;
  331. changed |= WIPHY_PARAM_RETRY_SHORT;
  332. }
  333. if (!changed)
  334. return 0;
  335. err = rdev_set_wiphy_params(rdev, changed);
  336. if (err) {
  337. wdev->wiphy->retry_short = oshort;
  338. wdev->wiphy->retry_long = olong;
  339. }
  340. return err;
  341. }
  342. int cfg80211_wext_giwretry(struct net_device *dev,
  343. struct iw_request_info *info,
  344. struct iw_param *retry, char *extra)
  345. {
  346. struct wireless_dev *wdev = dev->ieee80211_ptr;
  347. retry->disabled = 0;
  348. if (retry->flags == 0 || (retry->flags & IW_RETRY_SHORT)) {
  349. /*
  350. * First return short value, iwconfig will ask long value
  351. * later if needed
  352. */
  353. retry->flags |= IW_RETRY_LIMIT | IW_RETRY_SHORT;
  354. retry->value = wdev->wiphy->retry_short;
  355. if (wdev->wiphy->retry_long == wdev->wiphy->retry_short)
  356. retry->flags |= IW_RETRY_LONG;
  357. return 0;
  358. }
  359. if (retry->flags & IW_RETRY_LONG) {
  360. retry->flags = IW_RETRY_LIMIT | IW_RETRY_LONG;
  361. retry->value = wdev->wiphy->retry_long;
  362. }
  363. return 0;
  364. }
  365. EXPORT_WEXT_HANDLER(cfg80211_wext_giwretry);
  366. static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
  367. struct net_device *dev, bool pairwise,
  368. const u8 *addr, bool remove, bool tx_key,
  369. int idx, struct key_params *params)
  370. {
  371. struct wireless_dev *wdev = dev->ieee80211_ptr;
  372. int err, i;
  373. bool rejoin = false;
  374. if (pairwise && !addr)
  375. return -EINVAL;
  376. if (!wdev->wext.keys) {
  377. wdev->wext.keys = kzalloc(sizeof(*wdev->wext.keys),
  378. GFP_KERNEL);
  379. if (!wdev->wext.keys)
  380. return -ENOMEM;
  381. for (i = 0; i < 6; i++)
  382. wdev->wext.keys->params[i].key =
  383. wdev->wext.keys->data[i];
  384. }
  385. if (wdev->iftype != NL80211_IFTYPE_ADHOC &&
  386. wdev->iftype != NL80211_IFTYPE_STATION)
  387. return -EOPNOTSUPP;
  388. if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  389. if (!wdev->current_bss)
  390. return -ENOLINK;
  391. if (!rdev->ops->set_default_mgmt_key)
  392. return -EOPNOTSUPP;
  393. if (idx < 4 || idx > 5)
  394. return -EINVAL;
  395. } else if (idx < 0 || idx > 3)
  396. return -EINVAL;
  397. if (remove) {
  398. err = 0;
  399. if (wdev->current_bss) {
  400. /*
  401. * If removing the current TX key, we will need to
  402. * join a new IBSS without the privacy bit clear.
  403. */
  404. if (idx == wdev->wext.default_key &&
  405. wdev->iftype == NL80211_IFTYPE_ADHOC) {
  406. __cfg80211_leave_ibss(rdev, wdev->netdev, true);
  407. rejoin = true;
  408. }
  409. if (!pairwise && addr &&
  410. !(rdev->wiphy.flags & WIPHY_FLAG_IBSS_RSN))
  411. err = -ENOENT;
  412. else
  413. err = rdev_del_key(rdev, dev, idx, pairwise,
  414. addr);
  415. }
  416. wdev->wext.connect.privacy = false;
  417. /*
  418. * Applications using wireless extensions expect to be
  419. * able to delete keys that don't exist, so allow that.
  420. */
  421. if (err == -ENOENT)
  422. err = 0;
  423. if (!err) {
  424. if (!addr) {
  425. memset(wdev->wext.keys->data[idx], 0,
  426. sizeof(wdev->wext.keys->data[idx]));
  427. wdev->wext.keys->params[idx].key_len = 0;
  428. wdev->wext.keys->params[idx].cipher = 0;
  429. }
  430. if (idx == wdev->wext.default_key)
  431. wdev->wext.default_key = -1;
  432. else if (idx == wdev->wext.default_mgmt_key)
  433. wdev->wext.default_mgmt_key = -1;
  434. }
  435. if (!err && rejoin)
  436. err = cfg80211_ibss_wext_join(rdev, wdev);
  437. return err;
  438. }
  439. if (addr)
  440. tx_key = false;
  441. if (cfg80211_validate_key_settings(rdev, params, idx, pairwise, addr))
  442. return -EINVAL;
  443. err = 0;
  444. if (wdev->current_bss)
  445. err = rdev_add_key(rdev, dev, idx, pairwise, addr, params);
  446. if (err)
  447. return err;
  448. if (!addr) {
  449. wdev->wext.keys->params[idx] = *params;
  450. memcpy(wdev->wext.keys->data[idx],
  451. params->key, params->key_len);
  452. wdev->wext.keys->params[idx].key =
  453. wdev->wext.keys->data[idx];
  454. }
  455. if ((params->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  456. params->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  457. (tx_key || (!addr && wdev->wext.default_key == -1))) {
  458. if (wdev->current_bss) {
  459. /*
  460. * If we are getting a new TX key from not having
  461. * had one before we need to join a new IBSS with
  462. * the privacy bit set.
  463. */
  464. if (wdev->iftype == NL80211_IFTYPE_ADHOC &&
  465. wdev->wext.default_key == -1) {
  466. __cfg80211_leave_ibss(rdev, wdev->netdev, true);
  467. rejoin = true;
  468. }
  469. err = rdev_set_default_key(rdev, dev, idx, true, true);
  470. }
  471. if (!err) {
  472. wdev->wext.default_key = idx;
  473. if (rejoin)
  474. err = cfg80211_ibss_wext_join(rdev, wdev);
  475. }
  476. return err;
  477. }
  478. if (params->cipher == WLAN_CIPHER_SUITE_AES_CMAC &&
  479. (tx_key || (!addr && wdev->wext.default_mgmt_key == -1))) {
  480. if (wdev->current_bss)
  481. err = rdev_set_default_mgmt_key(rdev, dev, idx);
  482. if (!err)
  483. wdev->wext.default_mgmt_key = idx;
  484. return err;
  485. }
  486. return 0;
  487. }
  488. static int cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
  489. struct net_device *dev, bool pairwise,
  490. const u8 *addr, bool remove, bool tx_key,
  491. int idx, struct key_params *params)
  492. {
  493. int err;
  494. wdev_lock(dev->ieee80211_ptr);
  495. err = __cfg80211_set_encryption(rdev, dev, pairwise, addr,
  496. remove, tx_key, idx, params);
  497. wdev_unlock(dev->ieee80211_ptr);
  498. return err;
  499. }
  500. static int cfg80211_wext_siwencode(struct net_device *dev,
  501. struct iw_request_info *info,
  502. struct iw_point *erq, char *keybuf)
  503. {
  504. struct wireless_dev *wdev = dev->ieee80211_ptr;
  505. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  506. int idx, err;
  507. bool remove = false;
  508. struct key_params params;
  509. if (wdev->iftype != NL80211_IFTYPE_STATION &&
  510. wdev->iftype != NL80211_IFTYPE_ADHOC)
  511. return -EOPNOTSUPP;
  512. /* no use -- only MFP (set_default_mgmt_key) is optional */
  513. if (!rdev->ops->del_key ||
  514. !rdev->ops->add_key ||
  515. !rdev->ops->set_default_key)
  516. return -EOPNOTSUPP;
  517. idx = erq->flags & IW_ENCODE_INDEX;
  518. if (idx == 0) {
  519. idx = wdev->wext.default_key;
  520. if (idx < 0)
  521. idx = 0;
  522. } else if (idx < 1 || idx > 4)
  523. return -EINVAL;
  524. else
  525. idx--;
  526. if (erq->flags & IW_ENCODE_DISABLED)
  527. remove = true;
  528. else if (erq->length == 0) {
  529. /* No key data - just set the default TX key index */
  530. err = 0;
  531. wdev_lock(wdev);
  532. if (wdev->current_bss)
  533. err = rdev_set_default_key(rdev, dev, idx, true,
  534. true);
  535. if (!err)
  536. wdev->wext.default_key = idx;
  537. wdev_unlock(wdev);
  538. return err;
  539. }
  540. memset(&params, 0, sizeof(params));
  541. params.key = keybuf;
  542. params.key_len = erq->length;
  543. if (erq->length == 5)
  544. params.cipher = WLAN_CIPHER_SUITE_WEP40;
  545. else if (erq->length == 13)
  546. params.cipher = WLAN_CIPHER_SUITE_WEP104;
  547. else if (!remove)
  548. return -EINVAL;
  549. return cfg80211_set_encryption(rdev, dev, false, NULL, remove,
  550. wdev->wext.default_key == -1,
  551. idx, &params);
  552. }
  553. static int cfg80211_wext_siwencodeext(struct net_device *dev,
  554. struct iw_request_info *info,
  555. struct iw_point *erq, char *extra)
  556. {
  557. struct wireless_dev *wdev = dev->ieee80211_ptr;
  558. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  559. struct iw_encode_ext *ext = (struct iw_encode_ext *) extra;
  560. const u8 *addr;
  561. int idx;
  562. bool remove = false;
  563. struct key_params params;
  564. u32 cipher;
  565. if (wdev->iftype != NL80211_IFTYPE_STATION &&
  566. wdev->iftype != NL80211_IFTYPE_ADHOC)
  567. return -EOPNOTSUPP;
  568. /* no use -- only MFP (set_default_mgmt_key) is optional */
  569. if (!rdev->ops->del_key ||
  570. !rdev->ops->add_key ||
  571. !rdev->ops->set_default_key)
  572. return -EOPNOTSUPP;
  573. switch (ext->alg) {
  574. case IW_ENCODE_ALG_NONE:
  575. remove = true;
  576. cipher = 0;
  577. break;
  578. case IW_ENCODE_ALG_WEP:
  579. if (ext->key_len == 5)
  580. cipher = WLAN_CIPHER_SUITE_WEP40;
  581. else if (ext->key_len == 13)
  582. cipher = WLAN_CIPHER_SUITE_WEP104;
  583. else
  584. return -EINVAL;
  585. break;
  586. case IW_ENCODE_ALG_TKIP:
  587. cipher = WLAN_CIPHER_SUITE_TKIP;
  588. break;
  589. case IW_ENCODE_ALG_CCMP:
  590. cipher = WLAN_CIPHER_SUITE_CCMP;
  591. break;
  592. case IW_ENCODE_ALG_AES_CMAC:
  593. cipher = WLAN_CIPHER_SUITE_AES_CMAC;
  594. break;
  595. default:
  596. return -EOPNOTSUPP;
  597. }
  598. if (erq->flags & IW_ENCODE_DISABLED)
  599. remove = true;
  600. idx = erq->flags & IW_ENCODE_INDEX;
  601. if (cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
  602. if (idx < 4 || idx > 5) {
  603. idx = wdev->wext.default_mgmt_key;
  604. if (idx < 0)
  605. return -EINVAL;
  606. } else
  607. idx--;
  608. } else {
  609. if (idx < 1 || idx > 4) {
  610. idx = wdev->wext.default_key;
  611. if (idx < 0)
  612. return -EINVAL;
  613. } else
  614. idx--;
  615. }
  616. addr = ext->addr.sa_data;
  617. if (is_broadcast_ether_addr(addr))
  618. addr = NULL;
  619. memset(&params, 0, sizeof(params));
  620. params.key = ext->key;
  621. params.key_len = ext->key_len;
  622. params.cipher = cipher;
  623. if (ext->ext_flags & IW_ENCODE_EXT_RX_SEQ_VALID) {
  624. params.seq = ext->rx_seq;
  625. params.seq_len = 6;
  626. }
  627. return cfg80211_set_encryption(
  628. rdev, dev,
  629. !(ext->ext_flags & IW_ENCODE_EXT_GROUP_KEY),
  630. addr, remove,
  631. ext->ext_flags & IW_ENCODE_EXT_SET_TX_KEY,
  632. idx, &params);
  633. }
  634. static int cfg80211_wext_giwencode(struct net_device *dev,
  635. struct iw_request_info *info,
  636. struct iw_point *erq, char *keybuf)
  637. {
  638. struct wireless_dev *wdev = dev->ieee80211_ptr;
  639. int idx;
  640. if (wdev->iftype != NL80211_IFTYPE_STATION &&
  641. wdev->iftype != NL80211_IFTYPE_ADHOC)
  642. return -EOPNOTSUPP;
  643. idx = erq->flags & IW_ENCODE_INDEX;
  644. if (idx == 0) {
  645. idx = wdev->wext.default_key;
  646. if (idx < 0)
  647. idx = 0;
  648. } else if (idx < 1 || idx > 4)
  649. return -EINVAL;
  650. else
  651. idx--;
  652. erq->flags = idx + 1;
  653. if (!wdev->wext.keys || !wdev->wext.keys->params[idx].cipher) {
  654. erq->flags |= IW_ENCODE_DISABLED;
  655. erq->length = 0;
  656. return 0;
  657. }
  658. erq->length = min_t(size_t, erq->length,
  659. wdev->wext.keys->params[idx].key_len);
  660. memcpy(keybuf, wdev->wext.keys->params[idx].key, erq->length);
  661. erq->flags |= IW_ENCODE_ENABLED;
  662. return 0;
  663. }
  664. static int cfg80211_wext_siwfreq(struct net_device *dev,
  665. struct iw_request_info *info,
  666. struct iw_freq *wextfreq, char *extra)
  667. {
  668. struct wireless_dev *wdev = dev->ieee80211_ptr;
  669. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  670. struct cfg80211_chan_def chandef = {
  671. .width = NL80211_CHAN_WIDTH_20_NOHT,
  672. };
  673. int freq;
  674. switch (wdev->iftype) {
  675. case NL80211_IFTYPE_STATION:
  676. return cfg80211_mgd_wext_siwfreq(dev, info, wextfreq, extra);
  677. case NL80211_IFTYPE_ADHOC:
  678. return cfg80211_ibss_wext_siwfreq(dev, info, wextfreq, extra);
  679. case NL80211_IFTYPE_MONITOR:
  680. freq = cfg80211_wext_freq(wextfreq);
  681. if (freq < 0)
  682. return freq;
  683. if (freq == 0)
  684. return -EINVAL;
  685. chandef.center_freq1 = freq;
  686. chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
  687. if (!chandef.chan)
  688. return -EINVAL;
  689. return cfg80211_set_monitor_channel(rdev, &chandef);
  690. case NL80211_IFTYPE_MESH_POINT:
  691. freq = cfg80211_wext_freq(wextfreq);
  692. if (freq < 0)
  693. return freq;
  694. if (freq == 0)
  695. return -EINVAL;
  696. chandef.center_freq1 = freq;
  697. chandef.chan = ieee80211_get_channel(&rdev->wiphy, freq);
  698. if (!chandef.chan)
  699. return -EINVAL;
  700. return cfg80211_set_mesh_channel(rdev, wdev, &chandef);
  701. default:
  702. return -EOPNOTSUPP;
  703. }
  704. }
  705. static int cfg80211_wext_giwfreq(struct net_device *dev,
  706. struct iw_request_info *info,
  707. struct iw_freq *freq, char *extra)
  708. {
  709. struct wireless_dev *wdev = dev->ieee80211_ptr;
  710. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  711. struct cfg80211_chan_def chandef;
  712. int ret;
  713. switch (wdev->iftype) {
  714. case NL80211_IFTYPE_STATION:
  715. return cfg80211_mgd_wext_giwfreq(dev, info, freq, extra);
  716. case NL80211_IFTYPE_ADHOC:
  717. return cfg80211_ibss_wext_giwfreq(dev, info, freq, extra);
  718. case NL80211_IFTYPE_MONITOR:
  719. if (!rdev->ops->get_channel)
  720. return -EINVAL;
  721. ret = rdev_get_channel(rdev, wdev, &chandef);
  722. if (ret)
  723. return ret;
  724. freq->m = chandef.chan->center_freq;
  725. freq->e = 6;
  726. return 0;
  727. default:
  728. return -EINVAL;
  729. }
  730. }
  731. static int cfg80211_wext_siwtxpower(struct net_device *dev,
  732. struct iw_request_info *info,
  733. union iwreq_data *data, char *extra)
  734. {
  735. struct wireless_dev *wdev = dev->ieee80211_ptr;
  736. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  737. enum nl80211_tx_power_setting type;
  738. int dbm = 0;
  739. if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
  740. return -EINVAL;
  741. if (data->txpower.flags & IW_TXPOW_RANGE)
  742. return -EINVAL;
  743. if (!rdev->ops->set_tx_power)
  744. return -EOPNOTSUPP;
  745. /* only change when not disabling */
  746. if (!data->txpower.disabled) {
  747. rfkill_set_sw_state(rdev->rfkill, false);
  748. if (data->txpower.fixed) {
  749. /*
  750. * wext doesn't support negative values, see
  751. * below where it's for automatic
  752. */
  753. if (data->txpower.value < 0)
  754. return -EINVAL;
  755. dbm = data->txpower.value;
  756. type = NL80211_TX_POWER_FIXED;
  757. /* TODO: do regulatory check! */
  758. } else {
  759. /*
  760. * Automatic power level setting, max being the value
  761. * passed in from userland.
  762. */
  763. if (data->txpower.value < 0) {
  764. type = NL80211_TX_POWER_AUTOMATIC;
  765. } else {
  766. dbm = data->txpower.value;
  767. type = NL80211_TX_POWER_LIMITED;
  768. }
  769. }
  770. } else {
  771. rfkill_set_sw_state(rdev->rfkill, true);
  772. schedule_work(&rdev->rfkill_sync);
  773. return 0;
  774. }
  775. return rdev_set_tx_power(rdev, wdev, type, DBM_TO_MBM(dbm));
  776. }
  777. static int cfg80211_wext_giwtxpower(struct net_device *dev,
  778. struct iw_request_info *info,
  779. union iwreq_data *data, char *extra)
  780. {
  781. struct wireless_dev *wdev = dev->ieee80211_ptr;
  782. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  783. int err, val;
  784. if ((data->txpower.flags & IW_TXPOW_TYPE) != IW_TXPOW_DBM)
  785. return -EINVAL;
  786. if (data->txpower.flags & IW_TXPOW_RANGE)
  787. return -EINVAL;
  788. if (!rdev->ops->get_tx_power)
  789. return -EOPNOTSUPP;
  790. err = rdev_get_tx_power(rdev, wdev, &val);
  791. if (err)
  792. return err;
  793. /* well... oh well */
  794. data->txpower.fixed = 1;
  795. data->txpower.disabled = rfkill_blocked(rdev->rfkill);
  796. data->txpower.value = val;
  797. data->txpower.flags = IW_TXPOW_DBM;
  798. return 0;
  799. }
  800. static int cfg80211_set_auth_alg(struct wireless_dev *wdev,
  801. s32 auth_alg)
  802. {
  803. int nr_alg = 0;
  804. if (!auth_alg)
  805. return -EINVAL;
  806. if (auth_alg & ~(IW_AUTH_ALG_OPEN_SYSTEM |
  807. IW_AUTH_ALG_SHARED_KEY |
  808. IW_AUTH_ALG_LEAP))
  809. return -EINVAL;
  810. if (auth_alg & IW_AUTH_ALG_OPEN_SYSTEM) {
  811. nr_alg++;
  812. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM;
  813. }
  814. if (auth_alg & IW_AUTH_ALG_SHARED_KEY) {
  815. nr_alg++;
  816. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_SHARED_KEY;
  817. }
  818. if (auth_alg & IW_AUTH_ALG_LEAP) {
  819. nr_alg++;
  820. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_NETWORK_EAP;
  821. }
  822. if (nr_alg > 1)
  823. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  824. return 0;
  825. }
  826. static int cfg80211_set_wpa_version(struct wireless_dev *wdev, u32 wpa_versions)
  827. {
  828. if (wpa_versions & ~(IW_AUTH_WPA_VERSION_WPA |
  829. IW_AUTH_WPA_VERSION_WPA2|
  830. IW_AUTH_WPA_VERSION_DISABLED))
  831. return -EINVAL;
  832. if ((wpa_versions & IW_AUTH_WPA_VERSION_DISABLED) &&
  833. (wpa_versions & (IW_AUTH_WPA_VERSION_WPA|
  834. IW_AUTH_WPA_VERSION_WPA2)))
  835. return -EINVAL;
  836. if (wpa_versions & IW_AUTH_WPA_VERSION_DISABLED)
  837. wdev->wext.connect.crypto.wpa_versions &=
  838. ~(NL80211_WPA_VERSION_1|NL80211_WPA_VERSION_2);
  839. if (wpa_versions & IW_AUTH_WPA_VERSION_WPA)
  840. wdev->wext.connect.crypto.wpa_versions |=
  841. NL80211_WPA_VERSION_1;
  842. if (wpa_versions & IW_AUTH_WPA_VERSION_WPA2)
  843. wdev->wext.connect.crypto.wpa_versions |=
  844. NL80211_WPA_VERSION_2;
  845. return 0;
  846. }
  847. static int cfg80211_set_cipher_group(struct wireless_dev *wdev, u32 cipher)
  848. {
  849. if (cipher & IW_AUTH_CIPHER_WEP40)
  850. wdev->wext.connect.crypto.cipher_group =
  851. WLAN_CIPHER_SUITE_WEP40;
  852. else if (cipher & IW_AUTH_CIPHER_WEP104)
  853. wdev->wext.connect.crypto.cipher_group =
  854. WLAN_CIPHER_SUITE_WEP104;
  855. else if (cipher & IW_AUTH_CIPHER_TKIP)
  856. wdev->wext.connect.crypto.cipher_group =
  857. WLAN_CIPHER_SUITE_TKIP;
  858. else if (cipher & IW_AUTH_CIPHER_CCMP)
  859. wdev->wext.connect.crypto.cipher_group =
  860. WLAN_CIPHER_SUITE_CCMP;
  861. else if (cipher & IW_AUTH_CIPHER_AES_CMAC)
  862. wdev->wext.connect.crypto.cipher_group =
  863. WLAN_CIPHER_SUITE_AES_CMAC;
  864. else if (cipher & IW_AUTH_CIPHER_NONE)
  865. wdev->wext.connect.crypto.cipher_group = 0;
  866. else
  867. return -EINVAL;
  868. return 0;
  869. }
  870. static int cfg80211_set_cipher_pairwise(struct wireless_dev *wdev, u32 cipher)
  871. {
  872. int nr_ciphers = 0;
  873. u32 *ciphers_pairwise = wdev->wext.connect.crypto.ciphers_pairwise;
  874. if (cipher & IW_AUTH_CIPHER_WEP40) {
  875. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP40;
  876. nr_ciphers++;
  877. }
  878. if (cipher & IW_AUTH_CIPHER_WEP104) {
  879. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_WEP104;
  880. nr_ciphers++;
  881. }
  882. if (cipher & IW_AUTH_CIPHER_TKIP) {
  883. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_TKIP;
  884. nr_ciphers++;
  885. }
  886. if (cipher & IW_AUTH_CIPHER_CCMP) {
  887. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_CCMP;
  888. nr_ciphers++;
  889. }
  890. if (cipher & IW_AUTH_CIPHER_AES_CMAC) {
  891. ciphers_pairwise[nr_ciphers] = WLAN_CIPHER_SUITE_AES_CMAC;
  892. nr_ciphers++;
  893. }
  894. BUILD_BUG_ON(NL80211_MAX_NR_CIPHER_SUITES < 5);
  895. wdev->wext.connect.crypto.n_ciphers_pairwise = nr_ciphers;
  896. return 0;
  897. }
  898. static int cfg80211_set_key_mgt(struct wireless_dev *wdev, u32 key_mgt)
  899. {
  900. int nr_akm_suites = 0;
  901. if (key_mgt & ~(IW_AUTH_KEY_MGMT_802_1X |
  902. IW_AUTH_KEY_MGMT_PSK))
  903. return -EINVAL;
  904. if (key_mgt & IW_AUTH_KEY_MGMT_802_1X) {
  905. wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
  906. WLAN_AKM_SUITE_8021X;
  907. nr_akm_suites++;
  908. }
  909. if (key_mgt & IW_AUTH_KEY_MGMT_PSK) {
  910. wdev->wext.connect.crypto.akm_suites[nr_akm_suites] =
  911. WLAN_AKM_SUITE_PSK;
  912. nr_akm_suites++;
  913. }
  914. wdev->wext.connect.crypto.n_akm_suites = nr_akm_suites;
  915. return 0;
  916. }
  917. static int cfg80211_wext_siwauth(struct net_device *dev,
  918. struct iw_request_info *info,
  919. struct iw_param *data, char *extra)
  920. {
  921. struct wireless_dev *wdev = dev->ieee80211_ptr;
  922. if (wdev->iftype != NL80211_IFTYPE_STATION)
  923. return -EOPNOTSUPP;
  924. switch (data->flags & IW_AUTH_INDEX) {
  925. case IW_AUTH_PRIVACY_INVOKED:
  926. wdev->wext.connect.privacy = data->value;
  927. return 0;
  928. case IW_AUTH_WPA_VERSION:
  929. return cfg80211_set_wpa_version(wdev, data->value);
  930. case IW_AUTH_CIPHER_GROUP:
  931. return cfg80211_set_cipher_group(wdev, data->value);
  932. case IW_AUTH_KEY_MGMT:
  933. return cfg80211_set_key_mgt(wdev, data->value);
  934. case IW_AUTH_CIPHER_PAIRWISE:
  935. return cfg80211_set_cipher_pairwise(wdev, data->value);
  936. case IW_AUTH_80211_AUTH_ALG:
  937. return cfg80211_set_auth_alg(wdev, data->value);
  938. case IW_AUTH_WPA_ENABLED:
  939. case IW_AUTH_RX_UNENCRYPTED_EAPOL:
  940. case IW_AUTH_DROP_UNENCRYPTED:
  941. case IW_AUTH_MFP:
  942. return 0;
  943. default:
  944. return -EOPNOTSUPP;
  945. }
  946. }
  947. static int cfg80211_wext_giwauth(struct net_device *dev,
  948. struct iw_request_info *info,
  949. struct iw_param *data, char *extra)
  950. {
  951. /* XXX: what do we need? */
  952. return -EOPNOTSUPP;
  953. }
  954. static int cfg80211_wext_siwpower(struct net_device *dev,
  955. struct iw_request_info *info,
  956. struct iw_param *wrq, char *extra)
  957. {
  958. struct wireless_dev *wdev = dev->ieee80211_ptr;
  959. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  960. bool ps = wdev->ps;
  961. int timeout = wdev->ps_timeout;
  962. int err;
  963. if (wdev->iftype != NL80211_IFTYPE_STATION)
  964. return -EINVAL;
  965. if (!rdev->ops->set_power_mgmt)
  966. return -EOPNOTSUPP;
  967. if (wrq->disabled) {
  968. ps = false;
  969. } else {
  970. switch (wrq->flags & IW_POWER_MODE) {
  971. case IW_POWER_ON: /* If not specified */
  972. case IW_POWER_MODE: /* If set all mask */
  973. case IW_POWER_ALL_R: /* If explicitely state all */
  974. ps = true;
  975. break;
  976. default: /* Otherwise we ignore */
  977. return -EINVAL;
  978. }
  979. if (wrq->flags & ~(IW_POWER_MODE | IW_POWER_TIMEOUT))
  980. return -EINVAL;
  981. if (wrq->flags & IW_POWER_TIMEOUT)
  982. timeout = wrq->value / 1000;
  983. }
  984. err = rdev_set_power_mgmt(rdev, dev, ps, timeout);
  985. if (err)
  986. return err;
  987. wdev->ps = ps;
  988. wdev->ps_timeout = timeout;
  989. return 0;
  990. }
  991. static int cfg80211_wext_giwpower(struct net_device *dev,
  992. struct iw_request_info *info,
  993. struct iw_param *wrq, char *extra)
  994. {
  995. struct wireless_dev *wdev = dev->ieee80211_ptr;
  996. wrq->disabled = !wdev->ps;
  997. return 0;
  998. }
  999. static int cfg80211_wds_wext_siwap(struct net_device *dev,
  1000. struct iw_request_info *info,
  1001. struct sockaddr *addr, char *extra)
  1002. {
  1003. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1004. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  1005. int err;
  1006. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
  1007. return -EINVAL;
  1008. if (addr->sa_family != ARPHRD_ETHER)
  1009. return -EINVAL;
  1010. if (netif_running(dev))
  1011. return -EBUSY;
  1012. if (!rdev->ops->set_wds_peer)
  1013. return -EOPNOTSUPP;
  1014. err = rdev_set_wds_peer(rdev, dev, (u8 *)&addr->sa_data);
  1015. if (err)
  1016. return err;
  1017. memcpy(&wdev->wext.bssid, (u8 *) &addr->sa_data, ETH_ALEN);
  1018. return 0;
  1019. }
  1020. static int cfg80211_wds_wext_giwap(struct net_device *dev,
  1021. struct iw_request_info *info,
  1022. struct sockaddr *addr, char *extra)
  1023. {
  1024. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1025. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_WDS))
  1026. return -EINVAL;
  1027. addr->sa_family = ARPHRD_ETHER;
  1028. memcpy(&addr->sa_data, wdev->wext.bssid, ETH_ALEN);
  1029. return 0;
  1030. }
  1031. static int cfg80211_wext_siwrate(struct net_device *dev,
  1032. struct iw_request_info *info,
  1033. struct iw_param *rate, char *extra)
  1034. {
  1035. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1036. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  1037. struct cfg80211_bitrate_mask mask;
  1038. u32 fixed, maxrate;
  1039. struct ieee80211_supported_band *sband;
  1040. int band, ridx;
  1041. bool match = false;
  1042. if (!rdev->ops->set_bitrate_mask)
  1043. return -EOPNOTSUPP;
  1044. memset(&mask, 0, sizeof(mask));
  1045. fixed = 0;
  1046. maxrate = (u32)-1;
  1047. if (rate->value < 0) {
  1048. /* nothing */
  1049. } else if (rate->fixed) {
  1050. fixed = rate->value / 100000;
  1051. } else {
  1052. maxrate = rate->value / 100000;
  1053. }
  1054. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1055. sband = wdev->wiphy->bands[band];
  1056. if (sband == NULL)
  1057. continue;
  1058. for (ridx = 0; ridx < sband->n_bitrates; ridx++) {
  1059. struct ieee80211_rate *srate = &sband->bitrates[ridx];
  1060. if (fixed == srate->bitrate) {
  1061. mask.control[band].legacy = 1 << ridx;
  1062. match = true;
  1063. break;
  1064. }
  1065. if (srate->bitrate <= maxrate) {
  1066. mask.control[band].legacy |= 1 << ridx;
  1067. match = true;
  1068. }
  1069. }
  1070. }
  1071. if (!match)
  1072. return -EINVAL;
  1073. return rdev_set_bitrate_mask(rdev, dev, NULL, &mask);
  1074. }
  1075. static int cfg80211_wext_giwrate(struct net_device *dev,
  1076. struct iw_request_info *info,
  1077. struct iw_param *rate, char *extra)
  1078. {
  1079. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1080. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  1081. /* we are under RTNL - globally locked - so can use a static struct */
  1082. static struct station_info sinfo;
  1083. u8 addr[ETH_ALEN];
  1084. int err;
  1085. if (wdev->iftype != NL80211_IFTYPE_STATION)
  1086. return -EOPNOTSUPP;
  1087. if (!rdev->ops->get_station)
  1088. return -EOPNOTSUPP;
  1089. err = 0;
  1090. wdev_lock(wdev);
  1091. if (wdev->current_bss)
  1092. memcpy(addr, wdev->current_bss->pub.bssid, ETH_ALEN);
  1093. else
  1094. err = -EOPNOTSUPP;
  1095. wdev_unlock(wdev);
  1096. if (err)
  1097. return err;
  1098. err = rdev_get_station(rdev, dev, addr, &sinfo);
  1099. if (err)
  1100. return err;
  1101. if (!(sinfo.filled & BIT(NL80211_STA_INFO_TX_BITRATE)))
  1102. return -EOPNOTSUPP;
  1103. rate->value = 100000 * cfg80211_calculate_bitrate(&sinfo.txrate);
  1104. return 0;
  1105. }
  1106. /* Get wireless statistics. Called by /proc/net/wireless and by SIOCGIWSTATS */
  1107. static struct iw_statistics *cfg80211_wireless_stats(struct net_device *dev)
  1108. {
  1109. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1110. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  1111. /* we are under RTNL - globally locked - so can use static structs */
  1112. static struct iw_statistics wstats;
  1113. static struct station_info sinfo;
  1114. u8 bssid[ETH_ALEN];
  1115. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_STATION)
  1116. return NULL;
  1117. if (!rdev->ops->get_station)
  1118. return NULL;
  1119. /* Grab BSSID of current BSS, if any */
  1120. wdev_lock(wdev);
  1121. if (!wdev->current_bss) {
  1122. wdev_unlock(wdev);
  1123. return NULL;
  1124. }
  1125. memcpy(bssid, wdev->current_bss->pub.bssid, ETH_ALEN);
  1126. wdev_unlock(wdev);
  1127. memset(&sinfo, 0, sizeof(sinfo));
  1128. if (rdev_get_station(rdev, dev, bssid, &sinfo))
  1129. return NULL;
  1130. memset(&wstats, 0, sizeof(wstats));
  1131. switch (rdev->wiphy.signal_type) {
  1132. case CFG80211_SIGNAL_TYPE_MBM:
  1133. if (sinfo.filled & BIT(NL80211_STA_INFO_SIGNAL)) {
  1134. int sig = sinfo.signal;
  1135. wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
  1136. wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
  1137. wstats.qual.updated |= IW_QUAL_DBM;
  1138. wstats.qual.level = sig;
  1139. if (sig < -110)
  1140. sig = -110;
  1141. else if (sig > -40)
  1142. sig = -40;
  1143. wstats.qual.qual = sig + 110;
  1144. break;
  1145. }
  1146. case CFG80211_SIGNAL_TYPE_UNSPEC:
  1147. if (sinfo.filled & BIT(NL80211_STA_INFO_SIGNAL)) {
  1148. wstats.qual.updated |= IW_QUAL_LEVEL_UPDATED;
  1149. wstats.qual.updated |= IW_QUAL_QUAL_UPDATED;
  1150. wstats.qual.level = sinfo.signal;
  1151. wstats.qual.qual = sinfo.signal;
  1152. break;
  1153. }
  1154. default:
  1155. wstats.qual.updated |= IW_QUAL_LEVEL_INVALID;
  1156. wstats.qual.updated |= IW_QUAL_QUAL_INVALID;
  1157. }
  1158. wstats.qual.updated |= IW_QUAL_NOISE_INVALID;
  1159. if (sinfo.filled & BIT(NL80211_STA_INFO_RX_DROP_MISC))
  1160. wstats.discard.misc = sinfo.rx_dropped_misc;
  1161. if (sinfo.filled & BIT(NL80211_STA_INFO_TX_FAILED))
  1162. wstats.discard.retries = sinfo.tx_failed;
  1163. return &wstats;
  1164. }
  1165. static int cfg80211_wext_siwap(struct net_device *dev,
  1166. struct iw_request_info *info,
  1167. struct sockaddr *ap_addr, char *extra)
  1168. {
  1169. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1170. switch (wdev->iftype) {
  1171. case NL80211_IFTYPE_ADHOC:
  1172. return cfg80211_ibss_wext_siwap(dev, info, ap_addr, extra);
  1173. case NL80211_IFTYPE_STATION:
  1174. return cfg80211_mgd_wext_siwap(dev, info, ap_addr, extra);
  1175. case NL80211_IFTYPE_WDS:
  1176. return cfg80211_wds_wext_siwap(dev, info, ap_addr, extra);
  1177. default:
  1178. return -EOPNOTSUPP;
  1179. }
  1180. }
  1181. static int cfg80211_wext_giwap(struct net_device *dev,
  1182. struct iw_request_info *info,
  1183. struct sockaddr *ap_addr, char *extra)
  1184. {
  1185. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1186. switch (wdev->iftype) {
  1187. case NL80211_IFTYPE_ADHOC:
  1188. return cfg80211_ibss_wext_giwap(dev, info, ap_addr, extra);
  1189. case NL80211_IFTYPE_STATION:
  1190. return cfg80211_mgd_wext_giwap(dev, info, ap_addr, extra);
  1191. case NL80211_IFTYPE_WDS:
  1192. return cfg80211_wds_wext_giwap(dev, info, ap_addr, extra);
  1193. default:
  1194. return -EOPNOTSUPP;
  1195. }
  1196. }
  1197. static int cfg80211_wext_siwessid(struct net_device *dev,
  1198. struct iw_request_info *info,
  1199. struct iw_point *data, char *ssid)
  1200. {
  1201. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1202. switch (wdev->iftype) {
  1203. case NL80211_IFTYPE_ADHOC:
  1204. return cfg80211_ibss_wext_siwessid(dev, info, data, ssid);
  1205. case NL80211_IFTYPE_STATION:
  1206. return cfg80211_mgd_wext_siwessid(dev, info, data, ssid);
  1207. default:
  1208. return -EOPNOTSUPP;
  1209. }
  1210. }
  1211. static int cfg80211_wext_giwessid(struct net_device *dev,
  1212. struct iw_request_info *info,
  1213. struct iw_point *data, char *ssid)
  1214. {
  1215. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1216. data->flags = 0;
  1217. data->length = 0;
  1218. switch (wdev->iftype) {
  1219. case NL80211_IFTYPE_ADHOC:
  1220. return cfg80211_ibss_wext_giwessid(dev, info, data, ssid);
  1221. case NL80211_IFTYPE_STATION:
  1222. return cfg80211_mgd_wext_giwessid(dev, info, data, ssid);
  1223. default:
  1224. return -EOPNOTSUPP;
  1225. }
  1226. }
  1227. static int cfg80211_wext_siwpmksa(struct net_device *dev,
  1228. struct iw_request_info *info,
  1229. struct iw_point *data, char *extra)
  1230. {
  1231. struct wireless_dev *wdev = dev->ieee80211_ptr;
  1232. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
  1233. struct cfg80211_pmksa cfg_pmksa;
  1234. struct iw_pmksa *pmksa = (struct iw_pmksa *)extra;
  1235. memset(&cfg_pmksa, 0, sizeof(struct cfg80211_pmksa));
  1236. if (wdev->iftype != NL80211_IFTYPE_STATION)
  1237. return -EINVAL;
  1238. cfg_pmksa.bssid = pmksa->bssid.sa_data;
  1239. cfg_pmksa.pmkid = pmksa->pmkid;
  1240. switch (pmksa->cmd) {
  1241. case IW_PMKSA_ADD:
  1242. if (!rdev->ops->set_pmksa)
  1243. return -EOPNOTSUPP;
  1244. return rdev_set_pmksa(rdev, dev, &cfg_pmksa);
  1245. case IW_PMKSA_REMOVE:
  1246. if (!rdev->ops->del_pmksa)
  1247. return -EOPNOTSUPP;
  1248. return rdev_del_pmksa(rdev, dev, &cfg_pmksa);
  1249. case IW_PMKSA_FLUSH:
  1250. if (!rdev->ops->flush_pmksa)
  1251. return -EOPNOTSUPP;
  1252. return rdev_flush_pmksa(rdev, dev);
  1253. default:
  1254. return -EOPNOTSUPP;
  1255. }
  1256. }
  1257. static const iw_handler cfg80211_handlers[] = {
  1258. [IW_IOCTL_IDX(SIOCGIWNAME)] = (iw_handler) cfg80211_wext_giwname,
  1259. [IW_IOCTL_IDX(SIOCSIWFREQ)] = (iw_handler) cfg80211_wext_siwfreq,
  1260. [IW_IOCTL_IDX(SIOCGIWFREQ)] = (iw_handler) cfg80211_wext_giwfreq,
  1261. [IW_IOCTL_IDX(SIOCSIWMODE)] = (iw_handler) cfg80211_wext_siwmode,
  1262. [IW_IOCTL_IDX(SIOCGIWMODE)] = (iw_handler) cfg80211_wext_giwmode,
  1263. [IW_IOCTL_IDX(SIOCGIWRANGE)] = (iw_handler) cfg80211_wext_giwrange,
  1264. [IW_IOCTL_IDX(SIOCSIWAP)] = (iw_handler) cfg80211_wext_siwap,
  1265. [IW_IOCTL_IDX(SIOCGIWAP)] = (iw_handler) cfg80211_wext_giwap,
  1266. [IW_IOCTL_IDX(SIOCSIWMLME)] = (iw_handler) cfg80211_wext_siwmlme,
  1267. [IW_IOCTL_IDX(SIOCSIWSCAN)] = (iw_handler) cfg80211_wext_siwscan,
  1268. [IW_IOCTL_IDX(SIOCGIWSCAN)] = (iw_handler) cfg80211_wext_giwscan,
  1269. [IW_IOCTL_IDX(SIOCSIWESSID)] = (iw_handler) cfg80211_wext_siwessid,
  1270. [IW_IOCTL_IDX(SIOCGIWESSID)] = (iw_handler) cfg80211_wext_giwessid,
  1271. [IW_IOCTL_IDX(SIOCSIWRATE)] = (iw_handler) cfg80211_wext_siwrate,
  1272. [IW_IOCTL_IDX(SIOCGIWRATE)] = (iw_handler) cfg80211_wext_giwrate,
  1273. [IW_IOCTL_IDX(SIOCSIWRTS)] = (iw_handler) cfg80211_wext_siwrts,
  1274. [IW_IOCTL_IDX(SIOCGIWRTS)] = (iw_handler) cfg80211_wext_giwrts,
  1275. [IW_IOCTL_IDX(SIOCSIWFRAG)] = (iw_handler) cfg80211_wext_siwfrag,
  1276. [IW_IOCTL_IDX(SIOCGIWFRAG)] = (iw_handler) cfg80211_wext_giwfrag,
  1277. [IW_IOCTL_IDX(SIOCSIWTXPOW)] = (iw_handler) cfg80211_wext_siwtxpower,
  1278. [IW_IOCTL_IDX(SIOCGIWTXPOW)] = (iw_handler) cfg80211_wext_giwtxpower,
  1279. [IW_IOCTL_IDX(SIOCSIWRETRY)] = (iw_handler) cfg80211_wext_siwretry,
  1280. [IW_IOCTL_IDX(SIOCGIWRETRY)] = (iw_handler) cfg80211_wext_giwretry,
  1281. [IW_IOCTL_IDX(SIOCSIWENCODE)] = (iw_handler) cfg80211_wext_siwencode,
  1282. [IW_IOCTL_IDX(SIOCGIWENCODE)] = (iw_handler) cfg80211_wext_giwencode,
  1283. [IW_IOCTL_IDX(SIOCSIWPOWER)] = (iw_handler) cfg80211_wext_siwpower,
  1284. [IW_IOCTL_IDX(SIOCGIWPOWER)] = (iw_handler) cfg80211_wext_giwpower,
  1285. [IW_IOCTL_IDX(SIOCSIWGENIE)] = (iw_handler) cfg80211_wext_siwgenie,
  1286. [IW_IOCTL_IDX(SIOCSIWAUTH)] = (iw_handler) cfg80211_wext_siwauth,
  1287. [IW_IOCTL_IDX(SIOCGIWAUTH)] = (iw_handler) cfg80211_wext_giwauth,
  1288. [IW_IOCTL_IDX(SIOCSIWENCODEEXT)]= (iw_handler) cfg80211_wext_siwencodeext,
  1289. [IW_IOCTL_IDX(SIOCSIWPMKSA)] = (iw_handler) cfg80211_wext_siwpmksa,
  1290. };
  1291. const struct iw_handler_def cfg80211_wext_handler = {
  1292. .num_standard = ARRAY_SIZE(cfg80211_handlers),
  1293. .standard = cfg80211_handlers,
  1294. .get_wireless_stats = cfg80211_wireless_stats,
  1295. };