wext-compat.c 39 KB

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