cfg.c 54 KB

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