wext-compat.c 39 KB

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