wext-compat.c 39 KB

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