wext-compat.c 39 KB

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