cfg.c 54 KB

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