wext-compat.c 39 KB

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