cfg.c 54 KB

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