wext-compat.c 39 KB

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