wext-compat.c 40 KB

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