/net/wireless/wext-compat.c

http://github.com/mirrors/linux · C · 1515 lines · 1214 code · 231 blank · 70 comment · 297 complexity · a8bc1358d57d0bfe472a68dec677897d MD5 · raw file

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