cfg.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Implement cfg80211 ("iw") support.
  4. *
  5. * Copyright (C) 2009 M&N Solutions GmbH, 61191 Rosbach, Germany
  6. * Holger Schurig <hs4233@mail.mn-solutions.de>
  7. *
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/hardirq.h>
  11. #include <linux/sched.h>
  12. #include <linux/wait.h>
  13. #include <linux/slab.h>
  14. #include <linux/ieee80211.h>
  15. #include <net/cfg80211.h>
  16. #include <asm/unaligned.h>
  17. #include "decl.h"
  18. #include "cfg.h"
  19. #include "cmd.h"
  20. #include "mesh.h"
  21. #define CHAN2G(_channel, _freq, _flags) { \
  22. .band = NL80211_BAND_2GHZ, \
  23. .center_freq = (_freq), \
  24. .hw_value = (_channel), \
  25. .flags = (_flags), \
  26. .max_antenna_gain = 0, \
  27. .max_power = 30, \
  28. }
  29. static struct ieee80211_channel lbs_2ghz_channels[] = {
  30. CHAN2G(1, 2412, 0),
  31. CHAN2G(2, 2417, 0),
  32. CHAN2G(3, 2422, 0),
  33. CHAN2G(4, 2427, 0),
  34. CHAN2G(5, 2432, 0),
  35. CHAN2G(6, 2437, 0),
  36. CHAN2G(7, 2442, 0),
  37. CHAN2G(8, 2447, 0),
  38. CHAN2G(9, 2452, 0),
  39. CHAN2G(10, 2457, 0),
  40. CHAN2G(11, 2462, 0),
  41. CHAN2G(12, 2467, 0),
  42. CHAN2G(13, 2472, 0),
  43. CHAN2G(14, 2484, 0),
  44. };
  45. #define RATETAB_ENT(_rate, _hw_value, _flags) { \
  46. .bitrate = (_rate), \
  47. .hw_value = (_hw_value), \
  48. .flags = (_flags), \
  49. }
  50. /* Table 6 in section 3.2.1.1 */
  51. static struct ieee80211_rate lbs_rates[] = {
  52. RATETAB_ENT(10, 0, 0),
  53. RATETAB_ENT(20, 1, 0),
  54. RATETAB_ENT(55, 2, 0),
  55. RATETAB_ENT(110, 3, 0),
  56. RATETAB_ENT(60, 9, 0),
  57. RATETAB_ENT(90, 6, 0),
  58. RATETAB_ENT(120, 7, 0),
  59. RATETAB_ENT(180, 8, 0),
  60. RATETAB_ENT(240, 9, 0),
  61. RATETAB_ENT(360, 10, 0),
  62. RATETAB_ENT(480, 11, 0),
  63. RATETAB_ENT(540, 12, 0),
  64. };
  65. static struct ieee80211_supported_band lbs_band_2ghz = {
  66. .channels = lbs_2ghz_channels,
  67. .n_channels = ARRAY_SIZE(lbs_2ghz_channels),
  68. .bitrates = lbs_rates,
  69. .n_bitrates = ARRAY_SIZE(lbs_rates),
  70. };
  71. static const u32 cipher_suites[] = {
  72. WLAN_CIPHER_SUITE_WEP40,
  73. WLAN_CIPHER_SUITE_WEP104,
  74. WLAN_CIPHER_SUITE_TKIP,
  75. WLAN_CIPHER_SUITE_CCMP,
  76. };
  77. /* Time to stay on the channel */
  78. #define LBS_DWELL_PASSIVE 100
  79. #define LBS_DWELL_ACTIVE 40
  80. /***************************************************************************
  81. * Misc utility functions
  82. *
  83. * TLVs are Marvell specific. They are very similar to IEs, they have the
  84. * same structure: type, length, data*. The only difference: for IEs, the
  85. * type and length are u8, but for TLVs they're __le16.
  86. */
  87. /*
  88. * Convert NL80211's auth_type to the one from Libertas, see chapter 5.9.1
  89. * in the firmware spec
  90. */
  91. static int lbs_auth_to_authtype(enum nl80211_auth_type auth_type)
  92. {
  93. int ret = -ENOTSUPP;
  94. switch (auth_type) {
  95. case NL80211_AUTHTYPE_OPEN_SYSTEM:
  96. case NL80211_AUTHTYPE_SHARED_KEY:
  97. ret = auth_type;
  98. break;
  99. case NL80211_AUTHTYPE_AUTOMATIC:
  100. ret = NL80211_AUTHTYPE_OPEN_SYSTEM;
  101. break;
  102. case NL80211_AUTHTYPE_NETWORK_EAP:
  103. ret = 0x80;
  104. break;
  105. default:
  106. /* silence compiler */
  107. break;
  108. }
  109. return ret;
  110. }
  111. /*
  112. * Various firmware commands need the list of supported rates, but with
  113. * the hight-bit set for basic rates
  114. */
  115. static int lbs_add_rates(u8 *rates)
  116. {
  117. size_t i;
  118. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  119. u8 rate = lbs_rates[i].bitrate / 5;
  120. if (rate == 0x02 || rate == 0x04 ||
  121. rate == 0x0b || rate == 0x16)
  122. rate |= 0x80;
  123. rates[i] = rate;
  124. }
  125. return ARRAY_SIZE(lbs_rates);
  126. }
  127. /***************************************************************************
  128. * TLV utility functions
  129. *
  130. * TLVs are Marvell specific. They are very similar to IEs, they have the
  131. * same structure: type, length, data*. The only difference: for IEs, the
  132. * type and length are u8, but for TLVs they're __le16.
  133. */
  134. /*
  135. * Add ssid TLV
  136. */
  137. #define LBS_MAX_SSID_TLV_SIZE \
  138. (sizeof(struct mrvl_ie_header) \
  139. + IEEE80211_MAX_SSID_LEN)
  140. static int lbs_add_ssid_tlv(u8 *tlv, const u8 *ssid, int ssid_len)
  141. {
  142. struct mrvl_ie_ssid_param_set *ssid_tlv = (void *)tlv;
  143. /*
  144. * TLV-ID SSID 00 00
  145. * length 06 00
  146. * ssid 4d 4e 54 45 53 54
  147. */
  148. ssid_tlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
  149. ssid_tlv->header.len = cpu_to_le16(ssid_len);
  150. memcpy(ssid_tlv->ssid, ssid, ssid_len);
  151. return sizeof(ssid_tlv->header) + ssid_len;
  152. }
  153. /*
  154. * Add channel list TLV (section 8.4.2)
  155. *
  156. * Actual channel data comes from priv->wdev->wiphy->channels.
  157. */
  158. #define LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  159. (sizeof(struct mrvl_ie_header) \
  160. + (LBS_SCAN_BEFORE_NAP * sizeof(struct chanscanparamset)))
  161. static int lbs_add_channel_list_tlv(struct lbs_private *priv, u8 *tlv,
  162. int last_channel, int active_scan)
  163. {
  164. int chanscanparamsize = sizeof(struct chanscanparamset) *
  165. (last_channel - priv->scan_channel);
  166. struct mrvl_ie_header *header = (void *) tlv;
  167. /*
  168. * TLV-ID CHANLIST 01 01
  169. * length 0e 00
  170. * channel 00 01 00 00 00 64 00
  171. * radio type 00
  172. * channel 01
  173. * scan type 00
  174. * min scan time 00 00
  175. * max scan time 64 00
  176. * channel 2 00 02 00 00 00 64 00
  177. *
  178. */
  179. header->type = cpu_to_le16(TLV_TYPE_CHANLIST);
  180. header->len = cpu_to_le16(chanscanparamsize);
  181. tlv += sizeof(struct mrvl_ie_header);
  182. /* lbs_deb_scan("scan: channels %d to %d\n", priv->scan_channel,
  183. last_channel); */
  184. memset(tlv, 0, chanscanparamsize);
  185. while (priv->scan_channel < last_channel) {
  186. struct chanscanparamset *param = (void *) tlv;
  187. param->radiotype = CMD_SCAN_RADIO_TYPE_BG;
  188. param->channumber =
  189. priv->scan_req->channels[priv->scan_channel]->hw_value;
  190. if (active_scan) {
  191. param->maxscantime = cpu_to_le16(LBS_DWELL_ACTIVE);
  192. } else {
  193. param->chanscanmode.passivescan = 1;
  194. param->maxscantime = cpu_to_le16(LBS_DWELL_PASSIVE);
  195. }
  196. tlv += sizeof(struct chanscanparamset);
  197. priv->scan_channel++;
  198. }
  199. return sizeof(struct mrvl_ie_header) + chanscanparamsize;
  200. }
  201. /*
  202. * Add rates TLV
  203. *
  204. * The rates are in lbs_bg_rates[], but for the 802.11b
  205. * rates the high bit is set. We add this TLV only because
  206. * there's a firmware which otherwise doesn't report all
  207. * APs in range.
  208. */
  209. #define LBS_MAX_RATES_TLV_SIZE \
  210. (sizeof(struct mrvl_ie_header) \
  211. + (ARRAY_SIZE(lbs_rates)))
  212. /* Adds a TLV with all rates the hardware supports */
  213. static int lbs_add_supported_rates_tlv(u8 *tlv)
  214. {
  215. size_t i;
  216. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  217. /*
  218. * TLV-ID RATES 01 00
  219. * length 0e 00
  220. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c
  221. */
  222. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  223. tlv += sizeof(rate_tlv->header);
  224. i = lbs_add_rates(tlv);
  225. tlv += i;
  226. rate_tlv->header.len = cpu_to_le16(i);
  227. return sizeof(rate_tlv->header) + i;
  228. }
  229. /* Add common rates from a TLV and return the new end of the TLV */
  230. static u8 *
  231. add_ie_rates(u8 *tlv, const u8 *ie, int *nrates)
  232. {
  233. int hw, ap, ap_max = ie[1];
  234. u8 hw_rate;
  235. if (ap_max > MAX_RATES) {
  236. lbs_deb_assoc("invalid rates\n");
  237. return tlv;
  238. }
  239. /* Advance past IE header */
  240. ie += 2;
  241. lbs_deb_hex(LBS_DEB_ASSOC, "AP IE Rates", (u8 *) ie, ap_max);
  242. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  243. hw_rate = lbs_rates[hw].bitrate / 5;
  244. for (ap = 0; ap < ap_max; ap++) {
  245. if (hw_rate == (ie[ap] & 0x7f)) {
  246. *tlv++ = ie[ap];
  247. *nrates = *nrates + 1;
  248. }
  249. }
  250. }
  251. return tlv;
  252. }
  253. /*
  254. * Adds a TLV with all rates the hardware *and* BSS supports.
  255. */
  256. static int lbs_add_common_rates_tlv(u8 *tlv, struct cfg80211_bss *bss)
  257. {
  258. struct mrvl_ie_rates_param_set *rate_tlv = (void *)tlv;
  259. const u8 *rates_eid, *ext_rates_eid;
  260. int n = 0;
  261. rcu_read_lock();
  262. rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  263. ext_rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_EXT_SUPP_RATES);
  264. /*
  265. * 01 00 TLV_TYPE_RATES
  266. * 04 00 len
  267. * 82 84 8b 96 rates
  268. */
  269. rate_tlv->header.type = cpu_to_le16(TLV_TYPE_RATES);
  270. tlv += sizeof(rate_tlv->header);
  271. /* Add basic rates */
  272. if (rates_eid) {
  273. tlv = add_ie_rates(tlv, rates_eid, &n);
  274. /* Add extended rates, if any */
  275. if (ext_rates_eid)
  276. tlv = add_ie_rates(tlv, ext_rates_eid, &n);
  277. } else {
  278. lbs_deb_assoc("assoc: bss had no basic rate IE\n");
  279. /* Fallback: add basic 802.11b rates */
  280. *tlv++ = 0x82;
  281. *tlv++ = 0x84;
  282. *tlv++ = 0x8b;
  283. *tlv++ = 0x96;
  284. n = 4;
  285. }
  286. rcu_read_unlock();
  287. rate_tlv->header.len = cpu_to_le16(n);
  288. return sizeof(rate_tlv->header) + n;
  289. }
  290. /*
  291. * Add auth type TLV.
  292. *
  293. * This is only needed for newer firmware (V9 and up).
  294. */
  295. #define LBS_MAX_AUTH_TYPE_TLV_SIZE \
  296. sizeof(struct mrvl_ie_auth_type)
  297. static int lbs_add_auth_type_tlv(u8 *tlv, enum nl80211_auth_type auth_type)
  298. {
  299. struct mrvl_ie_auth_type *auth = (void *) tlv;
  300. /*
  301. * 1f 01 TLV_TYPE_AUTH_TYPE
  302. * 01 00 len
  303. * 01 auth type
  304. */
  305. auth->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
  306. auth->header.len = cpu_to_le16(sizeof(*auth)-sizeof(auth->header));
  307. auth->auth = cpu_to_le16(lbs_auth_to_authtype(auth_type));
  308. return sizeof(*auth);
  309. }
  310. /*
  311. * Add channel (phy ds) TLV
  312. */
  313. #define LBS_MAX_CHANNEL_TLV_SIZE \
  314. sizeof(struct mrvl_ie_header)
  315. static int lbs_add_channel_tlv(u8 *tlv, u8 channel)
  316. {
  317. struct mrvl_ie_ds_param_set *ds = (void *) tlv;
  318. /*
  319. * 03 00 TLV_TYPE_PHY_DS
  320. * 01 00 len
  321. * 06 channel
  322. */
  323. ds->header.type = cpu_to_le16(TLV_TYPE_PHY_DS);
  324. ds->header.len = cpu_to_le16(sizeof(*ds)-sizeof(ds->header));
  325. ds->channel = channel;
  326. return sizeof(*ds);
  327. }
  328. /*
  329. * Add (empty) CF param TLV of the form:
  330. */
  331. #define LBS_MAX_CF_PARAM_TLV_SIZE \
  332. sizeof(struct mrvl_ie_header)
  333. static int lbs_add_cf_param_tlv(u8 *tlv)
  334. {
  335. struct mrvl_ie_cf_param_set *cf = (void *)tlv;
  336. /*
  337. * 04 00 TLV_TYPE_CF
  338. * 06 00 len
  339. * 00 cfpcnt
  340. * 00 cfpperiod
  341. * 00 00 cfpmaxduration
  342. * 00 00 cfpdurationremaining
  343. */
  344. cf->header.type = cpu_to_le16(TLV_TYPE_CF);
  345. cf->header.len = cpu_to_le16(sizeof(*cf)-sizeof(cf->header));
  346. return sizeof(*cf);
  347. }
  348. /*
  349. * Add WPA TLV
  350. */
  351. #define LBS_MAX_WPA_TLV_SIZE \
  352. (sizeof(struct mrvl_ie_header) \
  353. + 128 /* TODO: I guessed the size */)
  354. static int lbs_add_wpa_tlv(u8 *tlv, const u8 *ie, u8 ie_len)
  355. {
  356. size_t tlv_len;
  357. /*
  358. * We need just convert an IE to an TLV. IEs use u8 for the header,
  359. * u8 type
  360. * u8 len
  361. * u8[] data
  362. * but TLVs use __le16 instead:
  363. * __le16 type
  364. * __le16 len
  365. * u8[] data
  366. */
  367. *tlv++ = *ie++;
  368. *tlv++ = 0;
  369. tlv_len = *tlv++ = *ie++;
  370. *tlv++ = 0;
  371. while (tlv_len--)
  372. *tlv++ = *ie++;
  373. /* the TLV is two bytes larger than the IE */
  374. return ie_len + 2;
  375. }
  376. /*
  377. * Set Channel
  378. */
  379. static int lbs_cfg_set_monitor_channel(struct wiphy *wiphy,
  380. struct cfg80211_chan_def *chandef)
  381. {
  382. struct lbs_private *priv = wiphy_priv(wiphy);
  383. int ret = -ENOTSUPP;
  384. if (cfg80211_get_chandef_type(chandef) != NL80211_CHAN_NO_HT)
  385. goto out;
  386. ret = lbs_set_channel(priv, chandef->chan->hw_value);
  387. out:
  388. return ret;
  389. }
  390. static int lbs_cfg_set_mesh_channel(struct wiphy *wiphy,
  391. struct net_device *netdev,
  392. struct ieee80211_channel *channel)
  393. {
  394. struct lbs_private *priv = wiphy_priv(wiphy);
  395. int ret = -ENOTSUPP;
  396. if (netdev != priv->mesh_dev)
  397. goto out;
  398. ret = lbs_mesh_set_channel(priv, channel->hw_value);
  399. out:
  400. return ret;
  401. }
  402. /*
  403. * Scanning
  404. */
  405. /*
  406. * When scanning, the firmware doesn't send a nul packet with the power-safe
  407. * bit to the AP. So we cannot stay away from our current channel too long,
  408. * otherwise we loose data. So take a "nap" while scanning every other
  409. * while.
  410. */
  411. #define LBS_SCAN_BEFORE_NAP 4
  412. /*
  413. * When the firmware reports back a scan-result, it gives us an "u8 rssi",
  414. * which isn't really an RSSI, as it becomes larger when moving away from
  415. * the AP. Anyway, we need to convert that into mBm.
  416. */
  417. #define LBS_SCAN_RSSI_TO_MBM(rssi) \
  418. ((-(int)rssi + 3)*100)
  419. static int lbs_ret_scan(struct lbs_private *priv, unsigned long dummy,
  420. struct cmd_header *resp)
  421. {
  422. struct cfg80211_bss *bss;
  423. struct cmd_ds_802_11_scan_rsp *scanresp = (void *)resp;
  424. int bsssize;
  425. const u8 *pos;
  426. const u8 *tsfdesc;
  427. int tsfsize;
  428. int i;
  429. int ret = -EILSEQ;
  430. bsssize = get_unaligned_le16(&scanresp->bssdescriptsize);
  431. lbs_deb_scan("scan response: %d BSSs (%d bytes); resp size %d bytes\n",
  432. scanresp->nr_sets, bsssize, le16_to_cpu(resp->size));
  433. if (scanresp->nr_sets == 0) {
  434. ret = 0;
  435. goto done;
  436. }
  437. /*
  438. * The general layout of the scan response is described in chapter
  439. * 5.7.1. Basically we have a common part, then any number of BSS
  440. * descriptor sections. Finally we have section with the same number
  441. * of TSFs.
  442. *
  443. * cmd_ds_802_11_scan_rsp
  444. * cmd_header
  445. * pos_size
  446. * nr_sets
  447. * bssdesc 1
  448. * bssid
  449. * rssi
  450. * timestamp
  451. * intvl
  452. * capa
  453. * IEs
  454. * bssdesc 2
  455. * bssdesc n
  456. * MrvlIEtypes_TsfFimestamp_t
  457. * TSF for BSS 1
  458. * TSF for BSS 2
  459. * TSF for BSS n
  460. */
  461. pos = scanresp->bssdesc_and_tlvbuffer;
  462. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_RSP", scanresp->bssdesc_and_tlvbuffer,
  463. scanresp->bssdescriptsize);
  464. tsfdesc = pos + bsssize;
  465. tsfsize = 4 + 8 * scanresp->nr_sets;
  466. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TSF", (u8 *) tsfdesc, tsfsize);
  467. /* Validity check: we expect a Marvell-Local TLV */
  468. i = get_unaligned_le16(tsfdesc);
  469. tsfdesc += 2;
  470. if (i != TLV_TYPE_TSFTIMESTAMP) {
  471. lbs_deb_scan("scan response: invalid TSF Timestamp %d\n", i);
  472. goto done;
  473. }
  474. /*
  475. * Validity check: the TLV holds TSF values with 8 bytes each, so
  476. * the size in the TLV must match the nr_sets value
  477. */
  478. i = get_unaligned_le16(tsfdesc);
  479. tsfdesc += 2;
  480. if (i / 8 != scanresp->nr_sets) {
  481. lbs_deb_scan("scan response: invalid number of TSF timestamp "
  482. "sets (expected %d got %d)\n", scanresp->nr_sets,
  483. i / 8);
  484. goto done;
  485. }
  486. for (i = 0; i < scanresp->nr_sets; i++) {
  487. const u8 *bssid;
  488. const u8 *ie;
  489. int left;
  490. int ielen;
  491. int rssi;
  492. u16 intvl;
  493. u16 capa;
  494. int chan_no = -1;
  495. const u8 *ssid = NULL;
  496. u8 ssid_len = 0;
  497. int len = get_unaligned_le16(pos);
  498. pos += 2;
  499. /* BSSID */
  500. bssid = pos;
  501. pos += ETH_ALEN;
  502. /* RSSI */
  503. rssi = *pos++;
  504. /* Packet time stamp */
  505. pos += 8;
  506. /* Beacon interval */
  507. intvl = get_unaligned_le16(pos);
  508. pos += 2;
  509. /* Capabilities */
  510. capa = get_unaligned_le16(pos);
  511. pos += 2;
  512. /* To find out the channel, we must parse the IEs */
  513. ie = pos;
  514. /*
  515. * 6+1+8+2+2: size of BSSID, RSSI, time stamp, beacon
  516. * interval, capabilities
  517. */
  518. ielen = left = len - (6 + 1 + 8 + 2 + 2);
  519. while (left >= 2) {
  520. u8 id, elen;
  521. id = *pos++;
  522. elen = *pos++;
  523. left -= 2;
  524. if (elen > left) {
  525. lbs_deb_scan("scan response: invalid IE fmt\n");
  526. goto done;
  527. }
  528. if (id == WLAN_EID_DS_PARAMS)
  529. chan_no = *pos;
  530. if (id == WLAN_EID_SSID) {
  531. ssid = pos;
  532. ssid_len = elen;
  533. }
  534. left -= elen;
  535. pos += elen;
  536. }
  537. /* No channel, no luck */
  538. if (chan_no != -1) {
  539. struct wiphy *wiphy = priv->wdev->wiphy;
  540. int freq = ieee80211_channel_to_frequency(chan_no,
  541. NL80211_BAND_2GHZ);
  542. struct ieee80211_channel *channel =
  543. ieee80211_get_channel(wiphy, freq);
  544. lbs_deb_scan("scan: %pM, capa %04x, chan %2d, %*pE, %d dBm\n",
  545. bssid, capa, chan_no, ssid_len, ssid,
  546. LBS_SCAN_RSSI_TO_MBM(rssi)/100);
  547. if (channel &&
  548. !(channel->flags & IEEE80211_CHAN_DISABLED)) {
  549. bss = cfg80211_inform_bss(wiphy, channel,
  550. CFG80211_BSS_FTYPE_UNKNOWN,
  551. bssid, get_unaligned_le64(tsfdesc),
  552. capa, intvl, ie, ielen,
  553. LBS_SCAN_RSSI_TO_MBM(rssi),
  554. GFP_KERNEL);
  555. cfg80211_put_bss(wiphy, bss);
  556. }
  557. } else
  558. lbs_deb_scan("scan response: missing BSS channel IE\n");
  559. tsfdesc += 8;
  560. }
  561. ret = 0;
  562. done:
  563. return ret;
  564. }
  565. /*
  566. * Our scan command contains a TLV, consting of a SSID TLV, a channel list
  567. * TLV and a rates TLV. Determine the maximum size of them:
  568. */
  569. #define LBS_SCAN_MAX_CMD_SIZE \
  570. (sizeof(struct cmd_ds_802_11_scan) \
  571. + LBS_MAX_SSID_TLV_SIZE \
  572. + LBS_MAX_CHANNEL_LIST_TLV_SIZE \
  573. + LBS_MAX_RATES_TLV_SIZE)
  574. /*
  575. * Assumes priv->scan_req is initialized and valid
  576. * Assumes priv->scan_channel is initialized
  577. */
  578. static void lbs_scan_worker(struct work_struct *work)
  579. {
  580. struct lbs_private *priv =
  581. container_of(work, struct lbs_private, scan_work.work);
  582. struct cmd_ds_802_11_scan *scan_cmd;
  583. u8 *tlv; /* pointer into our current, growing TLV storage area */
  584. int last_channel;
  585. int running, carrier;
  586. scan_cmd = kzalloc(LBS_SCAN_MAX_CMD_SIZE, GFP_KERNEL);
  587. if (scan_cmd == NULL)
  588. return;
  589. /* prepare fixed part of scan command */
  590. scan_cmd->bsstype = CMD_BSS_TYPE_ANY;
  591. /* stop network while we're away from our main channel */
  592. running = !netif_queue_stopped(priv->dev);
  593. carrier = netif_carrier_ok(priv->dev);
  594. if (running)
  595. netif_stop_queue(priv->dev);
  596. if (carrier)
  597. netif_carrier_off(priv->dev);
  598. /* prepare fixed part of scan command */
  599. tlv = scan_cmd->tlvbuffer;
  600. /* add SSID TLV */
  601. if (priv->scan_req->n_ssids && priv->scan_req->ssids[0].ssid_len > 0)
  602. tlv += lbs_add_ssid_tlv(tlv,
  603. priv->scan_req->ssids[0].ssid,
  604. priv->scan_req->ssids[0].ssid_len);
  605. /* add channel TLVs */
  606. last_channel = priv->scan_channel + LBS_SCAN_BEFORE_NAP;
  607. if (last_channel > priv->scan_req->n_channels)
  608. last_channel = priv->scan_req->n_channels;
  609. tlv += lbs_add_channel_list_tlv(priv, tlv, last_channel,
  610. priv->scan_req->n_ssids);
  611. /* add rates TLV */
  612. tlv += lbs_add_supported_rates_tlv(tlv);
  613. if (priv->scan_channel < priv->scan_req->n_channels) {
  614. cancel_delayed_work(&priv->scan_work);
  615. if (netif_running(priv->dev))
  616. queue_delayed_work(priv->work_thread, &priv->scan_work,
  617. msecs_to_jiffies(300));
  618. }
  619. /* This is the final data we are about to send */
  620. scan_cmd->hdr.size = cpu_to_le16(tlv - (u8 *)scan_cmd);
  621. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_CMD", (void *)scan_cmd,
  622. sizeof(*scan_cmd));
  623. lbs_deb_hex(LBS_DEB_SCAN, "SCAN_TLV", scan_cmd->tlvbuffer,
  624. tlv - scan_cmd->tlvbuffer);
  625. __lbs_cmd(priv, CMD_802_11_SCAN, &scan_cmd->hdr,
  626. le16_to_cpu(scan_cmd->hdr.size),
  627. lbs_ret_scan, 0);
  628. if (priv->scan_channel >= priv->scan_req->n_channels) {
  629. /* Mark scan done */
  630. cancel_delayed_work(&priv->scan_work);
  631. lbs_scan_done(priv);
  632. }
  633. /* Restart network */
  634. if (carrier)
  635. netif_carrier_on(priv->dev);
  636. if (running && !priv->tx_pending_len)
  637. netif_wake_queue(priv->dev);
  638. kfree(scan_cmd);
  639. /* Wake up anything waiting on scan completion */
  640. if (priv->scan_req == NULL) {
  641. lbs_deb_scan("scan: waking up waiters\n");
  642. wake_up_all(&priv->scan_q);
  643. }
  644. }
  645. static void _internal_start_scan(struct lbs_private *priv, bool internal,
  646. struct cfg80211_scan_request *request)
  647. {
  648. lbs_deb_scan("scan: ssids %d, channels %d, ie_len %zd\n",
  649. request->n_ssids, request->n_channels, request->ie_len);
  650. priv->scan_channel = 0;
  651. priv->scan_req = request;
  652. priv->internal_scan = internal;
  653. queue_delayed_work(priv->work_thread, &priv->scan_work,
  654. msecs_to_jiffies(50));
  655. }
  656. /*
  657. * Clean up priv->scan_req. Should be used to handle the allocation details.
  658. */
  659. void lbs_scan_done(struct lbs_private *priv)
  660. {
  661. WARN_ON(!priv->scan_req);
  662. if (priv->internal_scan) {
  663. kfree(priv->scan_req);
  664. } else {
  665. struct cfg80211_scan_info info = {
  666. .aborted = false,
  667. };
  668. cfg80211_scan_done(priv->scan_req, &info);
  669. }
  670. priv->scan_req = NULL;
  671. }
  672. static int lbs_cfg_scan(struct wiphy *wiphy,
  673. struct cfg80211_scan_request *request)
  674. {
  675. struct lbs_private *priv = wiphy_priv(wiphy);
  676. int ret = 0;
  677. if (priv->scan_req || delayed_work_pending(&priv->scan_work)) {
  678. /* old scan request not yet processed */
  679. ret = -EAGAIN;
  680. goto out;
  681. }
  682. _internal_start_scan(priv, false, request);
  683. if (priv->surpriseremoved)
  684. ret = -EIO;
  685. out:
  686. return ret;
  687. }
  688. /*
  689. * Events
  690. */
  691. void lbs_send_disconnect_notification(struct lbs_private *priv,
  692. bool locally_generated)
  693. {
  694. cfg80211_disconnected(priv->dev, 0, NULL, 0, locally_generated,
  695. GFP_KERNEL);
  696. }
  697. void lbs_send_mic_failureevent(struct lbs_private *priv, u32 event)
  698. {
  699. cfg80211_michael_mic_failure(priv->dev,
  700. priv->assoc_bss,
  701. event == MACREG_INT_CODE_MIC_ERR_MULTICAST ?
  702. NL80211_KEYTYPE_GROUP :
  703. NL80211_KEYTYPE_PAIRWISE,
  704. -1,
  705. NULL,
  706. GFP_KERNEL);
  707. }
  708. /*
  709. * Connect/disconnect
  710. */
  711. /*
  712. * This removes all WEP keys
  713. */
  714. static int lbs_remove_wep_keys(struct lbs_private *priv)
  715. {
  716. struct cmd_ds_802_11_set_wep cmd;
  717. int ret;
  718. memset(&cmd, 0, sizeof(cmd));
  719. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  720. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  721. cmd.action = cpu_to_le16(CMD_ACT_REMOVE);
  722. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  723. return ret;
  724. }
  725. /*
  726. * Set WEP keys
  727. */
  728. static int lbs_set_wep_keys(struct lbs_private *priv)
  729. {
  730. struct cmd_ds_802_11_set_wep cmd;
  731. int i;
  732. int ret;
  733. /*
  734. * command 13 00
  735. * size 50 00
  736. * sequence xx xx
  737. * result 00 00
  738. * action 02 00 ACT_ADD
  739. * transmit key 00 00
  740. * type for key 1 01 WEP40
  741. * type for key 2 00
  742. * type for key 3 00
  743. * type for key 4 00
  744. * key 1 39 39 39 39 39 00 00 00
  745. * 00 00 00 00 00 00 00 00
  746. * key 2 00 00 00 00 00 00 00 00
  747. * 00 00 00 00 00 00 00 00
  748. * key 3 00 00 00 00 00 00 00 00
  749. * 00 00 00 00 00 00 00 00
  750. * key 4 00 00 00 00 00 00 00 00
  751. */
  752. if (priv->wep_key_len[0] || priv->wep_key_len[1] ||
  753. priv->wep_key_len[2] || priv->wep_key_len[3]) {
  754. /* Only set wep keys if we have at least one of them */
  755. memset(&cmd, 0, sizeof(cmd));
  756. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  757. cmd.keyindex = cpu_to_le16(priv->wep_tx_key);
  758. cmd.action = cpu_to_le16(CMD_ACT_ADD);
  759. for (i = 0; i < 4; i++) {
  760. switch (priv->wep_key_len[i]) {
  761. case WLAN_KEY_LEN_WEP40:
  762. cmd.keytype[i] = CMD_TYPE_WEP_40_BIT;
  763. break;
  764. case WLAN_KEY_LEN_WEP104:
  765. cmd.keytype[i] = CMD_TYPE_WEP_104_BIT;
  766. break;
  767. default:
  768. cmd.keytype[i] = 0;
  769. break;
  770. }
  771. memcpy(cmd.keymaterial[i], priv->wep_key[i],
  772. priv->wep_key_len[i]);
  773. }
  774. ret = lbs_cmd_with_response(priv, CMD_802_11_SET_WEP, &cmd);
  775. } else {
  776. /* Otherwise remove all wep keys */
  777. ret = lbs_remove_wep_keys(priv);
  778. }
  779. return ret;
  780. }
  781. /*
  782. * Enable/Disable RSN status
  783. */
  784. static int lbs_enable_rsn(struct lbs_private *priv, int enable)
  785. {
  786. struct cmd_ds_802_11_enable_rsn cmd;
  787. int ret;
  788. /*
  789. * cmd 2f 00
  790. * size 0c 00
  791. * sequence xx xx
  792. * result 00 00
  793. * action 01 00 ACT_SET
  794. * enable 01 00
  795. */
  796. memset(&cmd, 0, sizeof(cmd));
  797. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  798. cmd.action = cpu_to_le16(CMD_ACT_SET);
  799. cmd.enable = cpu_to_le16(enable);
  800. ret = lbs_cmd_with_response(priv, CMD_802_11_ENABLE_RSN, &cmd);
  801. return ret;
  802. }
  803. /*
  804. * Set WPA/WPA key material
  805. */
  806. /*
  807. * like "struct cmd_ds_802_11_key_material", but with cmd_header. Once we
  808. * get rid of WEXT, this should go into host.h
  809. */
  810. struct cmd_key_material {
  811. struct cmd_header hdr;
  812. __le16 action;
  813. struct MrvlIEtype_keyParamSet param;
  814. } __packed;
  815. static int lbs_set_key_material(struct lbs_private *priv,
  816. int key_type, int key_info,
  817. const u8 *key, u16 key_len)
  818. {
  819. struct cmd_key_material cmd;
  820. int ret;
  821. /*
  822. * Example for WPA (TKIP):
  823. *
  824. * cmd 5e 00
  825. * size 34 00
  826. * sequence xx xx
  827. * result 00 00
  828. * action 01 00
  829. * TLV type 00 01 key param
  830. * length 00 26
  831. * key type 01 00 TKIP
  832. * key info 06 00 UNICAST | ENABLED
  833. * key len 20 00
  834. * key 32 bytes
  835. */
  836. memset(&cmd, 0, sizeof(cmd));
  837. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  838. cmd.action = cpu_to_le16(CMD_ACT_SET);
  839. cmd.param.type = cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
  840. cmd.param.length = cpu_to_le16(sizeof(cmd.param) - 4);
  841. cmd.param.keytypeid = cpu_to_le16(key_type);
  842. cmd.param.keyinfo = cpu_to_le16(key_info);
  843. cmd.param.keylen = cpu_to_le16(key_len);
  844. if (key && key_len)
  845. memcpy(cmd.param.key, key, key_len);
  846. ret = lbs_cmd_with_response(priv, CMD_802_11_KEY_MATERIAL, &cmd);
  847. return ret;
  848. }
  849. /*
  850. * Sets the auth type (open, shared, etc) in the firmware. That
  851. * we use CMD_802_11_AUTHENTICATE is misleading, this firmware
  852. * command doesn't send an authentication frame at all, it just
  853. * stores the auth_type.
  854. */
  855. static int lbs_set_authtype(struct lbs_private *priv,
  856. struct cfg80211_connect_params *sme)
  857. {
  858. struct cmd_ds_802_11_authenticate cmd;
  859. int ret;
  860. /*
  861. * cmd 11 00
  862. * size 19 00
  863. * sequence xx xx
  864. * result 00 00
  865. * BSS id 00 13 19 80 da 30
  866. * auth type 00
  867. * reserved 00 00 00 00 00 00 00 00 00 00
  868. */
  869. memset(&cmd, 0, sizeof(cmd));
  870. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  871. if (sme->bssid)
  872. memcpy(cmd.bssid, sme->bssid, ETH_ALEN);
  873. /* convert auth_type */
  874. ret = lbs_auth_to_authtype(sme->auth_type);
  875. if (ret < 0)
  876. goto done;
  877. cmd.authtype = ret;
  878. ret = lbs_cmd_with_response(priv, CMD_802_11_AUTHENTICATE, &cmd);
  879. done:
  880. return ret;
  881. }
  882. /*
  883. * Create association request
  884. */
  885. #define LBS_ASSOC_MAX_CMD_SIZE \
  886. (sizeof(struct cmd_ds_802_11_associate) \
  887. - 512 /* cmd_ds_802_11_associate.iebuf */ \
  888. + LBS_MAX_SSID_TLV_SIZE \
  889. + LBS_MAX_CHANNEL_TLV_SIZE \
  890. + LBS_MAX_CF_PARAM_TLV_SIZE \
  891. + LBS_MAX_AUTH_TYPE_TLV_SIZE \
  892. + LBS_MAX_WPA_TLV_SIZE)
  893. static int lbs_associate(struct lbs_private *priv,
  894. struct cfg80211_bss *bss,
  895. struct cfg80211_connect_params *sme)
  896. {
  897. struct cmd_ds_802_11_associate_response *resp;
  898. struct cmd_ds_802_11_associate *cmd = kzalloc(LBS_ASSOC_MAX_CMD_SIZE,
  899. GFP_KERNEL);
  900. const u8 *ssid_eid;
  901. size_t len, resp_ie_len;
  902. int status;
  903. int ret;
  904. u8 *pos;
  905. u8 *tmp;
  906. if (!cmd) {
  907. ret = -ENOMEM;
  908. goto done;
  909. }
  910. pos = &cmd->iebuf[0];
  911. /*
  912. * cmd 50 00
  913. * length 34 00
  914. * sequence xx xx
  915. * result 00 00
  916. * BSS id 00 13 19 80 da 30
  917. * capabilities 11 00
  918. * listen interval 0a 00
  919. * beacon interval 00 00
  920. * DTIM period 00
  921. * TLVs xx (up to 512 bytes)
  922. */
  923. cmd->hdr.command = cpu_to_le16(CMD_802_11_ASSOCIATE);
  924. /* Fill in static fields */
  925. memcpy(cmd->bssid, bss->bssid, ETH_ALEN);
  926. cmd->listeninterval = cpu_to_le16(MRVDRV_DEFAULT_LISTEN_INTERVAL);
  927. cmd->capability = cpu_to_le16(bss->capability);
  928. /* add SSID TLV */
  929. rcu_read_lock();
  930. ssid_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SSID);
  931. if (ssid_eid)
  932. pos += lbs_add_ssid_tlv(pos, ssid_eid + 2, ssid_eid[1]);
  933. else
  934. lbs_deb_assoc("no SSID\n");
  935. rcu_read_unlock();
  936. /* add DS param TLV */
  937. if (bss->channel)
  938. pos += lbs_add_channel_tlv(pos, bss->channel->hw_value);
  939. else
  940. lbs_deb_assoc("no channel\n");
  941. /* add (empty) CF param TLV */
  942. pos += lbs_add_cf_param_tlv(pos);
  943. /* add rates TLV */
  944. tmp = pos + 4; /* skip Marvell IE header */
  945. pos += lbs_add_common_rates_tlv(pos, bss);
  946. lbs_deb_hex(LBS_DEB_ASSOC, "Common Rates", tmp, pos - tmp);
  947. /* add auth type TLV */
  948. if (MRVL_FW_MAJOR_REV(priv->fwrelease) >= 9)
  949. pos += lbs_add_auth_type_tlv(pos, sme->auth_type);
  950. /* add WPA/WPA2 TLV */
  951. if (sme->ie && sme->ie_len)
  952. pos += lbs_add_wpa_tlv(pos, sme->ie, sme->ie_len);
  953. len = (sizeof(*cmd) - sizeof(cmd->iebuf)) +
  954. (u16)(pos - (u8 *) &cmd->iebuf);
  955. cmd->hdr.size = cpu_to_le16(len);
  956. lbs_deb_hex(LBS_DEB_ASSOC, "ASSOC_CMD", (u8 *) cmd,
  957. le16_to_cpu(cmd->hdr.size));
  958. /* store for later use */
  959. memcpy(priv->assoc_bss, bss->bssid, ETH_ALEN);
  960. ret = lbs_cmd_with_response(priv, CMD_802_11_ASSOCIATE, cmd);
  961. if (ret)
  962. goto done;
  963. /* generate connect message to cfg80211 */
  964. resp = (void *) cmd; /* recast for easier field access */
  965. status = le16_to_cpu(resp->statuscode);
  966. /* Older FW versions map the IEEE 802.11 Status Code in the association
  967. * response to the following values returned in resp->statuscode:
  968. *
  969. * IEEE Status Code Marvell Status Code
  970. * 0 -> 0x0000 ASSOC_RESULT_SUCCESS
  971. * 13 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  972. * 14 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  973. * 15 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  974. * 16 -> 0x0004 ASSOC_RESULT_AUTH_REFUSED
  975. * others -> 0x0003 ASSOC_RESULT_REFUSED
  976. *
  977. * Other response codes:
  978. * 0x0001 -> ASSOC_RESULT_INVALID_PARAMETERS (unused)
  979. * 0x0002 -> ASSOC_RESULT_TIMEOUT (internal timer expired waiting for
  980. * association response from the AP)
  981. */
  982. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
  983. switch (status) {
  984. case 0:
  985. break;
  986. case 1:
  987. lbs_deb_assoc("invalid association parameters\n");
  988. status = WLAN_STATUS_CAPS_UNSUPPORTED;
  989. break;
  990. case 2:
  991. lbs_deb_assoc("timer expired while waiting for AP\n");
  992. status = WLAN_STATUS_AUTH_TIMEOUT;
  993. break;
  994. case 3:
  995. lbs_deb_assoc("association refused by AP\n");
  996. status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
  997. break;
  998. case 4:
  999. lbs_deb_assoc("authentication refused by AP\n");
  1000. status = WLAN_STATUS_UNKNOWN_AUTH_TRANSACTION;
  1001. break;
  1002. default:
  1003. lbs_deb_assoc("association failure %d\n", status);
  1004. /* v5 OLPC firmware does return the AP status code if
  1005. * it's not one of the values above. Let that through.
  1006. */
  1007. break;
  1008. }
  1009. }
  1010. lbs_deb_assoc("status %d, statuscode 0x%04x, capability 0x%04x, "
  1011. "aid 0x%04x\n", status, le16_to_cpu(resp->statuscode),
  1012. le16_to_cpu(resp->capability), le16_to_cpu(resp->aid));
  1013. resp_ie_len = le16_to_cpu(resp->hdr.size)
  1014. - sizeof(resp->hdr)
  1015. - 6;
  1016. cfg80211_connect_result(priv->dev,
  1017. priv->assoc_bss,
  1018. sme->ie, sme->ie_len,
  1019. resp->iebuf, resp_ie_len,
  1020. status,
  1021. GFP_KERNEL);
  1022. if (status == 0) {
  1023. /* TODO: get rid of priv->connect_status */
  1024. priv->connect_status = LBS_CONNECTED;
  1025. netif_carrier_on(priv->dev);
  1026. if (!priv->tx_pending_len)
  1027. netif_tx_wake_all_queues(priv->dev);
  1028. }
  1029. kfree(cmd);
  1030. done:
  1031. return ret;
  1032. }
  1033. static struct cfg80211_scan_request *
  1034. _new_connect_scan_req(struct wiphy *wiphy, struct cfg80211_connect_params *sme)
  1035. {
  1036. struct cfg80211_scan_request *creq = NULL;
  1037. int i, n_channels = ieee80211_get_num_supported_channels(wiphy);
  1038. enum nl80211_band band;
  1039. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1040. n_channels * sizeof(void *),
  1041. GFP_ATOMIC);
  1042. if (!creq)
  1043. return NULL;
  1044. /* SSIDs come after channels */
  1045. creq->ssids = (void *)&creq->channels[n_channels];
  1046. creq->n_channels = n_channels;
  1047. creq->n_ssids = 1;
  1048. /* Scan all available channels */
  1049. i = 0;
  1050. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1051. int j;
  1052. if (!wiphy->bands[band])
  1053. continue;
  1054. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1055. /* ignore disabled channels */
  1056. if (wiphy->bands[band]->channels[j].flags &
  1057. IEEE80211_CHAN_DISABLED)
  1058. continue;
  1059. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1060. i++;
  1061. }
  1062. }
  1063. if (i) {
  1064. /* Set real number of channels specified in creq->channels[] */
  1065. creq->n_channels = i;
  1066. /* Scan for the SSID we're going to connect to */
  1067. memcpy(creq->ssids[0].ssid, sme->ssid, sme->ssid_len);
  1068. creq->ssids[0].ssid_len = sme->ssid_len;
  1069. } else {
  1070. /* No channels found... */
  1071. kfree(creq);
  1072. creq = NULL;
  1073. }
  1074. return creq;
  1075. }
  1076. static int lbs_cfg_connect(struct wiphy *wiphy, struct net_device *dev,
  1077. struct cfg80211_connect_params *sme)
  1078. {
  1079. struct lbs_private *priv = wiphy_priv(wiphy);
  1080. struct cfg80211_bss *bss = NULL;
  1081. int ret = 0;
  1082. u8 preamble = RADIO_PREAMBLE_SHORT;
  1083. if (dev == priv->mesh_dev)
  1084. return -EOPNOTSUPP;
  1085. if (!sme->bssid) {
  1086. struct cfg80211_scan_request *creq;
  1087. /*
  1088. * Scan for the requested network after waiting for existing
  1089. * scans to finish.
  1090. */
  1091. lbs_deb_assoc("assoc: waiting for existing scans\n");
  1092. wait_event_interruptible_timeout(priv->scan_q,
  1093. (priv->scan_req == NULL),
  1094. (15 * HZ));
  1095. creq = _new_connect_scan_req(wiphy, sme);
  1096. if (!creq) {
  1097. ret = -EINVAL;
  1098. goto done;
  1099. }
  1100. lbs_deb_assoc("assoc: scanning for compatible AP\n");
  1101. _internal_start_scan(priv, true, creq);
  1102. lbs_deb_assoc("assoc: waiting for scan to complete\n");
  1103. wait_event_interruptible_timeout(priv->scan_q,
  1104. (priv->scan_req == NULL),
  1105. (15 * HZ));
  1106. lbs_deb_assoc("assoc: scanning completed\n");
  1107. }
  1108. /* Find the BSS we want using available scan results */
  1109. bss = cfg80211_get_bss(wiphy, sme->channel, sme->bssid,
  1110. sme->ssid, sme->ssid_len, IEEE80211_BSS_TYPE_ESS,
  1111. IEEE80211_PRIVACY_ANY);
  1112. if (!bss) {
  1113. wiphy_err(wiphy, "assoc: bss %pM not in scan results\n",
  1114. sme->bssid);
  1115. ret = -ENOENT;
  1116. goto done;
  1117. }
  1118. lbs_deb_assoc("trying %pM\n", bss->bssid);
  1119. lbs_deb_assoc("cipher 0x%x, key index %d, key len %d\n",
  1120. sme->crypto.cipher_group,
  1121. sme->key_idx, sme->key_len);
  1122. /* As this is a new connection, clear locally stored WEP keys */
  1123. priv->wep_tx_key = 0;
  1124. memset(priv->wep_key, 0, sizeof(priv->wep_key));
  1125. memset(priv->wep_key_len, 0, sizeof(priv->wep_key_len));
  1126. /* set/remove WEP keys */
  1127. switch (sme->crypto.cipher_group) {
  1128. case WLAN_CIPHER_SUITE_WEP40:
  1129. case WLAN_CIPHER_SUITE_WEP104:
  1130. /* Store provided WEP keys in priv-> */
  1131. priv->wep_tx_key = sme->key_idx;
  1132. priv->wep_key_len[sme->key_idx] = sme->key_len;
  1133. memcpy(priv->wep_key[sme->key_idx], sme->key, sme->key_len);
  1134. /* Set WEP keys and WEP mode */
  1135. lbs_set_wep_keys(priv);
  1136. priv->mac_control |= CMD_ACT_MAC_WEP_ENABLE;
  1137. lbs_set_mac_control(priv);
  1138. /* No RSN mode for WEP */
  1139. lbs_enable_rsn(priv, 0);
  1140. break;
  1141. case 0: /* there's no WLAN_CIPHER_SUITE_NONE definition */
  1142. /*
  1143. * If we don't have no WEP, no WPA and no WPA2,
  1144. * we remove all keys like in the WPA/WPA2 setup,
  1145. * we just don't set RSN.
  1146. *
  1147. * Therefore: fall-through
  1148. */
  1149. case WLAN_CIPHER_SUITE_TKIP:
  1150. case WLAN_CIPHER_SUITE_CCMP:
  1151. /* Remove WEP keys and WEP mode */
  1152. lbs_remove_wep_keys(priv);
  1153. priv->mac_control &= ~CMD_ACT_MAC_WEP_ENABLE;
  1154. lbs_set_mac_control(priv);
  1155. /* clear the WPA/WPA2 keys */
  1156. lbs_set_key_material(priv,
  1157. KEY_TYPE_ID_WEP, /* doesn't matter */
  1158. KEY_INFO_WPA_UNICAST,
  1159. NULL, 0);
  1160. lbs_set_key_material(priv,
  1161. KEY_TYPE_ID_WEP, /* doesn't matter */
  1162. KEY_INFO_WPA_MCAST,
  1163. NULL, 0);
  1164. /* RSN mode for WPA/WPA2 */
  1165. lbs_enable_rsn(priv, sme->crypto.cipher_group != 0);
  1166. break;
  1167. default:
  1168. wiphy_err(wiphy, "unsupported cipher group 0x%x\n",
  1169. sme->crypto.cipher_group);
  1170. ret = -ENOTSUPP;
  1171. goto done;
  1172. }
  1173. ret = lbs_set_authtype(priv, sme);
  1174. if (ret == -ENOTSUPP) {
  1175. wiphy_err(wiphy, "unsupported authtype 0x%x\n", sme->auth_type);
  1176. goto done;
  1177. }
  1178. lbs_set_radio(priv, preamble, 1);
  1179. /* Do the actual association */
  1180. ret = lbs_associate(priv, bss, sme);
  1181. done:
  1182. if (bss)
  1183. cfg80211_put_bss(wiphy, bss);
  1184. return ret;
  1185. }
  1186. int lbs_disconnect(struct lbs_private *priv, u16 reason)
  1187. {
  1188. struct cmd_ds_802_11_deauthenticate cmd;
  1189. int ret;
  1190. memset(&cmd, 0, sizeof(cmd));
  1191. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1192. /* Mildly ugly to use a locally store my own BSSID ... */
  1193. memcpy(cmd.macaddr, &priv->assoc_bss, ETH_ALEN);
  1194. cmd.reasoncode = cpu_to_le16(reason);
  1195. ret = lbs_cmd_with_response(priv, CMD_802_11_DEAUTHENTICATE, &cmd);
  1196. if (ret)
  1197. return ret;
  1198. cfg80211_disconnected(priv->dev,
  1199. reason,
  1200. NULL, 0, true,
  1201. GFP_KERNEL);
  1202. priv->connect_status = LBS_DISCONNECTED;
  1203. return 0;
  1204. }
  1205. static int lbs_cfg_disconnect(struct wiphy *wiphy, struct net_device *dev,
  1206. u16 reason_code)
  1207. {
  1208. struct lbs_private *priv = wiphy_priv(wiphy);
  1209. if (dev == priv->mesh_dev)
  1210. return -EOPNOTSUPP;
  1211. /* store for lbs_cfg_ret_disconnect() */
  1212. priv->disassoc_reason = reason_code;
  1213. return lbs_disconnect(priv, reason_code);
  1214. }
  1215. static int lbs_cfg_set_default_key(struct wiphy *wiphy,
  1216. struct net_device *netdev,
  1217. u8 key_index, bool unicast,
  1218. bool multicast)
  1219. {
  1220. struct lbs_private *priv = wiphy_priv(wiphy);
  1221. if (netdev == priv->mesh_dev)
  1222. return -EOPNOTSUPP;
  1223. if (key_index != priv->wep_tx_key) {
  1224. lbs_deb_assoc("set_default_key: to %d\n", key_index);
  1225. priv->wep_tx_key = key_index;
  1226. lbs_set_wep_keys(priv);
  1227. }
  1228. return 0;
  1229. }
  1230. static int lbs_cfg_add_key(struct wiphy *wiphy, struct net_device *netdev,
  1231. u8 idx, bool pairwise, const u8 *mac_addr,
  1232. struct key_params *params)
  1233. {
  1234. struct lbs_private *priv = wiphy_priv(wiphy);
  1235. u16 key_info;
  1236. u16 key_type;
  1237. int ret = 0;
  1238. if (netdev == priv->mesh_dev)
  1239. return -EOPNOTSUPP;
  1240. lbs_deb_assoc("add_key: cipher 0x%x, mac_addr %pM\n",
  1241. params->cipher, mac_addr);
  1242. lbs_deb_assoc("add_key: key index %d, key len %d\n",
  1243. idx, params->key_len);
  1244. if (params->key_len)
  1245. lbs_deb_hex(LBS_DEB_CFG80211, "KEY",
  1246. params->key, params->key_len);
  1247. lbs_deb_assoc("add_key: seq len %d\n", params->seq_len);
  1248. if (params->seq_len)
  1249. lbs_deb_hex(LBS_DEB_CFG80211, "SEQ",
  1250. params->seq, params->seq_len);
  1251. switch (params->cipher) {
  1252. case WLAN_CIPHER_SUITE_WEP40:
  1253. case WLAN_CIPHER_SUITE_WEP104:
  1254. /* actually compare if something has changed ... */
  1255. if ((priv->wep_key_len[idx] != params->key_len) ||
  1256. memcmp(priv->wep_key[idx],
  1257. params->key, params->key_len) != 0) {
  1258. priv->wep_key_len[idx] = params->key_len;
  1259. memcpy(priv->wep_key[idx],
  1260. params->key, params->key_len);
  1261. lbs_set_wep_keys(priv);
  1262. }
  1263. break;
  1264. case WLAN_CIPHER_SUITE_TKIP:
  1265. case WLAN_CIPHER_SUITE_CCMP:
  1266. key_info = KEY_INFO_WPA_ENABLED | ((idx == 0)
  1267. ? KEY_INFO_WPA_UNICAST
  1268. : KEY_INFO_WPA_MCAST);
  1269. key_type = (params->cipher == WLAN_CIPHER_SUITE_TKIP)
  1270. ? KEY_TYPE_ID_TKIP
  1271. : KEY_TYPE_ID_AES;
  1272. lbs_set_key_material(priv,
  1273. key_type,
  1274. key_info,
  1275. params->key, params->key_len);
  1276. break;
  1277. default:
  1278. wiphy_err(wiphy, "unhandled cipher 0x%x\n", params->cipher);
  1279. ret = -ENOTSUPP;
  1280. break;
  1281. }
  1282. return ret;
  1283. }
  1284. static int lbs_cfg_del_key(struct wiphy *wiphy, struct net_device *netdev,
  1285. u8 key_index, bool pairwise, const u8 *mac_addr)
  1286. {
  1287. lbs_deb_assoc("del_key: key_idx %d, mac_addr %pM\n",
  1288. key_index, mac_addr);
  1289. #ifdef TODO
  1290. struct lbs_private *priv = wiphy_priv(wiphy);
  1291. /*
  1292. * I think can keep this a NO-OP, because:
  1293. * - we clear all keys whenever we do lbs_cfg_connect() anyway
  1294. * - neither "iw" nor "wpa_supplicant" won't call this during
  1295. * an ongoing connection
  1296. * - TODO: but I have to check if this is still true when
  1297. * I set the AP to periodic re-keying
  1298. * - we've not kzallec() something when we've added a key at
  1299. * lbs_cfg_connect() or lbs_cfg_add_key().
  1300. *
  1301. * This causes lbs_cfg_del_key() only called at disconnect time,
  1302. * where we'd just waste time deleting a key that is not going
  1303. * to be used anyway.
  1304. */
  1305. if (key_index < 3 && priv->wep_key_len[key_index]) {
  1306. priv->wep_key_len[key_index] = 0;
  1307. lbs_set_wep_keys(priv);
  1308. }
  1309. #endif
  1310. return 0;
  1311. }
  1312. /*
  1313. * Get station
  1314. */
  1315. static int lbs_cfg_get_station(struct wiphy *wiphy, struct net_device *dev,
  1316. const u8 *mac, struct station_info *sinfo)
  1317. {
  1318. struct lbs_private *priv = wiphy_priv(wiphy);
  1319. s8 signal, noise;
  1320. int ret;
  1321. size_t i;
  1322. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES) |
  1323. BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
  1324. BIT_ULL(NL80211_STA_INFO_RX_BYTES) |
  1325. BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
  1326. sinfo->tx_bytes = priv->dev->stats.tx_bytes;
  1327. sinfo->tx_packets = priv->dev->stats.tx_packets;
  1328. sinfo->rx_bytes = priv->dev->stats.rx_bytes;
  1329. sinfo->rx_packets = priv->dev->stats.rx_packets;
  1330. /* Get current RSSI */
  1331. ret = lbs_get_rssi(priv, &signal, &noise);
  1332. if (ret == 0) {
  1333. sinfo->signal = signal;
  1334. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
  1335. }
  1336. /* Convert priv->cur_rate from hw_value to NL80211 value */
  1337. for (i = 0; i < ARRAY_SIZE(lbs_rates); i++) {
  1338. if (priv->cur_rate == lbs_rates[i].hw_value) {
  1339. sinfo->txrate.legacy = lbs_rates[i].bitrate;
  1340. sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
  1341. break;
  1342. }
  1343. }
  1344. return 0;
  1345. }
  1346. /*
  1347. * Change interface
  1348. */
  1349. static int lbs_change_intf(struct wiphy *wiphy, struct net_device *dev,
  1350. enum nl80211_iftype type,
  1351. struct vif_params *params)
  1352. {
  1353. struct lbs_private *priv = wiphy_priv(wiphy);
  1354. int ret = 0;
  1355. if (dev == priv->mesh_dev)
  1356. return -EOPNOTSUPP;
  1357. switch (type) {
  1358. case NL80211_IFTYPE_MONITOR:
  1359. case NL80211_IFTYPE_STATION:
  1360. case NL80211_IFTYPE_ADHOC:
  1361. break;
  1362. default:
  1363. return -EOPNOTSUPP;
  1364. }
  1365. if (priv->iface_running)
  1366. ret = lbs_set_iface_type(priv, type);
  1367. if (!ret)
  1368. priv->wdev->iftype = type;
  1369. return ret;
  1370. }
  1371. /*
  1372. * IBSS (Ad-Hoc)
  1373. */
  1374. /*
  1375. * The firmware needs the following bits masked out of the beacon-derived
  1376. * capability field when associating/joining to a BSS:
  1377. * 9 (QoS), 11 (APSD), 12 (unused), 14 (unused), 15 (unused)
  1378. */
  1379. #define CAPINFO_MASK (~(0xda00))
  1380. static void lbs_join_post(struct lbs_private *priv,
  1381. struct cfg80211_ibss_params *params,
  1382. u8 *bssid, u16 capability)
  1383. {
  1384. u8 fake_ie[2 + IEEE80211_MAX_SSID_LEN + /* ssid */
  1385. 2 + 4 + /* basic rates */
  1386. 2 + 1 + /* DS parameter */
  1387. 2 + 2 + /* atim */
  1388. 2 + 8]; /* extended rates */
  1389. u8 *fake = fake_ie;
  1390. struct cfg80211_bss *bss;
  1391. /*
  1392. * For cfg80211_inform_bss, we'll need a fake IE, as we can't get
  1393. * the real IE from the firmware. So we fabricate a fake IE based on
  1394. * what the firmware actually sends (sniffed with wireshark).
  1395. */
  1396. /* Fake SSID IE */
  1397. *fake++ = WLAN_EID_SSID;
  1398. *fake++ = params->ssid_len;
  1399. memcpy(fake, params->ssid, params->ssid_len);
  1400. fake += params->ssid_len;
  1401. /* Fake supported basic rates IE */
  1402. *fake++ = WLAN_EID_SUPP_RATES;
  1403. *fake++ = 4;
  1404. *fake++ = 0x82;
  1405. *fake++ = 0x84;
  1406. *fake++ = 0x8b;
  1407. *fake++ = 0x96;
  1408. /* Fake DS channel IE */
  1409. *fake++ = WLAN_EID_DS_PARAMS;
  1410. *fake++ = 1;
  1411. *fake++ = params->chandef.chan->hw_value;
  1412. /* Fake IBSS params IE */
  1413. *fake++ = WLAN_EID_IBSS_PARAMS;
  1414. *fake++ = 2;
  1415. *fake++ = 0; /* ATIM=0 */
  1416. *fake++ = 0;
  1417. /* Fake extended rates IE, TODO: don't add this for 802.11b only,
  1418. * but I don't know how this could be checked */
  1419. *fake++ = WLAN_EID_EXT_SUPP_RATES;
  1420. *fake++ = 8;
  1421. *fake++ = 0x0c;
  1422. *fake++ = 0x12;
  1423. *fake++ = 0x18;
  1424. *fake++ = 0x24;
  1425. *fake++ = 0x30;
  1426. *fake++ = 0x48;
  1427. *fake++ = 0x60;
  1428. *fake++ = 0x6c;
  1429. lbs_deb_hex(LBS_DEB_CFG80211, "IE", fake_ie, fake - fake_ie);
  1430. bss = cfg80211_inform_bss(priv->wdev->wiphy,
  1431. params->chandef.chan,
  1432. CFG80211_BSS_FTYPE_UNKNOWN,
  1433. bssid,
  1434. 0,
  1435. capability,
  1436. params->beacon_interval,
  1437. fake_ie, fake - fake_ie,
  1438. 0, GFP_KERNEL);
  1439. cfg80211_put_bss(priv->wdev->wiphy, bss);
  1440. cfg80211_ibss_joined(priv->dev, bssid, params->chandef.chan,
  1441. GFP_KERNEL);
  1442. /* TODO: consider doing this at MACREG_INT_CODE_LINK_SENSED time */
  1443. priv->connect_status = LBS_CONNECTED;
  1444. netif_carrier_on(priv->dev);
  1445. if (!priv->tx_pending_len)
  1446. netif_wake_queue(priv->dev);
  1447. }
  1448. static int lbs_ibss_join_existing(struct lbs_private *priv,
  1449. struct cfg80211_ibss_params *params,
  1450. struct cfg80211_bss *bss)
  1451. {
  1452. const u8 *rates_eid;
  1453. struct cmd_ds_802_11_ad_hoc_join cmd;
  1454. u8 preamble = RADIO_PREAMBLE_SHORT;
  1455. int ret = 0;
  1456. int hw, i;
  1457. u8 rates_max;
  1458. u8 *rates;
  1459. /* TODO: set preamble based on scan result */
  1460. ret = lbs_set_radio(priv, preamble, 1);
  1461. if (ret)
  1462. goto out;
  1463. /*
  1464. * Example CMD_802_11_AD_HOC_JOIN command:
  1465. *
  1466. * command 2c 00 CMD_802_11_AD_HOC_JOIN
  1467. * size 65 00
  1468. * sequence xx xx
  1469. * result 00 00
  1470. * bssid 02 27 27 97 2f 96
  1471. * ssid 49 42 53 53 00 00 00 00
  1472. * 00 00 00 00 00 00 00 00
  1473. * 00 00 00 00 00 00 00 00
  1474. * 00 00 00 00 00 00 00 00
  1475. * type 02 CMD_BSS_TYPE_IBSS
  1476. * beacon period 64 00
  1477. * dtim period 00
  1478. * timestamp 00 00 00 00 00 00 00 00
  1479. * localtime 00 00 00 00 00 00 00 00
  1480. * IE DS 03
  1481. * IE DS len 01
  1482. * IE DS channel 01
  1483. * reserveed 00 00 00 00
  1484. * IE IBSS 06
  1485. * IE IBSS len 02
  1486. * IE IBSS atim 00 00
  1487. * reserved 00 00 00 00
  1488. * capability 02 00
  1489. * rates 82 84 8b 96 0c 12 18 24 30 48 60 6c 00
  1490. * fail timeout ff 00
  1491. * probe delay 00 00
  1492. */
  1493. memset(&cmd, 0, sizeof(cmd));
  1494. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1495. memcpy(cmd.bss.bssid, bss->bssid, ETH_ALEN);
  1496. memcpy(cmd.bss.ssid, params->ssid, params->ssid_len);
  1497. cmd.bss.type = CMD_BSS_TYPE_IBSS;
  1498. cmd.bss.beaconperiod = cpu_to_le16(params->beacon_interval);
  1499. cmd.bss.ds.header.id = WLAN_EID_DS_PARAMS;
  1500. cmd.bss.ds.header.len = 1;
  1501. cmd.bss.ds.channel = params->chandef.chan->hw_value;
  1502. cmd.bss.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1503. cmd.bss.ibss.header.len = 2;
  1504. cmd.bss.ibss.atimwindow = 0;
  1505. cmd.bss.capability = cpu_to_le16(bss->capability & CAPINFO_MASK);
  1506. /* set rates to the intersection of our rates and the rates in the
  1507. bss */
  1508. rcu_read_lock();
  1509. rates_eid = ieee80211_bss_get_ie(bss, WLAN_EID_SUPP_RATES);
  1510. if (!rates_eid) {
  1511. lbs_add_rates(cmd.bss.rates);
  1512. } else {
  1513. rates_max = rates_eid[1];
  1514. if (rates_max > MAX_RATES) {
  1515. lbs_deb_join("invalid rates");
  1516. rcu_read_unlock();
  1517. ret = -EINVAL;
  1518. goto out;
  1519. }
  1520. rates = cmd.bss.rates;
  1521. for (hw = 0; hw < ARRAY_SIZE(lbs_rates); hw++) {
  1522. u8 hw_rate = lbs_rates[hw].bitrate / 5;
  1523. for (i = 0; i < rates_max; i++) {
  1524. if (hw_rate == (rates_eid[i+2] & 0x7f)) {
  1525. u8 rate = rates_eid[i+2];
  1526. if (rate == 0x02 || rate == 0x04 ||
  1527. rate == 0x0b || rate == 0x16)
  1528. rate |= 0x80;
  1529. *rates++ = rate;
  1530. }
  1531. }
  1532. }
  1533. }
  1534. rcu_read_unlock();
  1535. /* Only v8 and below support setting this */
  1536. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8) {
  1537. cmd.failtimeout = cpu_to_le16(MRVDRV_ASSOCIATION_TIME_OUT);
  1538. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1539. }
  1540. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_JOIN, &cmd);
  1541. if (ret)
  1542. goto out;
  1543. /*
  1544. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1545. *
  1546. * response 2c 80
  1547. * size 09 00
  1548. * sequence xx xx
  1549. * result 00 00
  1550. * reserved 00
  1551. */
  1552. lbs_join_post(priv, params, bss->bssid, bss->capability);
  1553. out:
  1554. return ret;
  1555. }
  1556. static int lbs_ibss_start_new(struct lbs_private *priv,
  1557. struct cfg80211_ibss_params *params)
  1558. {
  1559. struct cmd_ds_802_11_ad_hoc_start cmd;
  1560. struct cmd_ds_802_11_ad_hoc_result *resp =
  1561. (struct cmd_ds_802_11_ad_hoc_result *) &cmd;
  1562. u8 preamble = RADIO_PREAMBLE_SHORT;
  1563. int ret = 0;
  1564. u16 capability;
  1565. ret = lbs_set_radio(priv, preamble, 1);
  1566. if (ret)
  1567. goto out;
  1568. /*
  1569. * Example CMD_802_11_AD_HOC_START command:
  1570. *
  1571. * command 2b 00 CMD_802_11_AD_HOC_START
  1572. * size b1 00
  1573. * sequence xx xx
  1574. * result 00 00
  1575. * ssid 54 45 53 54 00 00 00 00
  1576. * 00 00 00 00 00 00 00 00
  1577. * 00 00 00 00 00 00 00 00
  1578. * 00 00 00 00 00 00 00 00
  1579. * bss type 02
  1580. * beacon period 64 00
  1581. * dtim period 00
  1582. * IE IBSS 06
  1583. * IE IBSS len 02
  1584. * IE IBSS atim 00 00
  1585. * reserved 00 00 00 00
  1586. * IE DS 03
  1587. * IE DS len 01
  1588. * IE DS channel 01
  1589. * reserved 00 00 00 00
  1590. * probe delay 00 00
  1591. * capability 02 00
  1592. * rates 82 84 8b 96 (basic rates with have bit 7 set)
  1593. * 0c 12 18 24 30 48 60 6c
  1594. * padding 100 bytes
  1595. */
  1596. memset(&cmd, 0, sizeof(cmd));
  1597. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1598. memcpy(cmd.ssid, params->ssid, params->ssid_len);
  1599. cmd.bsstype = CMD_BSS_TYPE_IBSS;
  1600. cmd.beaconperiod = cpu_to_le16(params->beacon_interval);
  1601. cmd.ibss.header.id = WLAN_EID_IBSS_PARAMS;
  1602. cmd.ibss.header.len = 2;
  1603. cmd.ibss.atimwindow = 0;
  1604. cmd.ds.header.id = WLAN_EID_DS_PARAMS;
  1605. cmd.ds.header.len = 1;
  1606. cmd.ds.channel = params->chandef.chan->hw_value;
  1607. /* Only v8 and below support setting probe delay */
  1608. if (MRVL_FW_MAJOR_REV(priv->fwrelease) <= 8)
  1609. cmd.probedelay = cpu_to_le16(CMD_SCAN_PROBE_DELAY_TIME);
  1610. /* TODO: mix in WLAN_CAPABILITY_PRIVACY */
  1611. capability = WLAN_CAPABILITY_IBSS;
  1612. cmd.capability = cpu_to_le16(capability);
  1613. lbs_add_rates(cmd.rates);
  1614. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_START, &cmd);
  1615. if (ret)
  1616. goto out;
  1617. /*
  1618. * This is a sample response to CMD_802_11_AD_HOC_JOIN:
  1619. *
  1620. * response 2b 80
  1621. * size 14 00
  1622. * sequence xx xx
  1623. * result 00 00
  1624. * reserved 00
  1625. * bssid 02 2b 7b 0f 86 0e
  1626. */
  1627. lbs_join_post(priv, params, resp->bssid, capability);
  1628. out:
  1629. return ret;
  1630. }
  1631. static int lbs_join_ibss(struct wiphy *wiphy, struct net_device *dev,
  1632. struct cfg80211_ibss_params *params)
  1633. {
  1634. struct lbs_private *priv = wiphy_priv(wiphy);
  1635. int ret = 0;
  1636. struct cfg80211_bss *bss;
  1637. if (dev == priv->mesh_dev)
  1638. return -EOPNOTSUPP;
  1639. if (!params->chandef.chan) {
  1640. ret = -ENOTSUPP;
  1641. goto out;
  1642. }
  1643. ret = lbs_set_channel(priv, params->chandef.chan->hw_value);
  1644. if (ret)
  1645. goto out;
  1646. /* Search if someone is beaconing. This assumes that the
  1647. * bss list is populated already */
  1648. bss = cfg80211_get_bss(wiphy, params->chandef.chan, params->bssid,
  1649. params->ssid, params->ssid_len,
  1650. IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
  1651. if (bss) {
  1652. ret = lbs_ibss_join_existing(priv, params, bss);
  1653. cfg80211_put_bss(wiphy, bss);
  1654. } else
  1655. ret = lbs_ibss_start_new(priv, params);
  1656. out:
  1657. return ret;
  1658. }
  1659. static int lbs_leave_ibss(struct wiphy *wiphy, struct net_device *dev)
  1660. {
  1661. struct lbs_private *priv = wiphy_priv(wiphy);
  1662. struct cmd_ds_802_11_ad_hoc_stop cmd;
  1663. int ret = 0;
  1664. if (dev == priv->mesh_dev)
  1665. return -EOPNOTSUPP;
  1666. memset(&cmd, 0, sizeof(cmd));
  1667. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  1668. ret = lbs_cmd_with_response(priv, CMD_802_11_AD_HOC_STOP, &cmd);
  1669. /* TODO: consider doing this at MACREG_INT_CODE_ADHOC_BCN_LOST time */
  1670. lbs_mac_event_disconnected(priv, true);
  1671. return ret;
  1672. }
  1673. static int lbs_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
  1674. bool enabled, int timeout)
  1675. {
  1676. struct lbs_private *priv = wiphy_priv(wiphy);
  1677. if (!(priv->fwcapinfo & FW_CAPINFO_PS)) {
  1678. if (!enabled)
  1679. return 0;
  1680. else
  1681. return -EINVAL;
  1682. }
  1683. /* firmware does not work well with too long latency with power saving
  1684. * enabled, so do not enable it if there is only polling, no
  1685. * interrupts (like in some sdio hosts which can only
  1686. * poll for sdio irqs)
  1687. */
  1688. if (priv->is_polling) {
  1689. if (!enabled)
  1690. return 0;
  1691. else
  1692. return -EINVAL;
  1693. }
  1694. if (!enabled) {
  1695. priv->psmode = LBS802_11POWERMODECAM;
  1696. if (priv->psstate != PS_STATE_FULL_POWER)
  1697. lbs_set_ps_mode(priv,
  1698. PS_MODE_ACTION_EXIT_PS,
  1699. true);
  1700. return 0;
  1701. }
  1702. if (priv->psmode != LBS802_11POWERMODECAM)
  1703. return 0;
  1704. priv->psmode = LBS802_11POWERMODEMAX_PSP;
  1705. if (priv->connect_status == LBS_CONNECTED)
  1706. lbs_set_ps_mode(priv, PS_MODE_ACTION_ENTER_PS, true);
  1707. return 0;
  1708. }
  1709. /*
  1710. * Initialization
  1711. */
  1712. static const struct cfg80211_ops lbs_cfg80211_ops = {
  1713. .set_monitor_channel = lbs_cfg_set_monitor_channel,
  1714. .libertas_set_mesh_channel = lbs_cfg_set_mesh_channel,
  1715. .scan = lbs_cfg_scan,
  1716. .connect = lbs_cfg_connect,
  1717. .disconnect = lbs_cfg_disconnect,
  1718. .add_key = lbs_cfg_add_key,
  1719. .del_key = lbs_cfg_del_key,
  1720. .set_default_key = lbs_cfg_set_default_key,
  1721. .get_station = lbs_cfg_get_station,
  1722. .change_virtual_intf = lbs_change_intf,
  1723. .join_ibss = lbs_join_ibss,
  1724. .leave_ibss = lbs_leave_ibss,
  1725. .set_power_mgmt = lbs_set_power_mgmt,
  1726. };
  1727. /*
  1728. * At this time lbs_private *priv doesn't even exist, so we just allocate
  1729. * memory and don't initialize the wiphy further. This is postponed until we
  1730. * can talk to the firmware and happens at registration time in
  1731. * lbs_cfg_wiphy_register().
  1732. */
  1733. struct wireless_dev *lbs_cfg_alloc(struct device *dev)
  1734. {
  1735. int ret = 0;
  1736. struct wireless_dev *wdev;
  1737. wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
  1738. if (!wdev)
  1739. return ERR_PTR(-ENOMEM);
  1740. wdev->wiphy = wiphy_new(&lbs_cfg80211_ops, sizeof(struct lbs_private));
  1741. if (!wdev->wiphy) {
  1742. dev_err(dev, "cannot allocate wiphy\n");
  1743. ret = -ENOMEM;
  1744. goto err_wiphy_new;
  1745. }
  1746. return wdev;
  1747. err_wiphy_new:
  1748. kfree(wdev);
  1749. return ERR_PTR(ret);
  1750. }
  1751. static void lbs_cfg_set_regulatory_hint(struct lbs_private *priv)
  1752. {
  1753. struct region_code_mapping {
  1754. const char *cn;
  1755. int code;
  1756. };
  1757. /* Section 5.17.2 */
  1758. static const struct region_code_mapping regmap[] = {
  1759. {"US ", 0x10}, /* US FCC */
  1760. {"CA ", 0x20}, /* Canada */
  1761. {"EU ", 0x30}, /* ETSI */
  1762. {"ES ", 0x31}, /* Spain */
  1763. {"FR ", 0x32}, /* France */
  1764. {"JP ", 0x40}, /* Japan */
  1765. };
  1766. size_t i;
  1767. for (i = 0; i < ARRAY_SIZE(regmap); i++)
  1768. if (regmap[i].code == priv->regioncode) {
  1769. regulatory_hint(priv->wdev->wiphy, regmap[i].cn);
  1770. break;
  1771. }
  1772. }
  1773. static void lbs_reg_notifier(struct wiphy *wiphy,
  1774. struct regulatory_request *request)
  1775. {
  1776. struct lbs_private *priv = wiphy_priv(wiphy);
  1777. memcpy(priv->country_code, request->alpha2, sizeof(request->alpha2));
  1778. if (lbs_iface_active(priv))
  1779. lbs_set_11d_domain_info(priv);
  1780. }
  1781. /*
  1782. * This function get's called after lbs_setup_firmware() determined the
  1783. * firmware capabities. So we can setup the wiphy according to our
  1784. * hardware/firmware.
  1785. */
  1786. int lbs_cfg_register(struct lbs_private *priv)
  1787. {
  1788. struct wireless_dev *wdev = priv->wdev;
  1789. int ret;
  1790. wdev->wiphy->max_scan_ssids = 1;
  1791. wdev->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
  1792. wdev->wiphy->interface_modes =
  1793. BIT(NL80211_IFTYPE_STATION) |
  1794. BIT(NL80211_IFTYPE_ADHOC);
  1795. if (lbs_rtap_supported(priv))
  1796. wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
  1797. if (lbs_mesh_activated(priv))
  1798. wdev->wiphy->interface_modes |= BIT(NL80211_IFTYPE_MESH_POINT);
  1799. wdev->wiphy->bands[NL80211_BAND_2GHZ] = &lbs_band_2ghz;
  1800. /*
  1801. * We could check priv->fwcapinfo && FW_CAPINFO_WPA, but I have
  1802. * never seen a firmware without WPA
  1803. */
  1804. wdev->wiphy->cipher_suites = cipher_suites;
  1805. wdev->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
  1806. wdev->wiphy->reg_notifier = lbs_reg_notifier;
  1807. ret = wiphy_register(wdev->wiphy);
  1808. if (ret < 0)
  1809. pr_err("cannot register wiphy device\n");
  1810. priv->wiphy_registered = true;
  1811. ret = register_netdev(priv->dev);
  1812. if (ret)
  1813. pr_err("cannot register network device\n");
  1814. INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
  1815. lbs_cfg_set_regulatory_hint(priv);
  1816. return ret;
  1817. }
  1818. void lbs_scan_deinit(struct lbs_private *priv)
  1819. {
  1820. cancel_delayed_work_sync(&priv->scan_work);
  1821. }
  1822. void lbs_cfg_free(struct lbs_private *priv)
  1823. {
  1824. struct wireless_dev *wdev = priv->wdev;
  1825. if (!wdev)
  1826. return;
  1827. if (priv->wiphy_registered)
  1828. wiphy_unregister(wdev->wiphy);
  1829. if (wdev->wiphy)
  1830. wiphy_free(wdev->wiphy);
  1831. kfree(wdev);
  1832. }